Thierry Fournier | e726b14 | 2016-02-11 17:57:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Lua unsafe core engine |
| 3 | * |
| 4 | * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Bertrand Jacquin | f4c12d4 | 2021-01-21 21:14:07 +0000 | [diff] [blame] | 13 | #define _GNU_SOURCE |
| 14 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 15 | #include <ctype.h> |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 16 | #include <setjmp.h> |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 17 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 18 | #include <lauxlib.h> |
| 19 | #include <lua.h> |
| 20 | #include <lualib.h> |
| 21 | |
Thierry FOURNIER | 463119c | 2015-03-10 00:35:36 +0100 | [diff] [blame] | 22 | #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503 |
| 23 | #error "Requires Lua 5.3 or later." |
Cyril Bonté | dc0306e | 2015-03-02 00:08:40 +0100 | [diff] [blame] | 24 | #endif |
| 25 | |
Willy Tarreau | 8d2b777 | 2020-05-27 10:58:19 +0200 | [diff] [blame] | 26 | #include <import/ebpttree.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 27 | |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 28 | #include <haproxy/api.h> |
| 29 | #include <haproxy/applet.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 30 | #include <haproxy/arg.h> |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 31 | #include <haproxy/auth.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 32 | #include <haproxy/cfgparse.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 33 | #include <haproxy/channel.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 34 | #include <haproxy/cli.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 35 | #include <haproxy/connection.h> |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 36 | #include <haproxy/filters.h> |
Willy Tarreau | 5413a87 | 2020-06-02 19:33:08 +0200 | [diff] [blame] | 37 | #include <haproxy/h1.h> |
Willy Tarreau | 8641605 | 2020-06-04 09:20:54 +0200 | [diff] [blame] | 38 | #include <haproxy/hlua.h> |
Willy Tarreau | 8c79400 | 2020-06-04 10:05:25 +0200 | [diff] [blame] | 39 | #include <haproxy/hlua_fcn.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 40 | #include <haproxy/http_ana.h> |
Willy Tarreau | 126ba3a | 2020-06-04 18:26:43 +0200 | [diff] [blame] | 41 | #include <haproxy/http_fetch.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 42 | #include <haproxy/http_htx.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 43 | #include <haproxy/http_rules.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 44 | #include <haproxy/log.h> |
Willy Tarreau | 2cd5809 | 2020-06-04 15:10:43 +0200 | [diff] [blame] | 45 | #include <haproxy/map.h> |
Willy Tarreau | 8efbdfb | 2020-06-04 11:29:21 +0200 | [diff] [blame] | 46 | #include <haproxy/obj_type.h> |
Willy Tarreau | 225a90a | 2020-06-04 15:06:28 +0200 | [diff] [blame] | 47 | #include <haproxy/pattern.h> |
Willy Tarreau | 469509b | 2020-06-04 15:13:30 +0200 | [diff] [blame] | 48 | #include <haproxy/payload.h> |
Willy Tarreau | 3d6ee40 | 2021-05-08 20:28:07 +0200 | [diff] [blame] | 49 | #include <haproxy/proxy.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 50 | #include <haproxy/regex.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 51 | #include <haproxy/sample.h> |
Willy Tarreau | 198e92a | 2021-03-05 10:23:32 +0100 | [diff] [blame] | 52 | #include <haproxy/server.h> |
Willy Tarreau | 48d25b3 | 2020-06-04 18:58:52 +0200 | [diff] [blame] | 53 | #include <haproxy/session.h> |
Willy Tarreau | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 54 | #include <haproxy/stats-t.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 55 | #include <haproxy/stream.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 56 | #include <haproxy/stream_interface.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 57 | #include <haproxy/task.h> |
Willy Tarreau | 8b550af | 2020-06-04 17:42:48 +0200 | [diff] [blame] | 58 | #include <haproxy/tcp_rules.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 59 | #include <haproxy/thread.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 60 | #include <haproxy/tools.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 61 | #include <haproxy/vars.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 62 | #include <haproxy/xref.h> |
| 63 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 64 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 65 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 66 | * macro is used only for identifying the function that can |
| 67 | * not return because a longjmp is executed. |
| 68 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 69 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 70 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 71 | */ |
| 72 | #define __LJMP |
Willy Tarreau | 4e7cc33 | 2018-10-20 17:45:48 +0200 | [diff] [blame] | 73 | #define WILL_LJMP(func) do { func; my_unreachable(); } while(0) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 74 | #define MAY_LJMP(func) func |
| 75 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 76 | /* This couple of function executes securely some Lua calls outside of |
| 77 | * the lua runtime environment. Each Lua call can return a longjmp |
| 78 | * if it encounter a memory error. |
| 79 | * |
| 80 | * Lua documentation extract: |
| 81 | * |
| 82 | * If an error happens outside any protected environment, Lua calls |
| 83 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 84 | * exiting the host application. Your panic function can avoid this |
| 85 | * exit by never returning (e.g., doing a long jump to your own |
| 86 | * recovery point outside Lua). |
| 87 | * |
| 88 | * The panic function runs as if it were a message handler (see |
| 89 | * §2.3); in particular, the error message is at the top of the |
| 90 | * stack. However, there is no guarantee about stack space. To push |
| 91 | * anything on the stack, the panic function must first check the |
| 92 | * available space (see §4.2). |
| 93 | * |
| 94 | * We must check all the Lua entry point. This includes: |
| 95 | * - The include/proto/hlua.h exported functions |
| 96 | * - the task wrapper function |
| 97 | * - The action wrapper function |
| 98 | * - The converters wrapper function |
| 99 | * - The sample-fetch wrapper functions |
| 100 | * |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 101 | * It is tolerated that the initialisation function returns an abort. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 102 | * Before each Lua abort, an error message is written on stderr. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 103 | * |
| 104 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 105 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 106 | * because they must be exists in the program stack when the longjmp |
| 107 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 108 | * |
| 109 | * Note that the Lua processing is not really thread safe. It provides |
| 110 | * heavy system which consists to add our own lock function in the Lua |
| 111 | * code and recompile the library. This system will probably not accepted |
| 112 | * by maintainers of various distribs. |
| 113 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 114 | * 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] | 115 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 116 | * of function and a lua_unlock() at the end of the function. So I |
| 117 | * conclude that the Lua thread safe mode just perform a mutex around |
| 118 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 119 | * easier for distro maintainers. |
| 120 | * |
| 121 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 122 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 123 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 124 | */ |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 125 | __decl_spinlock(hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 126 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 127 | static int hlua_panic_safe(lua_State *L) { return 0; } |
Willy Tarreau | 6a51090 | 2021-07-14 19:41:25 +0200 | [diff] [blame] | 128 | 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] | 129 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 130 | /* This is the chained list of struct hlua_function referenced |
| 131 | * for haproxy action, sample-fetches, converters, cli and |
| 132 | * applet bindings. It is used for a post-initialisation control. |
| 133 | */ |
| 134 | static struct list referenced_functions = LIST_HEAD_INIT(referenced_functions); |
| 135 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 136 | /* This variable is used only during initialization to identify the Lua state |
| 137 | * currently being initialized. 0 is the common lua state, 1 to n are the Lua |
| 138 | * states dedicated to each thread (in this case hlua_state_id==tid+1). |
| 139 | */ |
| 140 | static int hlua_state_id; |
| 141 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 142 | /* This is a NULL-terminated list of lua file which are referenced to load per thread */ |
| 143 | static char **per_thread_load = NULL; |
| 144 | |
| 145 | lua_State *hlua_init_state(int thread_id); |
| 146 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 147 | #define SET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 148 | ({ \ |
| 149 | int ret; \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 150 | if ((__HLUA)->state_id == 0) \ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 151 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 152 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 153 | lua_atpanic(__L, hlua_panic_safe); \ |
| 154 | ret = 0; \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 155 | if ((__HLUA)->state_id == 0) \ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 156 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 157 | } else { \ |
| 158 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 159 | ret = 1; \ |
| 160 | } \ |
| 161 | ret; \ |
| 162 | }) |
| 163 | |
| 164 | /* If we are the last function catching Lua errors, we |
| 165 | * must reset the panic function. |
| 166 | */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 167 | #define RESET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 168 | do { \ |
| 169 | lua_atpanic(__L, hlua_panic_safe); \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 170 | if ((__HLUA)->state_id == 0) \ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 171 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 172 | } while(0) |
| 173 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 174 | #define SET_SAFE_LJMP(__HLUA) \ |
| 175 | SET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 176 | |
| 177 | #define RESET_SAFE_LJMP(__HLUA) \ |
| 178 | RESET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 179 | |
| 180 | #define SET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 181 | SET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 182 | |
| 183 | #define RESET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 184 | RESET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 185 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 186 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 187 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 188 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 189 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 190 | /* unused: 0x08, 0x10 */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 191 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 192 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 193 | |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 194 | /* The main Lua execution context. The 0 index is the |
| 195 | * common state shared by all threads. |
| 196 | */ |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 197 | static lua_State *hlua_states[MAX_THREADS + 1]; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 198 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 199 | #define HLUA_FLT_CB_FINAL 0x00000001 |
| 200 | #define HLUA_FLT_CB_RETVAL 0x00000002 |
| 201 | #define HLUA_FLT_CB_ARG_CHN 0x00000004 |
| 202 | #define HLUA_FLT_CB_ARG_HTTP_MSG 0x00000008 |
| 203 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 204 | #define HLUA_FLT_CTX_FL_PAYLOAD 0x00000001 |
| 205 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 206 | struct hlua_reg_filter { |
| 207 | char *name; |
| 208 | int flt_ref[MAX_THREADS + 1]; |
| 209 | int fun_ref[MAX_THREADS + 1]; |
| 210 | struct list l; |
| 211 | }; |
| 212 | |
| 213 | struct hlua_flt_config { |
| 214 | struct hlua_reg_filter *reg; |
| 215 | int ref[MAX_THREADS + 1]; |
| 216 | char **args; |
| 217 | }; |
| 218 | |
| 219 | struct hlua_flt_ctx { |
| 220 | int ref; /* ref to the filter lua object */ |
| 221 | struct hlua *hlua[2]; /* lua runtime context (0: request, 1: response) */ |
| 222 | unsigned int cur_off[2]; /* current offset (0: request, 1: response) */ |
| 223 | unsigned int cur_len[2]; /* current forwardable length (0: request, 1: response) */ |
| 224 | unsigned int flags; /* HLUA_FLT_CTX_FL_* */ |
| 225 | }; |
| 226 | |
| 227 | DECLARE_STATIC_POOL(pool_head_hlua_flt_ctx, "hlua_flt_ctx", sizeof(struct hlua_flt_ctx)); |
| 228 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 229 | static int hlua_filter_from_payload(struct filter *filter); |
| 230 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 231 | /* This is the chained list of struct hlua_flt referenced |
| 232 | * for haproxy filters. It is used for a post-initialisation control. |
| 233 | */ |
| 234 | static struct list referenced_filters = LIST_HEAD_INIT(referenced_filters); |
| 235 | |
| 236 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 237 | /* This is the memory pool containing struct lua for applets |
| 238 | * (including cli). |
| 239 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 240 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 241 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 242 | /* Used for Socket connection. */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 243 | static struct proxy *socket_proxy; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 244 | static struct server *socket_tcp; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 245 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 246 | static struct server *socket_ssl; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 247 | #endif |
| 248 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 249 | /* List head of the function called at the initialisation time. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 250 | struct list hlua_init_functions[MAX_THREADS + 1]; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 251 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 252 | /* The following variables contains the reference of the different |
| 253 | * Lua classes. These references are useful for identify metadata |
| 254 | * associated with an object. |
| 255 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 256 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 257 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 258 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 259 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 260 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 261 | static int class_http_ref; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 262 | static int class_http_msg_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 263 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 264 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 265 | static int class_applet_http_ref; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 266 | static int class_txn_reply_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 267 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 268 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 269 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 270 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 271 | * because a task may remain alive during all the haproxy execution. |
| 272 | */ |
| 273 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 274 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 275 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 276 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 277 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 278 | * it is used for preventing infinite loops. |
| 279 | * |
| 280 | * I test the scheer with an infinite loop containing one incrementation |
| 281 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 282 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 283 | * to one interrupt each 10 000 instructions. |
| 284 | * |
| 285 | * configured | Number of |
| 286 | * instructions | loops executed |
| 287 | * between two | in milions |
| 288 | * forced yields | |
| 289 | * ---------------+--------------- |
| 290 | * 10 | 160 |
| 291 | * 500 | 670 |
| 292 | * 1000 | 680 |
| 293 | * 5000 | 700 |
| 294 | * 7000 | 700 |
| 295 | * 8000 | 700 |
| 296 | * 9000 | 710 <- ceil |
| 297 | * 10000 | 710 |
| 298 | * 100000 | 710 |
| 299 | * 1000000 | 710 |
| 300 | * |
| 301 | */ |
| 302 | static unsigned int hlua_nb_instruction = 10000; |
| 303 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 304 | /* Descriptor for the memory allocation state. The limit is pre-initialised to |
| 305 | * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it |
| 306 | * is replaced with ~0 during post_init after everything was loaded. This way |
| 307 | * it is guaranteed that if limit is ~0 the boot is complete and that if it's |
| 308 | * zero it's not yet limited and proper accounting is required. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 309 | */ |
| 310 | struct hlua_mem_allocator { |
| 311 | size_t allocated; |
| 312 | size_t limit; |
| 313 | }; |
| 314 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 315 | static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 316 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 317 | /* These functions converts types between HAProxy internal args or |
| 318 | * sample and LUA types. Another function permits to check if the |
| 319 | * LUA stack contains arguments according with an required ARG_T |
| 320 | * format. |
| 321 | */ |
| 322 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 323 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 324 | __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] | 325 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 326 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 327 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 328 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 329 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 330 | __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] | 331 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 332 | struct prepend_path { |
| 333 | struct list l; |
| 334 | char *type; |
| 335 | char *path; |
| 336 | }; |
| 337 | |
| 338 | static struct list prepend_path_list = LIST_HEAD_INIT(prepend_path_list); |
| 339 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 340 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 341 | do { \ |
| 342 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 343 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 344 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 345 | } while (0) |
| 346 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 347 | static inline struct hlua_function *new_hlua_function() |
| 348 | { |
| 349 | struct hlua_function *fcn; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 350 | int i; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 351 | |
| 352 | fcn = calloc(1, sizeof(*fcn)); |
| 353 | if (!fcn) |
| 354 | return NULL; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 355 | LIST_APPEND(&referenced_functions, &fcn->l); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 356 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 357 | fcn->function_ref[i] = -1; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 358 | return fcn; |
| 359 | } |
| 360 | |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 361 | static inline void release_hlua_function(struct hlua_function *fcn) |
| 362 | { |
| 363 | if (!fcn) |
| 364 | return; |
| 365 | if (fcn->name) |
| 366 | ha_free(&fcn->name); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 367 | LIST_DELETE(&fcn->l); |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 368 | ha_free(&fcn); |
| 369 | } |
| 370 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 371 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 372 | static inline int fcn_ref_to_stack_id(struct hlua_function *fcn) |
| 373 | { |
| 374 | if (fcn->function_ref[0] == -1) |
| 375 | return tid + 1; |
| 376 | return 0; |
| 377 | } |
| 378 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 379 | /* Create a new registered filter. Only its name is filled */ |
| 380 | static inline struct hlua_reg_filter *new_hlua_reg_filter(const char *name) |
| 381 | { |
| 382 | struct hlua_reg_filter *reg_flt; |
| 383 | int i; |
| 384 | |
| 385 | reg_flt = calloc(1, sizeof(*reg_flt)); |
| 386 | if (!reg_flt) |
| 387 | return NULL; |
| 388 | reg_flt->name = strdup(name); |
| 389 | if (!reg_flt->name) { |
| 390 | free(reg_flt); |
| 391 | return NULL; |
| 392 | } |
| 393 | LIST_APPEND(&referenced_filters, ®_flt->l); |
| 394 | for (i = 0; i < MAX_THREADS + 1; i++) { |
| 395 | reg_flt->flt_ref[i] = -1; |
| 396 | reg_flt->fun_ref[i] = -1; |
| 397 | } |
| 398 | return reg_flt; |
| 399 | } |
| 400 | |
| 401 | /* Release a registered filter */ |
| 402 | static inline void release_hlua_reg_filter(struct hlua_reg_filter *reg_flt) |
| 403 | { |
| 404 | if (!reg_flt) |
| 405 | return; |
| 406 | if (reg_flt->name) |
| 407 | ha_free(®_flt->name); |
| 408 | LIST_DELETE(®_flt->l); |
| 409 | ha_free(®_flt); |
| 410 | } |
| 411 | |
| 412 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 413 | static inline int reg_flt_to_stack_id(struct hlua_reg_filter *reg_flt) |
| 414 | { |
| 415 | if (reg_flt->fun_ref[0] == -1) |
| 416 | return tid + 1; |
| 417 | return 0; |
| 418 | } |
| 419 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 420 | /* Used to check an Lua function type in the stack. It creates and |
| 421 | * returns a reference of the function. This function throws an |
| 422 | * error if the rgument is not a "function". |
| 423 | */ |
| 424 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 425 | { |
| 426 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 427 | 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] | 428 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 429 | } |
| 430 | lua_pushvalue(L, argno); |
| 431 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 432 | } |
| 433 | |
Christopher Faulet | ba9e21d | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 434 | /* Used to check an Lua table type in the stack. It creates and |
| 435 | * returns a reference of the table. This function throws an |
| 436 | * error if the rgument is not a "table". |
| 437 | */ |
| 438 | __LJMP unsigned int hlua_checktable(lua_State *L, int argno) |
| 439 | { |
| 440 | if (!lua_istable(L, argno)) { |
| 441 | const char *msg = lua_pushfstring(L, "table expected, got %s", luaL_typename(L, argno)); |
| 442 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 443 | } |
| 444 | lua_pushvalue(L, argno); |
| 445 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 446 | } |
| 447 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 448 | /* Return the string that is of the top of the stack. */ |
| 449 | const char *hlua_get_top_error_string(lua_State *L) |
| 450 | { |
| 451 | if (lua_gettop(L) < 1) |
| 452 | return "unknown error"; |
| 453 | if (lua_type(L, -1) != LUA_TSTRING) |
| 454 | return "unknown error"; |
| 455 | return lua_tostring(L, -1); |
| 456 | } |
| 457 | |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 458 | __LJMP const char *hlua_traceback(lua_State *L, const char* sep) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 459 | { |
| 460 | lua_Debug ar; |
| 461 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 462 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 463 | |
| 464 | while (lua_getstack(L, level++, &ar)) { |
| 465 | |
| 466 | /* Add separator */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 467 | if (b_data(msg)) |
| 468 | chunk_appendf(msg, "%s", sep); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 469 | |
| 470 | /* Fill fields: |
| 471 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 472 | * 'l': fills in the field currentline; |
| 473 | * 'n': fills in the field name and namewhat; |
| 474 | * 't': fills in the field istailcall; |
| 475 | */ |
| 476 | lua_getinfo(L, "Slnt", &ar); |
| 477 | |
| 478 | /* Append code localisation */ |
| 479 | if (ar.currentline > 0) |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 480 | chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 481 | else |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 482 | chunk_appendf(msg, "%s: ", ar.short_src); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 483 | |
| 484 | /* |
| 485 | * Get function name |
| 486 | * |
| 487 | * if namewhat is no empty, name is defined. |
| 488 | * what contains "Lua" for Lua function, "C" for C function, |
| 489 | * or "main" for main code. |
| 490 | */ |
| 491 | 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] | 492 | chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */ |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 493 | |
| 494 | 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] | 495 | chunk_appendf(msg, "in main chunk"); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 496 | |
| 497 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 498 | chunk_appendf(msg, "in function line %d", ar.linedefined); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 499 | |
| 500 | else /* nothing left... */ |
| 501 | chunk_appendf(msg, "?"); |
| 502 | |
| 503 | |
| 504 | /* Display tailed call */ |
| 505 | if (ar.istailcall) |
| 506 | chunk_appendf(msg, " ..."); |
| 507 | } |
| 508 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 509 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 513 | /* This function check the number of arguments available in the |
| 514 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 515 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 516 | */ |
| 517 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 518 | { |
| 519 | if (lua_gettop(L) == nb) |
| 520 | return; |
| 521 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 522 | } |
| 523 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 524 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 525 | * and the line number where the error is encountered. |
| 526 | */ |
| 527 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 528 | { |
| 529 | va_list argp; |
| 530 | va_start(argp, fmt); |
| 531 | luaL_where(L, 1); |
| 532 | lua_pushvfstring(L, fmt, argp); |
| 533 | va_end(argp); |
| 534 | lua_concat(L, 2); |
| 535 | return 1; |
| 536 | } |
| 537 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 538 | /* This functions is used with sample fetch and converters. It |
| 539 | * converts the HAProxy configuration argument in a lua stack |
| 540 | * values. |
| 541 | * |
| 542 | * It takes an array of "arg", and each entry of the array is |
| 543 | * converted and pushed in the LUA stack. |
| 544 | */ |
| 545 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 546 | { |
| 547 | switch (arg->type) { |
| 548 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 549 | case ARGT_TIME: |
| 550 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 551 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 552 | break; |
| 553 | |
| 554 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 555 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 556 | break; |
| 557 | |
| 558 | case ARGT_IPV4: |
| 559 | case ARGT_IPV6: |
| 560 | case ARGT_MSK4: |
| 561 | case ARGT_MSK6: |
| 562 | case ARGT_FE: |
| 563 | case ARGT_BE: |
| 564 | case ARGT_TAB: |
| 565 | case ARGT_SRV: |
| 566 | case ARGT_USR: |
| 567 | case ARGT_MAP: |
| 568 | default: |
| 569 | lua_pushnil(L); |
| 570 | break; |
| 571 | } |
| 572 | return 1; |
| 573 | } |
| 574 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 575 | /* 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] | 576 | * 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] | 577 | * with sample fetch wrappers. The input arguments are given to the |
| 578 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 579 | */ |
| 580 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 581 | { |
| 582 | switch (lua_type(L, ud)) { |
| 583 | |
| 584 | case LUA_TNUMBER: |
| 585 | case LUA_TBOOLEAN: |
| 586 | arg->type = ARGT_SINT; |
| 587 | arg->data.sint = lua_tointeger(L, ud); |
| 588 | break; |
| 589 | |
| 590 | case LUA_TSTRING: |
| 591 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 592 | 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] | 593 | /* 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] | 594 | 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] | 595 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 596 | break; |
| 597 | |
| 598 | case LUA_TUSERDATA: |
| 599 | case LUA_TNIL: |
| 600 | case LUA_TTABLE: |
| 601 | case LUA_TFUNCTION: |
| 602 | case LUA_TTHREAD: |
| 603 | case LUA_TLIGHTUSERDATA: |
| 604 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 605 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 606 | break; |
| 607 | } |
| 608 | return 1; |
| 609 | } |
| 610 | |
| 611 | /* the following functions are used to convert a struct sample |
| 612 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 613 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 614 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 615 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 616 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 617 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 618 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 619 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 620 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 621 | break; |
| 622 | |
| 623 | case SMP_T_BIN: |
| 624 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 625 | 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] | 626 | break; |
| 627 | |
| 628 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 629 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 630 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 631 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 632 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 633 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 634 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 635 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 636 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 637 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 638 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 639 | 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] | 640 | break; |
| 641 | default: |
| 642 | lua_pushnil(L); |
| 643 | break; |
| 644 | } |
| 645 | break; |
| 646 | |
| 647 | case SMP_T_IPV4: |
| 648 | case SMP_T_IPV6: |
| 649 | 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] | 650 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 651 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 652 | 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] | 653 | else |
| 654 | lua_pushnil(L); |
| 655 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 656 | default: |
| 657 | lua_pushnil(L); |
| 658 | break; |
| 659 | } |
| 660 | return 1; |
| 661 | } |
| 662 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 663 | /* the following functions are used to convert a struct sample |
| 664 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 665 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 666 | */ |
| 667 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 668 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 669 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 670 | |
| 671 | case SMP_T_BIN: |
| 672 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 673 | 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] | 674 | break; |
| 675 | |
| 676 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 677 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 678 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 679 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 680 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 681 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 682 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 683 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 684 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 685 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 686 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 687 | 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] | 688 | break; |
| 689 | default: |
| 690 | lua_pushstring(L, ""); |
| 691 | break; |
| 692 | } |
| 693 | break; |
| 694 | |
| 695 | case SMP_T_SINT: |
| 696 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 697 | case SMP_T_IPV4: |
| 698 | case SMP_T_IPV6: |
| 699 | 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] | 700 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 701 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 702 | 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] | 703 | else |
| 704 | lua_pushstring(L, ""); |
| 705 | break; |
| 706 | default: |
| 707 | lua_pushstring(L, ""); |
| 708 | break; |
| 709 | } |
| 710 | return 1; |
| 711 | } |
| 712 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 713 | /* the following functions are used to convert an Lua type in a |
| 714 | * struct sample. This is useful to provide data from a converter |
| 715 | * to the LUA code. |
| 716 | */ |
| 717 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 718 | { |
| 719 | switch (lua_type(L, ud)) { |
| 720 | |
| 721 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 722 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 723 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 724 | break; |
| 725 | |
| 726 | |
| 727 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 728 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 729 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 730 | break; |
| 731 | |
| 732 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 733 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 734 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 735 | 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] | 736 | /* 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] | 737 | 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] | 738 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 739 | break; |
| 740 | |
| 741 | case LUA_TUSERDATA: |
| 742 | case LUA_TNIL: |
| 743 | case LUA_TTABLE: |
| 744 | case LUA_TFUNCTION: |
| 745 | case LUA_TTHREAD: |
| 746 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 747 | case LUA_TNONE: |
| 748 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 749 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 750 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 751 | break; |
| 752 | } |
| 753 | return 1; |
| 754 | } |
| 755 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 756 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 757 | * 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] | 758 | * 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] | 759 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 760 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 761 | * 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] | 762 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 763 | __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] | 764 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 765 | { |
| 766 | int min_arg; |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 767 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 768 | struct proxy *px; |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 769 | struct userlist *ul; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 770 | struct my_regex *reg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 771 | const char *msg = NULL; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 772 | char *sname, *pname, *err = NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 773 | |
| 774 | idx = 0; |
| 775 | min_arg = ARGM(mask); |
| 776 | mask >>= ARGM_BITS; |
| 777 | |
| 778 | while (1) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 779 | struct buffer tmp = BUF_NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 780 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 781 | /* Check for mandatory arguments. */ |
| 782 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 783 | if (idx < min_arg) { |
| 784 | |
| 785 | /* If miss other argument than the first one, we return an error. */ |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 786 | if (idx > 0) { |
| 787 | msg = "Mandatory argument expected"; |
| 788 | goto error; |
| 789 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 790 | |
| 791 | /* If first argument have a certain type, some default values |
| 792 | * may be used. See the function smp_resolve_args(). |
| 793 | */ |
| 794 | switch (mask & ARGT_MASK) { |
| 795 | |
| 796 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 797 | if (!(p->cap & PR_CAP_FE)) { |
| 798 | msg = "Mandatory argument expected"; |
| 799 | goto error; |
| 800 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 801 | argp[idx].data.prx = p; |
| 802 | argp[idx].type = ARGT_FE; |
| 803 | argp[idx+1].type = ARGT_STOP; |
| 804 | break; |
| 805 | |
| 806 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 807 | if (!(p->cap & PR_CAP_BE)) { |
| 808 | msg = "Mandatory argument expected"; |
| 809 | goto error; |
| 810 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 811 | argp[idx].data.prx = p; |
| 812 | argp[idx].type = ARGT_BE; |
| 813 | argp[idx+1].type = ARGT_STOP; |
| 814 | break; |
| 815 | |
| 816 | case ARGT_TAB: |
| 817 | argp[idx].data.prx = p; |
| 818 | argp[idx].type = ARGT_TAB; |
| 819 | argp[idx+1].type = ARGT_STOP; |
| 820 | break; |
| 821 | |
| 822 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 823 | msg = "Mandatory argument expected"; |
| 824 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 825 | break; |
| 826 | } |
| 827 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 828 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 829 | } |
| 830 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 831 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 832 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 833 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 834 | msg = "Last argument expected"; |
| 835 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 839 | argp[idx].type == ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 840 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 841 | } |
| 842 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 843 | /* Convert some argument types. All string in argp[] are for not |
| 844 | * duplicated yet. |
| 845 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 846 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 847 | case ARGT_SINT: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 848 | if (argp[idx].type != ARGT_SINT) { |
| 849 | msg = "integer expected"; |
| 850 | goto error; |
| 851 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 852 | argp[idx].type = ARGT_SINT; |
| 853 | break; |
| 854 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 855 | case ARGT_TIME: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 856 | if (argp[idx].type != ARGT_SINT) { |
| 857 | msg = "integer expected"; |
| 858 | goto error; |
| 859 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 860 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 861 | break; |
| 862 | |
| 863 | case ARGT_SIZE: |
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 | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 868 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 869 | break; |
| 870 | |
| 871 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 872 | if (argp[idx].type != ARGT_STR) { |
| 873 | msg = "string expected"; |
| 874 | goto error; |
| 875 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 876 | 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] | 877 | if (!argp[idx].data.prx) { |
| 878 | msg = "frontend doesn't exist"; |
| 879 | goto error; |
| 880 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 881 | argp[idx].type = ARGT_FE; |
| 882 | break; |
| 883 | |
| 884 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 885 | if (argp[idx].type != ARGT_STR) { |
| 886 | msg = "string expected"; |
| 887 | goto error; |
| 888 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 889 | 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] | 890 | if (!argp[idx].data.prx) { |
| 891 | msg = "backend doesn't exist"; |
| 892 | goto error; |
| 893 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 894 | argp[idx].type = ARGT_BE; |
| 895 | break; |
| 896 | |
| 897 | case ARGT_TAB: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 898 | if (argp[idx].type != ARGT_STR) { |
| 899 | msg = "string expected"; |
| 900 | goto error; |
| 901 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 902 | 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] | 903 | if (!argp[idx].data.t) { |
| 904 | msg = "table doesn't exist"; |
| 905 | goto error; |
| 906 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 907 | argp[idx].type = ARGT_TAB; |
| 908 | break; |
| 909 | |
| 910 | case ARGT_SRV: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 911 | if (argp[idx].type != ARGT_STR) { |
| 912 | msg = "string expected"; |
| 913 | goto error; |
| 914 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 915 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 916 | if (sname) { |
| 917 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 918 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 919 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 920 | if (!px) { |
| 921 | msg = "backend doesn't exist"; |
| 922 | goto error; |
| 923 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 924 | } |
| 925 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 926 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 927 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 928 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 929 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 930 | if (!argp[idx].data.srv) { |
| 931 | msg = "server doesn't exist"; |
| 932 | goto error; |
| 933 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 934 | argp[idx].type = ARGT_SRV; |
| 935 | break; |
| 936 | |
| 937 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 938 | if (argp[idx].type != ARGT_STR) { |
| 939 | msg = "string expected"; |
| 940 | goto error; |
| 941 | } |
| 942 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 943 | msg = "invalid IPv4 address"; |
| 944 | goto error; |
| 945 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 946 | argp[idx].type = ARGT_IPV4; |
| 947 | break; |
| 948 | |
| 949 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 950 | if (argp[idx].type == ARGT_SINT) |
| 951 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 952 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 953 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 954 | msg = "invalid IPv4 mask"; |
| 955 | goto error; |
| 956 | } |
| 957 | } |
| 958 | else { |
| 959 | msg = "integer or string expected"; |
| 960 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 961 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 962 | argp[idx].type = ARGT_MSK4; |
| 963 | break; |
| 964 | |
| 965 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 966 | if (argp[idx].type != ARGT_STR) { |
| 967 | msg = "string expected"; |
| 968 | goto error; |
| 969 | } |
| 970 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 971 | msg = "invalid IPv6 address"; |
| 972 | goto error; |
| 973 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 974 | argp[idx].type = ARGT_IPV6; |
| 975 | break; |
| 976 | |
| 977 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 978 | if (argp[idx].type == ARGT_SINT) |
| 979 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 980 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 981 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 982 | msg = "invalid IPv6 mask"; |
| 983 | goto error; |
| 984 | } |
| 985 | } |
| 986 | else { |
| 987 | msg = "integer or string expected"; |
| 988 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 989 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 990 | argp[idx].type = ARGT_MSK6; |
| 991 | break; |
| 992 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 993 | case ARGT_REG: |
| 994 | if (argp[idx].type != ARGT_STR) { |
| 995 | msg = "string expected"; |
| 996 | goto error; |
| 997 | } |
| 998 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 999 | if (!reg) { |
| 1000 | msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'", |
| 1001 | argp[idx].data.str.area, err); |
| 1002 | free(err); |
| 1003 | goto error; |
| 1004 | } |
| 1005 | argp[idx].type = ARGT_REG; |
| 1006 | argp[idx].data.reg = reg; |
| 1007 | break; |
| 1008 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1009 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1010 | if (argp[idx].type != ARGT_STR) { |
| 1011 | msg = "string expected"; |
| 1012 | goto error; |
| 1013 | } |
| 1014 | if (p->uri_auth && p->uri_auth->userlist && |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1015 | strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0) |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1016 | ul = p->uri_auth->userlist; |
| 1017 | else |
| 1018 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 1019 | |
| 1020 | if (!ul) { |
| 1021 | msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area); |
| 1022 | goto error; |
| 1023 | } |
| 1024 | argp[idx].type = ARGT_USR; |
| 1025 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1026 | break; |
| 1027 | |
| 1028 | case ARGT_STR: |
| 1029 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 1030 | msg = "unable to duplicate string arg"; |
| 1031 | goto error; |
| 1032 | } |
| 1033 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1034 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1035 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1036 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1037 | msg = "type not yet supported"; |
| 1038 | goto error; |
| 1039 | break; |
| 1040 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | /* Check for type of argument. */ |
| 1044 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1045 | msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 1046 | arg_type_names[(mask & ARGT_MASK)], |
| 1047 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 1048 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | /* Next argument. */ |
| 1052 | mask >>= ARGT_BITS; |
| 1053 | idx++; |
| 1054 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1055 | return 0; |
| 1056 | |
| 1057 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 1058 | free_args(argp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1059 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 1060 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1061 | } |
| 1062 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1063 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1064 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1065 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1066 | * |
| 1067 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1068 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 1069 | */ |
| 1070 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 1071 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1072 | struct hlua **hlua = lua_getextraspace(L); |
| 1073 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1074 | } |
| 1075 | static inline void hlua_sethlua(struct hlua *hlua) |
| 1076 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1077 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 1078 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1079 | } |
| 1080 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1081 | /* This function is used to send logs. It try to send on screen (stderr) |
| 1082 | * and on the default syslog server. |
| 1083 | */ |
| 1084 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 1085 | { |
| 1086 | struct tm tm; |
| 1087 | char *p; |
| 1088 | |
| 1089 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1090 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1091 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1092 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 1093 | /* Break the message if exceed the buffer size. */ |
| 1094 | *(p-4) = ' '; |
| 1095 | *(p-3) = '.'; |
| 1096 | *(p-2) = '.'; |
| 1097 | *(p-1) = '.'; |
| 1098 | break; |
| 1099 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1100 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1101 | *p = *msg; |
| 1102 | else |
| 1103 | *p = '.'; |
| 1104 | } |
| 1105 | *p = '\0'; |
| 1106 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1107 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1108 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 1109 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 1110 | return; |
| 1111 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 1112 | get_localtime(date.tv_sec, &tm); |
| 1113 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1114 | 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] | 1115 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1116 | fflush(stderr); |
| 1117 | } |
| 1118 | } |
| 1119 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1120 | /* This function just ensure that the yield will be always |
| 1121 | * returned with a timeout and permit to set some flags |
| 1122 | */ |
| 1123 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1124 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1125 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1126 | struct hlua *hlua; |
| 1127 | |
| 1128 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1129 | hlua = hlua_gethlua(L); |
| 1130 | if (!hlua) { |
| 1131 | return; |
| 1132 | } |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1133 | |
| 1134 | /* Set the wake timeout. If timeout is required, we set |
| 1135 | * the expiration time. |
| 1136 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1137 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1138 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1139 | hlua->flags |= flags; |
| 1140 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1141 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1142 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1143 | } |
| 1144 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1145 | /* This function initialises the Lua environment stored in the stream. |
| 1146 | * 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] | 1147 | * 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] | 1148 | * |
| 1149 | * This function is particular. it initialises a new Lua thread. If the |
| 1150 | * initialisation fails (example: out of memory error), the lua function |
| 1151 | * throws an error (longjmp). |
| 1152 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1153 | * 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] | 1154 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1155 | * threads appear, the safe environment set a lock to ensure only one |
| 1156 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1157 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1158 | * |
| 1159 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1160 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1161 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1162 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1163 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1164 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1165 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1166 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1167 | 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] | 1168 | { |
| 1169 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1170 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1171 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1172 | lua->wake_time = TICK_ETERNITY; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1173 | lua->state_id = state_id; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1174 | LIST_INIT(&lua->com); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1175 | if (!already_safe) { |
| 1176 | if (!SET_SAFE_LJMP_PARENT(lua)) { |
| 1177 | lua->Tref = LUA_REFNIL; |
| 1178 | return 0; |
| 1179 | } |
| 1180 | } |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1181 | lua->T = lua_newthread(hlua_states[state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1182 | if (!lua->T) { |
| 1183 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1184 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1185 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1186 | return 0; |
| 1187 | } |
| 1188 | hlua_sethlua(lua); |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1189 | lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1190 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1191 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1192 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1193 | return 1; |
| 1194 | } |
| 1195 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1196 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1197 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 1198 | * is not freed. |
| 1199 | */ |
| 1200 | void hlua_ctx_destroy(struct hlua *lua) |
| 1201 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1202 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1203 | return; |
| 1204 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1205 | if (!lua->T) |
| 1206 | goto end; |
| 1207 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1208 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1209 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1210 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1211 | if (!SET_SAFE_LJMP(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1212 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1213 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1214 | RESET_SAFE_LJMP(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1215 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1216 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1217 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1218 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1219 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1220 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1221 | * without error, we run the GC on the thread pointer. Its freed all |
| 1222 | * the unused memory. |
| 1223 | * If the thread is finnish with an error or is currently yielded, |
| 1224 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1225 | * so e run the GC on the main thread. |
| 1226 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1227 | * the garbage collection. |
| 1228 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1229 | if (lua->gc_count) { |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1230 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1231 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1232 | lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1233 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1234 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1235 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1236 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1237 | |
| 1238 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1239 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | /* This function is used to restore the Lua context when a coroutine |
| 1243 | * fails. This function copy the common memory between old coroutine |
| 1244 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1245 | * replaced by the new coroutine. |
| 1246 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1247 | * as string error message and it is copied in the new stack. |
| 1248 | */ |
| 1249 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1250 | { |
| 1251 | lua_State *T; |
| 1252 | int new_ref; |
| 1253 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1254 | /* New Lua coroutine. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1255 | T = lua_newthread(hlua_states[lua->state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1256 | if (!T) |
| 1257 | return 0; |
| 1258 | |
| 1259 | /* Copy last error message. */ |
| 1260 | if (keep_msg) |
| 1261 | lua_xmove(lua->T, T, 1); |
| 1262 | |
| 1263 | /* Copy data between the coroutines. */ |
| 1264 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1265 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1266 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1267 | |
| 1268 | /* Destroy old data. */ |
| 1269 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1270 | |
| 1271 | /* The thread is garbage collected by Lua. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1272 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1273 | |
| 1274 | /* Fill the struct with the new coroutine values. */ |
| 1275 | lua->Mref = new_ref; |
| 1276 | lua->T = T; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1277 | lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1278 | |
| 1279 | /* Set context. */ |
| 1280 | hlua_sethlua(lua); |
| 1281 | |
| 1282 | return 1; |
| 1283 | } |
| 1284 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1285 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1286 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1287 | struct hlua *hlua; |
| 1288 | |
| 1289 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1290 | hlua = hlua_gethlua(L); |
| 1291 | if (!hlua) |
| 1292 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1293 | |
| 1294 | /* Lua cannot yield when its returning from a function, |
| 1295 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1296 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1297 | */ |
| 1298 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1299 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1300 | return; |
| 1301 | } |
| 1302 | |
| 1303 | /* restore the interrupt condition. */ |
| 1304 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1305 | |
| 1306 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1307 | * If the state is not yieldable, trying yield causes an error. |
| 1308 | */ |
| 1309 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1310 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1311 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1312 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1313 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1314 | hlua->run_time += now_ms - hlua->start_time; |
| 1315 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1316 | lua_pushfstring(L, "execution timeout"); |
| 1317 | WILL_LJMP(lua_error(L)); |
| 1318 | } |
| 1319 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1320 | /* Update the start time. */ |
| 1321 | hlua->start_time = now_ms; |
| 1322 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1323 | /* Try to interrupt the process at the end of the current |
| 1324 | * unyieldable function. |
| 1325 | */ |
| 1326 | 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] | 1327 | } |
| 1328 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1329 | /* This function start or resumes the Lua stack execution. If the flag |
| 1330 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1331 | * The function return an error. |
| 1332 | * |
| 1333 | * The function can returns 4 values: |
| 1334 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1335 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1336 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1337 | * - 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] | 1338 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1339 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1340 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1341 | * 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] | 1342 | * LUA code. |
| 1343 | */ |
| 1344 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1345 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1346 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1347 | int nres; |
| 1348 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1349 | int ret; |
| 1350 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1351 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1352 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1353 | /* Initialise run time counter. */ |
| 1354 | if (!HLUA_IS_RUNNING(lua)) |
| 1355 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1356 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1357 | /* Lock the whole Lua execution. This lock must be before the |
| 1358 | * label "resume_execution". |
| 1359 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1360 | if (lua->state_id == 0) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1361 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1362 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1363 | resume_execution: |
| 1364 | |
| 1365 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1366 | * instructions. it is used for preventing infinite loops. |
| 1367 | */ |
| 1368 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1369 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1370 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1371 | HLUA_SET_RUN(lua); |
| 1372 | HLUA_CLR_CTRLYIELD(lua); |
| 1373 | HLUA_CLR_WAKERESWR(lua); |
| 1374 | HLUA_CLR_WAKEREQWR(lua); |
Christopher Faulet | 1f43a34 | 2021-08-04 17:58:21 +0200 | [diff] [blame] | 1375 | HLUA_CLR_NOYIELD(lua); |
| 1376 | if (!yield_allowed) |
| 1377 | HLUA_SET_NOYIELD(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1378 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1379 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1380 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1381 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1382 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1383 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1384 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1385 | 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] | 1386 | #else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1387 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1388 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1389 | switch (ret) { |
| 1390 | |
| 1391 | case LUA_OK: |
| 1392 | ret = HLUA_E_OK; |
| 1393 | break; |
| 1394 | |
| 1395 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1396 | /* Check if the execution timeout is expired. It it is the case, we |
| 1397 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1398 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1399 | tv_update_date(0, 1); |
| 1400 | lua->run_time += now_ms - lua->start_time; |
| 1401 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1402 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1403 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1404 | break; |
| 1405 | } |
| 1406 | /* Process the forced yield. if the general yield is not allowed or |
| 1407 | * if no task were associated this the current Lua execution |
| 1408 | * coroutine, we resume the execution. Else we want to return in the |
| 1409 | * scheduler and we want to be waked up again, to continue the |
| 1410 | * current Lua execution. So we schedule our own task. |
| 1411 | */ |
| 1412 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1413 | if (!yield_allowed || !lua->task) |
| 1414 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1415 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1416 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1417 | if (!yield_allowed) { |
| 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_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1420 | break; |
| 1421 | } |
| 1422 | ret = HLUA_E_AGAIN; |
| 1423 | break; |
| 1424 | |
| 1425 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1426 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1427 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1428 | * because the errors ares the only one mean to return immediately |
| 1429 | * from and lua execution. |
| 1430 | */ |
| 1431 | if (lua->flags & HLUA_EXIT) { |
| 1432 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1433 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1434 | break; |
| 1435 | } |
| 1436 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1437 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1438 | if (!lua_checkstack(lua->T, 1)) { |
| 1439 | ret = HLUA_E_ERR; |
| 1440 | break; |
| 1441 | } |
| 1442 | msg = lua_tostring(lua->T, -1); |
| 1443 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1444 | lua_pop(lua->T, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 1445 | trace = hlua_traceback(lua->T, ", "); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1446 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1447 | 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] | 1448 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1449 | 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] | 1450 | ret = HLUA_E_ERRMSG; |
| 1451 | break; |
| 1452 | |
| 1453 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1454 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1455 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1456 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1457 | break; |
| 1458 | |
| 1459 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1460 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1461 | if (!lua_checkstack(lua->T, 1)) { |
| 1462 | ret = HLUA_E_ERR; |
| 1463 | break; |
| 1464 | } |
| 1465 | msg = lua_tostring(lua->T, -1); |
| 1466 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1467 | lua_pop(lua->T, 1); |
| 1468 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1469 | 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] | 1470 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1471 | 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] | 1472 | ret = HLUA_E_ERRMSG; |
| 1473 | break; |
| 1474 | |
| 1475 | default: |
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 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1478 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1479 | break; |
| 1480 | } |
| 1481 | |
| 1482 | switch (ret) { |
| 1483 | case HLUA_E_AGAIN: |
| 1484 | break; |
| 1485 | |
| 1486 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1487 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1488 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1489 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1490 | break; |
| 1491 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1492 | case HLUA_E_ETMOUT: |
| 1493 | case HLUA_E_NOMEM: |
| 1494 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1495 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1496 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1497 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1498 | hlua_ctx_renew(lua, 0); |
| 1499 | break; |
| 1500 | |
| 1501 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1502 | HLUA_CLR_RUN(lua); |
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 | break; |
| 1505 | } |
| 1506 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1507 | /* This is the main exit point, remove the Lua lock. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1508 | if (lua->state_id == 0) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1509 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1510 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1511 | return ret; |
| 1512 | } |
| 1513 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1514 | /* This function exit the current code. */ |
| 1515 | __LJMP static int hlua_done(lua_State *L) |
| 1516 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1517 | struct hlua *hlua; |
| 1518 | |
| 1519 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1520 | hlua = hlua_gethlua(L); |
| 1521 | if (!hlua) |
| 1522 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1523 | |
| 1524 | hlua->flags |= HLUA_EXIT; |
| 1525 | WILL_LJMP(lua_error(L)); |
| 1526 | |
| 1527 | return 0; |
| 1528 | } |
| 1529 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1530 | /* This function is an LUA binding. It provides a function |
| 1531 | * for deleting ACL from a referenced ACL file. |
| 1532 | */ |
| 1533 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1534 | { |
| 1535 | const char *name; |
| 1536 | const char *key; |
| 1537 | struct pat_ref *ref; |
| 1538 | |
| 1539 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1540 | |
| 1541 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1542 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1543 | |
| 1544 | ref = pat_ref_lookup(name); |
| 1545 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1546 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1547 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1548 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1549 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1550 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1551 | return 0; |
| 1552 | } |
| 1553 | |
| 1554 | /* This function is an LUA binding. It provides a function |
| 1555 | * for deleting map entry from a referenced map file. |
| 1556 | */ |
| 1557 | static int hlua_del_map(lua_State *L) |
| 1558 | { |
| 1559 | const char *name; |
| 1560 | const char *key; |
| 1561 | struct pat_ref *ref; |
| 1562 | |
| 1563 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1564 | |
| 1565 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1566 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1567 | |
| 1568 | ref = pat_ref_lookup(name); |
| 1569 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1570 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1571 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1572 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1573 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1574 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1575 | return 0; |
| 1576 | } |
| 1577 | |
| 1578 | /* This function is an LUA binding. It provides a function |
| 1579 | * for adding ACL pattern from a referenced ACL file. |
| 1580 | */ |
| 1581 | static int hlua_add_acl(lua_State *L) |
| 1582 | { |
| 1583 | const char *name; |
| 1584 | const char *key; |
| 1585 | struct pat_ref *ref; |
| 1586 | |
| 1587 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1588 | |
| 1589 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1590 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1591 | |
| 1592 | ref = pat_ref_lookup(name); |
| 1593 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1594 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1595 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1596 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1597 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1598 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1599 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1600 | return 0; |
| 1601 | } |
| 1602 | |
| 1603 | /* This function is an LUA binding. It provides a function |
| 1604 | * for setting map pattern and sample from a referenced map |
| 1605 | * file. |
| 1606 | */ |
| 1607 | static int hlua_set_map(lua_State *L) |
| 1608 | { |
| 1609 | const char *name; |
| 1610 | const char *key; |
| 1611 | const char *value; |
| 1612 | struct pat_ref *ref; |
| 1613 | |
| 1614 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1615 | |
| 1616 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1617 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1618 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1619 | |
| 1620 | ref = pat_ref_lookup(name); |
| 1621 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1622 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1623 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1624 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1625 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1626 | pat_ref_set(ref, key, value, NULL); |
| 1627 | else |
| 1628 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1629 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1630 | return 0; |
| 1631 | } |
| 1632 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1633 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1634 | * 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] | 1635 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1636 | * the name of the object (_G[<name>] = <metable> ). |
| 1637 | * |
| 1638 | * A metable is a table that modify the standard behavior of a standard |
| 1639 | * access to the associated data. The entries of this new metatable are |
| 1640 | * defined as is: |
| 1641 | * |
| 1642 | * http://lua-users.org/wiki/MetatableEvents |
| 1643 | * |
| 1644 | * __index |
| 1645 | * |
| 1646 | * we access an absent field in a table, the result is nil. This is |
| 1647 | * true, but it is not the whole truth. Actually, such access triggers |
| 1648 | * the interpreter to look for an __index metamethod: If there is no |
| 1649 | * such method, as usually happens, then the access results in nil; |
| 1650 | * otherwise, the metamethod will provide the result. |
| 1651 | * |
| 1652 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1653 | * the key does not appear in the table, but the metatable has an __index |
| 1654 | * property: |
| 1655 | * |
| 1656 | * - if the value is a function, the function is called, passing in the |
| 1657 | * table and the key; the return value of that function is returned as |
| 1658 | * the result. |
| 1659 | * |
| 1660 | * - if the value is another table, the value of the key in that table is |
| 1661 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1662 | * table's metatable has an __index property, then it continues on up) |
| 1663 | * |
| 1664 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1665 | * |
| 1666 | * http://www.lua.org/pil/13.4.1.html |
| 1667 | * |
| 1668 | * __newindex |
| 1669 | * |
| 1670 | * Like __index, but control property assignment. |
| 1671 | * |
| 1672 | * __mode - Control weak references. A string value with one or both |
| 1673 | * of the characters 'k' and 'v' which specifies that the the |
| 1674 | * keys and/or values in the table are weak references. |
| 1675 | * |
| 1676 | * __call - Treat a table like a function. When a table is followed by |
| 1677 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1678 | * a __call key pointing to a function, that function is invoked |
| 1679 | * (passing any specified arguments) and the return value is |
| 1680 | * returned. |
| 1681 | * |
| 1682 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1683 | * called, if the metatable for myTable has a __metatable |
| 1684 | * key, the value of that key is returned instead of the |
| 1685 | * actual metatable. |
| 1686 | * |
| 1687 | * __tostring - Control string representation. When the builtin |
| 1688 | * "tostring( myTable )" function is called, if the metatable |
| 1689 | * for myTable has a __tostring property set to a function, |
| 1690 | * that function is invoked (passing myTable to it) and the |
| 1691 | * return value is used as the string representation. |
| 1692 | * |
| 1693 | * __len - Control table length. When the table length is requested using |
| 1694 | * the length operator ( '#' ), if the metatable for myTable has |
| 1695 | * a __len key pointing to a function, that function is invoked |
| 1696 | * (passing myTable to it) and the return value used as the value |
| 1697 | * of "#myTable". |
| 1698 | * |
| 1699 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1700 | * collected, if the metatable has a __gc field pointing to a |
| 1701 | * function, that function is first invoked, passing the userdata |
| 1702 | * to it. The __gc metamethod is not called for tables. |
| 1703 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1704 | * |
| 1705 | * Special metamethods for redefining standard operators: |
| 1706 | * http://www.lua.org/pil/13.1.html |
| 1707 | * |
| 1708 | * __add "+" |
| 1709 | * __sub "-" |
| 1710 | * __mul "*" |
| 1711 | * __div "/" |
| 1712 | * __unm "!" |
| 1713 | * __pow "^" |
| 1714 | * __concat ".." |
| 1715 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1716 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1717 | * http://www.lua.org/pil/13.2.html |
| 1718 | * |
| 1719 | * __eq "==" |
| 1720 | * __lt "<" |
| 1721 | * __le "<=" |
| 1722 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1723 | |
| 1724 | /* |
| 1725 | * |
| 1726 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1727 | * Class Map |
| 1728 | * |
| 1729 | * |
| 1730 | */ |
| 1731 | |
| 1732 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1733 | * a class session, otherwise it throws an error. |
| 1734 | */ |
| 1735 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1736 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1737 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1738 | } |
| 1739 | |
| 1740 | /* This function is the map constructor. It don't need |
| 1741 | * the class Map object. It creates and return a new Map |
| 1742 | * object. It must be called only during "body" or "init" |
| 1743 | * context because it process some filesystem accesses. |
| 1744 | */ |
| 1745 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1746 | { |
| 1747 | const char *fn; |
| 1748 | int match = PAT_MATCH_STR; |
| 1749 | struct sample_conv conv; |
| 1750 | const char *file = ""; |
| 1751 | int line = 0; |
| 1752 | lua_Debug ar; |
| 1753 | char *err = NULL; |
| 1754 | struct arg args[2]; |
| 1755 | |
| 1756 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1757 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1758 | |
| 1759 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1760 | |
| 1761 | if (lua_gettop(L) >= 2) { |
| 1762 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1763 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1764 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1765 | } |
| 1766 | |
| 1767 | /* Get Lua filename and line number. */ |
| 1768 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1769 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1770 | if (ar.currentline > 0) { /* is there info? */ |
| 1771 | file = ar.short_src; |
| 1772 | line = ar.currentline; |
| 1773 | } |
| 1774 | } |
| 1775 | |
| 1776 | /* fill fake sample_conv struct. */ |
| 1777 | conv.kw = ""; /* unused. */ |
| 1778 | conv.process = NULL; /* unused. */ |
| 1779 | conv.arg_mask = 0; /* unused. */ |
| 1780 | conv.val_args = NULL; /* unused. */ |
| 1781 | conv.out_type = SMP_T_STR; |
| 1782 | conv.private = (void *)(long)match; |
| 1783 | switch (match) { |
| 1784 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1785 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1786 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1787 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1788 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1789 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1790 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1791 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1792 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1793 | default: |
| 1794 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1795 | } |
| 1796 | |
| 1797 | /* fill fake args. */ |
| 1798 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1799 | args[0].data.str.area = strdup(fn); |
| 1800 | args[0].data.str.data = strlen(fn); |
| 1801 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1802 | args[1].type = ARGT_STOP; |
| 1803 | |
| 1804 | /* load the map. */ |
| 1805 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1806 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1807 | * free the err variable. |
| 1808 | */ |
| 1809 | luaL_where(L, 1); |
| 1810 | lua_pushfstring(L, "'new': %s.", err); |
| 1811 | lua_concat(L, 2); |
| 1812 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1813 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1814 | WILL_LJMP(lua_error(L)); |
| 1815 | } |
| 1816 | |
| 1817 | /* create the lua object. */ |
| 1818 | lua_newtable(L); |
| 1819 | lua_pushlightuserdata(L, args[0].data.map); |
| 1820 | lua_rawseti(L, -2, 0); |
| 1821 | |
| 1822 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1823 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1824 | lua_setmetatable(L, -2); |
| 1825 | |
| 1826 | |
| 1827 | return 1; |
| 1828 | } |
| 1829 | |
| 1830 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1831 | { |
| 1832 | struct map_descriptor *desc; |
| 1833 | struct pattern *pat; |
| 1834 | struct sample smp; |
| 1835 | |
| 1836 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1837 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1838 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1839 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1840 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1841 | } |
| 1842 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1843 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1844 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1845 | 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] | 1846 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1847 | } |
| 1848 | |
| 1849 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1850 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1851 | if (str) |
| 1852 | lua_pushstring(L, ""); |
| 1853 | else |
| 1854 | lua_pushnil(L); |
| 1855 | return 1; |
| 1856 | } |
| 1857 | |
| 1858 | /* 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] | 1859 | 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] | 1860 | return 1; |
| 1861 | } |
| 1862 | |
| 1863 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1864 | { |
| 1865 | return _hlua_map_lookup(L, 0); |
| 1866 | } |
| 1867 | |
| 1868 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1869 | { |
| 1870 | return _hlua_map_lookup(L, 1); |
| 1871 | } |
| 1872 | |
| 1873 | /* |
| 1874 | * |
| 1875 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1876 | * Class Socket |
| 1877 | * |
| 1878 | * |
| 1879 | */ |
| 1880 | |
| 1881 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1882 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1883 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1884 | } |
| 1885 | |
| 1886 | /* 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] | 1887 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1888 | * received. |
| 1889 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1890 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1891 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1892 | struct stream_interface *si = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1893 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1894 | if (appctx->ctx.hlua_cosocket.die) { |
| 1895 | si_shutw(si); |
| 1896 | si_shutr(si); |
| 1897 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1898 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1899 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1900 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1901 | } |
| 1902 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1903 | /* If we can't write, wakeup the pending write signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1904 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1905 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1906 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1907 | /* If we can't read, wakeup the pending read signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1908 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1909 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1910 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1911 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1912 | * to be notified whenever the connection completes. |
| 1913 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1914 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1915 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1916 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1917 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1918 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1919 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1920 | |
| 1921 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1922 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1923 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1924 | /* 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] | 1925 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1926 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1927 | |
| 1928 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1929 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1930 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1931 | |
| 1932 | /* Some data were injected in the buffer, notify the stream |
| 1933 | * interface. |
| 1934 | */ |
| 1935 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1936 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1937 | |
| 1938 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1939 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1940 | */ |
| 1941 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1942 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1943 | } |
| 1944 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1945 | /* This function is called when the "struct stream" is destroyed. |
| 1946 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1947 | * Wake all the pending signals. |
| 1948 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1949 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1950 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1951 | struct xref *peer; |
| 1952 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1953 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1954 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1955 | if (peer) |
| 1956 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1957 | |
| 1958 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1959 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1960 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1961 | } |
| 1962 | |
| 1963 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1964 | * uses this object. If the stream does not exists, just quit. |
| 1965 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1966 | * pending signal can rest in the read and write lists. destroy |
| 1967 | * it. |
| 1968 | */ |
| 1969 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1970 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1971 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1972 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1973 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1974 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1975 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1976 | |
| 1977 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1978 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1979 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1980 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1981 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1982 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1983 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1984 | appctx->ctx.hlua_cosocket.die = 1; |
| 1985 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1986 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1987 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1988 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1989 | return 0; |
| 1990 | } |
| 1991 | |
| 1992 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1993 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1994 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1995 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1996 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1997 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1998 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1999 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2000 | struct hlua *hlua; |
| 2001 | |
| 2002 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2003 | hlua = hlua_gethlua(L); |
| 2004 | if (!hlua) |
| 2005 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2006 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2007 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2008 | |
| 2009 | /* Check if we run on the same thread than the xreator thread. |
| 2010 | * We cannot access to the socket if the thread is different. |
| 2011 | */ |
| 2012 | if (socket->tid != tid) |
| 2013 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2014 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2015 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2016 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2017 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2018 | |
| 2019 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2020 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2021 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2022 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2023 | appctx->ctx.hlua_cosocket.die = 1; |
| 2024 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2025 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2026 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2027 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2028 | return 0; |
| 2029 | } |
| 2030 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2031 | /* The close function calls close_helper. |
| 2032 | */ |
| 2033 | __LJMP static int hlua_socket_close(lua_State *L) |
| 2034 | { |
| 2035 | MAY_LJMP(check_args(L, 1, "close")); |
| 2036 | return hlua_socket_close_helper(L); |
| 2037 | } |
| 2038 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2039 | /* This Lua function assumes that the stack contain three parameters. |
| 2040 | * 1 - USERDATA containing a struct socket |
| 2041 | * 2 - INTEGER with values of the macro defined below |
| 2042 | * If the integer is -1, we must read at most one line. |
| 2043 | * If the integer is -2, we ust read all the data until the |
| 2044 | * end of the stream. |
| 2045 | * If the integer is positive value, we must read a number of |
| 2046 | * bytes corresponding to this value. |
| 2047 | */ |
| 2048 | #define HLSR_READ_LINE (-1) |
| 2049 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2050 | __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] | 2051 | { |
| 2052 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2053 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2054 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2055 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2056 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2057 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2058 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2059 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2060 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2061 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2062 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 2063 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2064 | struct stream_interface *si; |
| 2065 | struct stream *s; |
| 2066 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2067 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2068 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2069 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2070 | hlua = hlua_gethlua(L); |
| 2071 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2072 | /* Check if this lua stack is schedulable. */ |
| 2073 | if (!hlua || !hlua->task) |
| 2074 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 2075 | "'frontend', 'backend' or 'task'")); |
| 2076 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2077 | /* Check if we run on the same thread than the xreator thread. |
| 2078 | * We cannot access to the socket if the thread is different. |
| 2079 | */ |
| 2080 | if (socket->tid != tid) |
| 2081 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2082 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2083 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2084 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2085 | if (!peer) |
| 2086 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2087 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2088 | si = appctx->owner; |
| 2089 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2090 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2091 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2092 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2093 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2094 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2095 | if (nblk < 0) /* Connection close. */ |
| 2096 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2097 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2098 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2099 | |
| 2100 | /* remove final \r\n. */ |
| 2101 | if (nblk == 1) { |
| 2102 | if (blk1[len1-1] == '\n') { |
| 2103 | len1--; |
| 2104 | skip_at_end++; |
| 2105 | if (blk1[len1-1] == '\r') { |
| 2106 | len1--; |
| 2107 | skip_at_end++; |
| 2108 | } |
| 2109 | } |
| 2110 | } |
| 2111 | else { |
| 2112 | if (blk2[len2-1] == '\n') { |
| 2113 | len2--; |
| 2114 | skip_at_end++; |
| 2115 | if (blk2[len2-1] == '\r') { |
| 2116 | len2--; |
| 2117 | skip_at_end++; |
| 2118 | } |
| 2119 | } |
| 2120 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2121 | } |
| 2122 | |
| 2123 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2124 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2125 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2126 | if (nblk < 0) /* Connection close. */ |
| 2127 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2128 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2129 | goto connection_empty; |
| 2130 | } |
| 2131 | |
| 2132 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2133 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2134 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2135 | if (nblk < 0) /* Connection close. */ |
| 2136 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2137 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2138 | goto connection_empty; |
| 2139 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2140 | missing_bytes = wanted - socket->b.n; |
| 2141 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2142 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2143 | len1 = missing_bytes; |
| 2144 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2145 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2146 | } |
| 2147 | |
| 2148 | len = len1; |
| 2149 | |
| 2150 | luaL_addlstring(&socket->b, blk1, len1); |
| 2151 | if (nblk == 2) { |
| 2152 | len += len2; |
| 2153 | luaL_addlstring(&socket->b, blk2, len2); |
| 2154 | } |
| 2155 | |
| 2156 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2157 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2158 | |
| 2159 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2160 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2161 | |
| 2162 | /* If the pattern reclaim to read all the data |
| 2163 | * in the connection, got out. |
| 2164 | */ |
| 2165 | if (wanted == HLSR_READ_ALL) |
| 2166 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2167 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2168 | goto connection_empty; |
| 2169 | |
| 2170 | /* Return result. */ |
| 2171 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2172 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2173 | return 1; |
| 2174 | |
| 2175 | connection_closed: |
| 2176 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2177 | xref_unlock(&socket->xref, peer); |
| 2178 | |
| 2179 | no_peer: |
| 2180 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2181 | /* If the buffer containds data. */ |
| 2182 | if (socket->b.n > 0) { |
| 2183 | luaL_pushresult(&socket->b); |
| 2184 | return 1; |
| 2185 | } |
| 2186 | lua_pushnil(L); |
| 2187 | lua_pushstring(L, "connection closed."); |
| 2188 | return 2; |
| 2189 | |
| 2190 | connection_empty: |
| 2191 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2192 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 2193 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2194 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2195 | } |
| 2196 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2197 | 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] | 2198 | return 0; |
| 2199 | } |
| 2200 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2201 | /* This Lua function gets two parameters. The first one can be string |
| 2202 | * or a number. If the string is "*l", the user requires one line. If |
| 2203 | * 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] | 2204 | * If the value is a number, the user require a number of bytes equal |
| 2205 | * to the value. The default value is "*l" (a line). |
| 2206 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2207 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2208 | * integer takes this values: |
| 2209 | * -1 : read a line |
| 2210 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2211 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2212 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2213 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2214 | * concatenated with the read data. |
| 2215 | */ |
| 2216 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2217 | { |
| 2218 | int wanted = HLSR_READ_LINE; |
| 2219 | const char *pattern; |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2220 | int lastarg, type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2221 | char *error; |
| 2222 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2223 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2224 | |
| 2225 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2226 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2227 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2228 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2229 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2230 | /* Check if we run on the same thread than the xreator thread. |
| 2231 | * We cannot access to the socket if the thread is different. |
| 2232 | */ |
| 2233 | if (socket->tid != tid) |
| 2234 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2235 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2236 | /* check for pattern. */ |
| 2237 | if (lua_gettop(L) >= 2) { |
| 2238 | type = lua_type(L, 2); |
| 2239 | if (type == LUA_TSTRING) { |
| 2240 | pattern = lua_tostring(L, 2); |
| 2241 | if (strcmp(pattern, "*a") == 0) |
| 2242 | wanted = HLSR_READ_ALL; |
| 2243 | else if (strcmp(pattern, "*l") == 0) |
| 2244 | wanted = HLSR_READ_LINE; |
| 2245 | else { |
| 2246 | wanted = strtoll(pattern, &error, 10); |
| 2247 | if (*error != '\0') |
| 2248 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2249 | } |
| 2250 | } |
| 2251 | else if (type == LUA_TNUMBER) { |
| 2252 | wanted = lua_tointeger(L, 2); |
| 2253 | if (wanted < 0) |
| 2254 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2255 | } |
| 2256 | } |
| 2257 | |
| 2258 | /* Set pattern. */ |
| 2259 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2260 | |
| 2261 | /* Check if we would replace the top by itself. */ |
| 2262 | if (lua_gettop(L) != 2) |
| 2263 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2264 | |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2265 | /* Save index of the top of the stack because since buffers are used, it |
| 2266 | * may change |
| 2267 | */ |
| 2268 | lastarg = lua_gettop(L); |
| 2269 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2270 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2271 | luaL_buffinit(L, &socket->b); |
| 2272 | |
| 2273 | /* Check prefix. */ |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2274 | if (lastarg >= 3) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2275 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2276 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2277 | pattern = lua_tolstring(L, 3, &len); |
| 2278 | luaL_addlstring(&socket->b, pattern, len); |
| 2279 | } |
| 2280 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2281 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2282 | } |
| 2283 | |
| 2284 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2285 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2286 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2287 | 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] | 2288 | { |
| 2289 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2290 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2291 | struct appctx *appctx; |
| 2292 | size_t buf_len; |
| 2293 | const char *buf; |
| 2294 | int len; |
| 2295 | int send_len; |
| 2296 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2297 | struct xref *peer; |
| 2298 | struct stream_interface *si; |
| 2299 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2300 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2301 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2302 | hlua = hlua_gethlua(L); |
| 2303 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2304 | /* Check if this lua stack is schedulable. */ |
| 2305 | if (!hlua || !hlua->task) |
| 2306 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2307 | "'frontend', 'backend' or 'task'")); |
| 2308 | |
| 2309 | /* Get object */ |
| 2310 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2311 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2312 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2313 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2314 | /* Check if we run on the same thread than the xreator thread. |
| 2315 | * We cannot access to the socket if the thread is different. |
| 2316 | */ |
| 2317 | if (socket->tid != tid) |
| 2318 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2319 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2320 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2321 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2322 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2323 | lua_pushinteger(L, -1); |
| 2324 | return 1; |
| 2325 | } |
| 2326 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2327 | si = appctx->owner; |
| 2328 | s = si_strm(si); |
| 2329 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2330 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2331 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2332 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2333 | lua_pushinteger(L, -1); |
| 2334 | return 1; |
| 2335 | } |
| 2336 | |
| 2337 | /* Update the input buffer data. */ |
| 2338 | buf += sent; |
| 2339 | send_len = buf_len - sent; |
| 2340 | |
| 2341 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2342 | if (sent >= buf_len) { |
| 2343 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2344 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2345 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2346 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2347 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2348 | * the request buffer if its not required. |
| 2349 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2350 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2351 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2352 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2353 | } |
| 2354 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2355 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2356 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2357 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2358 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2359 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2360 | |
| 2361 | /* send data */ |
| 2362 | if (len < send_len) |
| 2363 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2364 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2365 | |
| 2366 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2367 | * the data" (-2) are not expected because the available length |
| 2368 | * is tested. |
| 2369 | * Other unknown error are also not expected. |
| 2370 | */ |
| 2371 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2372 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2373 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2374 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2375 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2376 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2377 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2378 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2379 | return 1; |
| 2380 | } |
| 2381 | |
| 2382 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2383 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2384 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2385 | s->req.rex = TICK_ETERNITY; |
| 2386 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2387 | |
| 2388 | /* Update length sent. */ |
| 2389 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2390 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2391 | |
| 2392 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2393 | if (sent + len >= buf_len) { |
| 2394 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2395 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2396 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2397 | |
| 2398 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2399 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2400 | xref_unlock(&socket->xref, peer); |
| 2401 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2402 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2403 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2404 | 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] | 2405 | return 0; |
| 2406 | } |
| 2407 | |
| 2408 | /* This function initiate the send of data. It just check the input |
| 2409 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2410 | * 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] | 2411 | * "hlua_socket_write_yield" that can yield. |
| 2412 | * |
| 2413 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2414 | * the associated object. The second is a string buffer. The third is |
| 2415 | * a facultative integer that represents where is the buffer position |
| 2416 | * of the start of the data that can send. The first byte is the |
| 2417 | * position "1". The default value is "1". The fourth argument is a |
| 2418 | * facultative integer that represents where is the buffer position |
| 2419 | * of the end of the data that can send. The default is the last byte. |
| 2420 | */ |
| 2421 | static int hlua_socket_send(struct lua_State *L) |
| 2422 | { |
| 2423 | int i; |
| 2424 | int j; |
| 2425 | const char *buf; |
| 2426 | size_t buf_len; |
| 2427 | |
| 2428 | /* Check number of arguments. */ |
| 2429 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2430 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2431 | |
| 2432 | /* Get the string. */ |
| 2433 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2434 | |
| 2435 | /* Get and check j. */ |
| 2436 | if (lua_gettop(L) == 4) { |
| 2437 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2438 | if (j < 0) |
| 2439 | j = buf_len + j + 1; |
| 2440 | if (j > buf_len) |
| 2441 | j = buf_len + 1; |
| 2442 | lua_pop(L, 1); |
| 2443 | } |
| 2444 | else |
| 2445 | j = buf_len; |
| 2446 | |
| 2447 | /* Get and check i. */ |
| 2448 | if (lua_gettop(L) == 3) { |
| 2449 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2450 | if (i < 0) |
| 2451 | i = buf_len + i + 1; |
| 2452 | if (i > buf_len) |
| 2453 | i = buf_len + 1; |
| 2454 | lua_pop(L, 1); |
| 2455 | } else |
| 2456 | i = 1; |
| 2457 | |
| 2458 | /* Check bth i and j. */ |
| 2459 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2460 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2461 | return 1; |
| 2462 | } |
| 2463 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2464 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2465 | return 1; |
| 2466 | } |
| 2467 | if (i == 0) |
| 2468 | i = 1; |
| 2469 | if (j == 0) |
| 2470 | j = 1; |
| 2471 | |
| 2472 | /* Pop the string. */ |
| 2473 | lua_pop(L, 1); |
| 2474 | |
| 2475 | /* Update the buffer length. */ |
| 2476 | buf += i - 1; |
| 2477 | buf_len = j - i + 1; |
| 2478 | lua_pushlstring(L, buf, buf_len); |
| 2479 | |
| 2480 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2481 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2482 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2483 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2484 | } |
| 2485 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2486 | #define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345") |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2487 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2488 | { |
| 2489 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2490 | int ret; |
| 2491 | int len; |
| 2492 | char *p; |
| 2493 | |
| 2494 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2495 | if (ret <= 0) { |
| 2496 | lua_pushnil(L); |
| 2497 | return 1; |
| 2498 | } |
| 2499 | |
| 2500 | if (ret == AF_UNIX) { |
| 2501 | lua_pushstring(L, buffer+1); |
| 2502 | return 1; |
| 2503 | } |
| 2504 | else if (ret == AF_INET6) { |
| 2505 | buffer[0] = '['; |
| 2506 | len = strlen(buffer); |
| 2507 | buffer[len] = ']'; |
| 2508 | len++; |
| 2509 | buffer[len] = ':'; |
| 2510 | len++; |
| 2511 | p = buffer; |
| 2512 | } |
| 2513 | else if (ret == AF_INET) { |
| 2514 | p = buffer + 1; |
| 2515 | len = strlen(p); |
| 2516 | p[len] = ':'; |
| 2517 | len++; |
| 2518 | } |
| 2519 | else { |
| 2520 | lua_pushnil(L); |
| 2521 | return 1; |
| 2522 | } |
| 2523 | |
| 2524 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2525 | lua_pushnil(L); |
| 2526 | return 1; |
| 2527 | } |
| 2528 | |
| 2529 | lua_pushstring(L, p); |
| 2530 | return 1; |
| 2531 | } |
| 2532 | |
| 2533 | /* Returns information about the peer of the connection. */ |
| 2534 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2535 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2536 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2537 | struct xref *peer; |
| 2538 | struct appctx *appctx; |
| 2539 | struct stream_interface *si; |
| 2540 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2541 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2542 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2543 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2544 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2545 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2546 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2547 | /* Check if we run on the same thread than the xreator thread. |
| 2548 | * We cannot access to the socket if the thread is different. |
| 2549 | */ |
| 2550 | if (socket->tid != tid) |
| 2551 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2552 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2553 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2554 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2555 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2556 | lua_pushnil(L); |
| 2557 | return 1; |
| 2558 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2559 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2560 | si = appctx->owner; |
| 2561 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2562 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2563 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2564 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2565 | lua_pushnil(L); |
| 2566 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2567 | } |
| 2568 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2569 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2570 | xref_unlock(&socket->xref, peer); |
| 2571 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2572 | } |
| 2573 | |
| 2574 | /* Returns information about my connection side. */ |
| 2575 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2576 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2577 | struct hlua_socket *socket; |
| 2578 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2579 | struct appctx *appctx; |
| 2580 | struct xref *peer; |
| 2581 | struct stream_interface *si; |
| 2582 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2583 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2584 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2585 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2586 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2587 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2588 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2589 | /* Check if we run on the same thread than the xreator thread. |
| 2590 | * We cannot access to the socket if the thread is different. |
| 2591 | */ |
| 2592 | if (socket->tid != tid) |
| 2593 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2594 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2595 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2596 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2597 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2598 | lua_pushnil(L); |
| 2599 | return 1; |
| 2600 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2601 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2602 | si = appctx->owner; |
| 2603 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2604 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2605 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2606 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2607 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2608 | lua_pushnil(L); |
| 2609 | return 1; |
| 2610 | } |
| 2611 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2612 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2613 | xref_unlock(&socket->xref, peer); |
| 2614 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2615 | } |
| 2616 | |
| 2617 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2618 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2619 | .obj_type = OBJ_TYPE_APPLET, |
| 2620 | .name = "<LUA_TCP>", |
| 2621 | .fct = hlua_socket_handler, |
| 2622 | .release = hlua_socket_release, |
| 2623 | }; |
| 2624 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2625 | __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] | 2626 | { |
| 2627 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2628 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2629 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2630 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2631 | struct stream_interface *si; |
| 2632 | struct stream *s; |
| 2633 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2634 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2635 | hlua = hlua_gethlua(L); |
| 2636 | if (!hlua) |
| 2637 | return 0; |
| 2638 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2639 | /* Check if we run on the same thread than the xreator thread. |
| 2640 | * We cannot access to the socket if the thread is different. |
| 2641 | */ |
| 2642 | if (socket->tid != tid) |
| 2643 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2644 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2645 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2646 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2647 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2648 | lua_pushnil(L); |
| 2649 | lua_pushstring(L, "Can't connect"); |
| 2650 | return 2; |
| 2651 | } |
| 2652 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2653 | si = appctx->owner; |
| 2654 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2655 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2656 | /* Check if we run on the same thread than the xreator thread. |
| 2657 | * We cannot access to the socket if the thread is different. |
| 2658 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2659 | if (socket->tid != tid) { |
| 2660 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2661 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2662 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2663 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2664 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2665 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2666 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2667 | lua_pushnil(L); |
| 2668 | lua_pushstring(L, "Can't connect"); |
| 2669 | return 2; |
| 2670 | } |
| 2671 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2672 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2673 | |
| 2674 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2675 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2676 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2677 | lua_pushinteger(L, 1); |
| 2678 | return 1; |
| 2679 | } |
| 2680 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2681 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2682 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2683 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2684 | } |
| 2685 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2686 | 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] | 2687 | return 0; |
| 2688 | } |
| 2689 | |
| 2690 | /* This function fail or initite the connection. */ |
| 2691 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2692 | { |
| 2693 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2694 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2695 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2696 | struct hlua *hlua; |
| 2697 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2698 | int low, high; |
| 2699 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2700 | struct xref *peer; |
| 2701 | struct stream_interface *si; |
| 2702 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2703 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2704 | if (lua_gettop(L) < 2) |
| 2705 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2706 | |
| 2707 | /* Get args. */ |
| 2708 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2709 | |
| 2710 | /* Check if we run on the same thread than the xreator thread. |
| 2711 | * We cannot access to the socket if the thread is different. |
| 2712 | */ |
| 2713 | if (socket->tid != tid) |
| 2714 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2715 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2716 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2717 | if (lua_gettop(L) >= 3) { |
| 2718 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2719 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2720 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2721 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2722 | * that are not enclosed within square brackets. |
| 2723 | */ |
| 2724 | if (port > 0) { |
| 2725 | luaL_buffinit(L, &b); |
| 2726 | luaL_addstring(&b, ip); |
| 2727 | luaL_addchar(&b, ':'); |
| 2728 | luaL_pushresult(&b); |
| 2729 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2730 | } |
| 2731 | } |
| 2732 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2733 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2734 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2735 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2736 | lua_pushnil(L); |
| 2737 | return 1; |
| 2738 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2739 | |
| 2740 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2741 | 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] | 2742 | if (!addr) { |
| 2743 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2744 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2745 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2746 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2747 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2748 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2749 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2750 | if (port == -1) { |
| 2751 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2752 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2753 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2754 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2755 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2756 | if (port == -1) { |
| 2757 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2758 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2759 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2760 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2761 | } |
| 2762 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2763 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2764 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2765 | si = appctx->owner; |
| 2766 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2767 | |
Willy Tarreau | 9b7587a | 2020-10-15 07:32:10 +0200 | [diff] [blame] | 2768 | if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2769 | xref_unlock(&socket->xref, peer); |
| 2770 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2771 | } |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2772 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2773 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2774 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2775 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2776 | if (!hlua) |
| 2777 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2778 | |
| 2779 | /* inform the stream that we want to be notified whenever the |
| 2780 | * connection completes. |
| 2781 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2782 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2783 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2784 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2785 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2786 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2787 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2788 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2789 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2790 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2791 | } |
| 2792 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2793 | |
| 2794 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2795 | /* Return yield waiting for connection. */ |
| 2796 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2797 | 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] | 2798 | |
| 2799 | return 0; |
| 2800 | } |
| 2801 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2802 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2803 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2804 | { |
| 2805 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2806 | struct xref *peer; |
| 2807 | struct appctx *appctx; |
| 2808 | struct stream_interface *si; |
| 2809 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2810 | |
| 2811 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2812 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2813 | |
| 2814 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2815 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2816 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2817 | lua_pushnil(L); |
| 2818 | return 1; |
| 2819 | } |
| 2820 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2821 | si = appctx->owner; |
| 2822 | s = si_strm(si); |
| 2823 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2824 | s->target = &socket_ssl->obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2825 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2826 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2827 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2828 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2829 | |
| 2830 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2831 | { |
| 2832 | return 0; |
| 2833 | } |
| 2834 | |
| 2835 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2836 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2837 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2838 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2839 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2840 | struct xref *peer; |
| 2841 | struct appctx *appctx; |
| 2842 | struct stream_interface *si; |
| 2843 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2844 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2845 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2846 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2847 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2848 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2849 | /* convert the timeout to millis */ |
| 2850 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2851 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2852 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2853 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2854 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2855 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2856 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2857 | 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] | 2858 | |
| 2859 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2860 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2861 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2862 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2863 | /* Check if we run on the same thread than the xreator thread. |
| 2864 | * We cannot access to the socket if the thread is different. |
| 2865 | */ |
| 2866 | if (socket->tid != tid) |
| 2867 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2868 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2869 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2870 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2871 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2872 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2873 | WILL_LJMP(lua_error(L)); |
| 2874 | return 0; |
| 2875 | } |
| 2876 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2877 | si = appctx->owner; |
| 2878 | s = si_strm(si); |
| 2879 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2880 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2881 | s->req.rto = tmout; |
| 2882 | s->req.wto = tmout; |
| 2883 | s->res.rto = tmout; |
| 2884 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2885 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2886 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2887 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2888 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2889 | |
| 2890 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2891 | task_queue(s->task); |
| 2892 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2893 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2894 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2895 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2896 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2897 | } |
| 2898 | |
| 2899 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2900 | { |
| 2901 | struct hlua_socket *socket; |
| 2902 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2903 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2904 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2905 | |
| 2906 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2907 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2908 | hlua_pusherror(L, "socket: full stack"); |
| 2909 | goto out_fail_conf; |
| 2910 | } |
| 2911 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2912 | /* Create the object: obj[0] = userdata. */ |
| 2913 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2914 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2915 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2916 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2917 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2918 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2919 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2920 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2921 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2922 | goto out_fail_conf; |
| 2923 | } |
| 2924 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2925 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2926 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2927 | lua_setmetatable(L, -2); |
| 2928 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2929 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2930 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2931 | if (!appctx) { |
| 2932 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2933 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2934 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2935 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2936 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2937 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2938 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2939 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2940 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2941 | /* Now create a session, task and stream for this applet */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 2942 | sess = session_new(socket_proxy, NULL, &appctx->obj_type); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2943 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2944 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2945 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2946 | } |
| 2947 | |
Christopher Faulet | 26256f8 | 2020-09-14 11:40:13 +0200 | [diff] [blame] | 2948 | strm = stream_new(sess, &appctx->obj_type, &BUF_NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2949 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2950 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2951 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2952 | } |
| 2953 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2954 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2955 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2956 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2957 | /* Configure "right" stream interface. this "si" is used to connect |
| 2958 | * and retrieve data from the server. The connection is initialized |
| 2959 | * with the "struct server". |
| 2960 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2961 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2962 | |
| 2963 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2964 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2965 | strm->target = &socket_tcp->obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2966 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2967 | return 1; |
| 2968 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2969 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2970 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2971 | out_fail_sess: |
| 2972 | appctx_free(appctx); |
| 2973 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2974 | WILL_LJMP(lua_error(L)); |
| 2975 | return 0; |
| 2976 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2977 | |
| 2978 | /* |
| 2979 | * |
| 2980 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2981 | * Class Channel |
| 2982 | * |
| 2983 | * |
| 2984 | */ |
| 2985 | |
| 2986 | /* Returns the struct hlua_channel join to the class channel in the |
| 2987 | * stack entry "ud" or throws an argument error. |
| 2988 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2989 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2990 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2991 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2992 | } |
| 2993 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2994 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2995 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2996 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2997 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2998 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2999 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3000 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3001 | return 0; |
| 3002 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3003 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3004 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3005 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3006 | |
| 3007 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3008 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 3009 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3010 | return 1; |
| 3011 | } |
| 3012 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3013 | /* Helper function returning a filter attached to a channel at the position <ud> |
| 3014 | * in the stack, filling the current offset and length of the filter. If no |
| 3015 | * filter is attached, NULL is returned and <offet> and <len> are not |
| 3016 | * initialized. |
| 3017 | */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3018 | 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] | 3019 | { |
| 3020 | struct filter *filter = NULL; |
| 3021 | |
| 3022 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 3023 | struct hlua_flt_ctx *flt_ctx; |
| 3024 | |
| 3025 | filter = lua_touserdata (L, -1); |
| 3026 | flt_ctx = filter->ctx; |
| 3027 | if (hlua_filter_from_payload(filter)) { |
| 3028 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 3029 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 3030 | } |
| 3031 | } |
| 3032 | |
| 3033 | lua_pop(L, 1); |
| 3034 | return filter; |
| 3035 | } |
| 3036 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3037 | /* Copies <len> bytes of data present in the channel's buffer, starting at the |
| 3038 | * 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] | 3039 | * responsibility to ensure <len> and <offset> are valid. It always return the |
| 3040 | * length of the built string. <len> may be 0, in this case, an empty string is |
| 3041 | * created and 0 is returned. |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3042 | */ |
| 3043 | 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] | 3044 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3045 | size_t block1, block2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3046 | luaL_Buffer b; |
| 3047 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3048 | block1 = len; |
| 3049 | if (block1 > b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset))) |
| 3050 | block1 = b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset)); |
| 3051 | block2 = len - block1; |
| 3052 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3053 | luaL_buffinit(L, &b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3054 | luaL_addlstring(&b, b_peek(&chn->buf, offset), block1); |
| 3055 | if (block2) |
| 3056 | luaL_addlstring(&b, b_orig(&chn->buf), block2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3057 | luaL_pushresult(&b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3058 | return len; |
| 3059 | } |
| 3060 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3061 | /* Inserts the string <str> to the channel's buffer at the offset <offset>. This |
| 3062 | * function returns -1 if data cannot be copied. Otherwise, it returns the |
| 3063 | * number of bytes copied. |
| 3064 | */ |
| 3065 | static int _hlua_channel_insert(struct channel *chn, lua_State *L, struct ist str, size_t offset) |
| 3066 | { |
| 3067 | int ret = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3068 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3069 | /* Nothing to do, just return */ |
| 3070 | if (unlikely(istlen(str) == 0)) |
| 3071 | goto end; |
| 3072 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3073 | if (istlen(str) > c_room(chn)) { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3074 | ret = -1; |
| 3075 | goto end; |
| 3076 | } |
| 3077 | ret = b_insert_blk(&chn->buf, offset, istptr(str), istlen(str)); |
| 3078 | |
| 3079 | end: |
| 3080 | return ret; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3081 | } |
| 3082 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3083 | /* Removes <len> bytes of data at the absolute position <offset>. |
| 3084 | */ |
| 3085 | static void _hlua_channel_delete(struct channel *chn, size_t offset, size_t len) |
| 3086 | { |
| 3087 | size_t end = offset + len; |
| 3088 | |
| 3089 | if (b_peek(&chn->buf, end) != b_tail(&chn->buf)) |
| 3090 | b_move(&chn->buf, b_peek_ofs(&chn->buf, end), |
| 3091 | b_data(&chn->buf) - end, -len); |
| 3092 | b_sub(&chn->buf, len); |
| 3093 | } |
| 3094 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3095 | /* Copies input data in the channel's buffer. It is possible to set a specific |
| 3096 | * offset (0 by default) and a length (all remaining input data starting for the |
| 3097 | * offset by default). If there is not enough input data and more data can be |
| 3098 | * received, this function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3099 | * |
| 3100 | * From an action, All input data are considered. For a filter, the offset and |
| 3101 | * 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] | 3102 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3103 | __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] | 3104 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3105 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3106 | struct filter *filter; |
| 3107 | size_t input, output; |
| 3108 | int offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3109 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3110 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3111 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3112 | output = co_data(chn); |
| 3113 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3114 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3115 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3116 | if (filter && !hlua_filter_from_payload(filter)) |
| 3117 | WILL_LJMP(lua_error(L)); |
| 3118 | |
| 3119 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3120 | if (lua_gettop(L) > 1) { |
| 3121 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3122 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3123 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3124 | offset += output; |
| 3125 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3126 | lua_pushfstring(L, "offset out of range."); |
| 3127 | WILL_LJMP(lua_error(L)); |
| 3128 | } |
| 3129 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3130 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3131 | if (lua_gettop(L) == 3) { |
| 3132 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3133 | if (!len) |
| 3134 | goto dup; |
| 3135 | if (len == -1) |
| 3136 | len = global.tune.bufsize; |
| 3137 | if (len < 0) { |
| 3138 | lua_pushfstring(L, "length out of range."); |
| 3139 | WILL_LJMP(lua_error(L)); |
| 3140 | } |
| 3141 | } |
| 3142 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3143 | if (offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3144 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3145 | /* Yield waiting for more data, as requested */ |
| 3146 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_data_yield, TICK_ETERNITY, 0)); |
| 3147 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3148 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3149 | } |
| 3150 | |
| 3151 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3152 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3153 | return 1; |
| 3154 | } |
| 3155 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3156 | /* Copies the first line (including the trailing LF) of input data in the |
| 3157 | * channel's buffer. It is possible to set a specific offset (0 by default) and |
| 3158 | * a length (all remaining input data starting for the offset by default). If |
| 3159 | * there is not enough input data and more data can be received, the function |
| 3160 | * yields. If a length is explicitly specified, no more data are |
| 3161 | * copied. Otherwise, if no LF is found and more data can be received, this |
| 3162 | * function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3163 | * |
| 3164 | * From an action, All input data are considered. For a filter, the offset and |
| 3165 | * 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] | 3166 | */ |
| 3167 | __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] | 3168 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3169 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3170 | struct filter *filter; |
| 3171 | size_t l, input, output; |
| 3172 | int offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3173 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3174 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3175 | output = co_data(chn); |
| 3176 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3177 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3178 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3179 | if (filter && !hlua_filter_from_payload(filter)) |
| 3180 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3181 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3182 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3183 | if (lua_gettop(L) > 1) { |
| 3184 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3185 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3186 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3187 | offset += output; |
| 3188 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3189 | lua_pushfstring(L, "offset out of range."); |
| 3190 | WILL_LJMP(lua_error(L)); |
| 3191 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3192 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3193 | |
| 3194 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3195 | if (lua_gettop(L) == 3) { |
| 3196 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3197 | if (!len) |
| 3198 | goto dup; |
| 3199 | if (len == -1) |
| 3200 | len = global.tune.bufsize; |
| 3201 | if (len < 0) { |
| 3202 | lua_pushfstring(L, "length out of range."); |
| 3203 | WILL_LJMP(lua_error(L)); |
| 3204 | } |
| 3205 | } |
| 3206 | |
| 3207 | for (l = 0; l < len; l++) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3208 | if (l + offset >= output + input) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3209 | break; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3210 | if (*(b_peek(&chn->buf, offset + l)) == '\n') { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3211 | len = l+1; |
| 3212 | goto dup; |
| 3213 | } |
| 3214 | } |
| 3215 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3216 | if (offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3217 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3218 | /* Yield waiting for more data */ |
| 3219 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_line_yield, TICK_ETERNITY, 0)); |
| 3220 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3221 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3222 | } |
| 3223 | |
| 3224 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3225 | _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3226 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3227 | } |
| 3228 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3229 | /* [ DEPRECATED ] |
| 3230 | * |
| 3231 | * Duplicate all input data foud in the channel's buffer. The data are not |
| 3232 | * removed from the buffer. This function relies on _hlua_channel_dup(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3233 | * |
| 3234 | * From an action, All input data are considered. For a filter, the offset and |
| 3235 | * 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] | 3236 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3237 | __LJMP static int hlua_channel_dup(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3238 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3239 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3240 | struct filter *filter; |
| 3241 | size_t offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3242 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3243 | MAY_LJMP(check_args(L, 1, "dup")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3244 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3245 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3246 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3247 | WILL_LJMP(lua_error(L)); |
| 3248 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3249 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3250 | offset = co_data(chn); |
| 3251 | len = ci_data(chn); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3252 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3253 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3254 | if (filter && !hlua_filter_from_payload(filter)) |
| 3255 | WILL_LJMP(lua_error(L)); |
| 3256 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3257 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3258 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3259 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3260 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3261 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3262 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3263 | return 1; |
| 3264 | } |
| 3265 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3266 | /* [ DEPRECATED ] |
| 3267 | * |
| 3268 | * Get all input data foud in the channel's buffer. The data are removed from |
| 3269 | * the buffer after the copy. This function relies on _hlua_channel_dup() and |
| 3270 | * _hlua_channel_delete(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3271 | * |
| 3272 | * From an action, All input data are considered. For a filter, the offset and |
| 3273 | * 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] | 3274 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3275 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3276 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3277 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3278 | struct filter *filter; |
| 3279 | size_t offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3280 | int ret; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3281 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3282 | MAY_LJMP(check_args(L, 1, "get")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3283 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3284 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3285 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3286 | WILL_LJMP(lua_error(L)); |
| 3287 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3288 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3289 | offset = co_data(chn); |
| 3290 | len = ci_data(chn); |
| 3291 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3292 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3293 | if (filter && !hlua_filter_from_payload(filter)) |
| 3294 | WILL_LJMP(lua_error(L)); |
| 3295 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3296 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3297 | lua_pushnil(L); |
| 3298 | return 1; |
| 3299 | } |
| 3300 | |
| 3301 | ret = _hlua_channel_dup(chn, L, offset, len); |
| 3302 | _hlua_channel_delete(chn, offset, ret); |
| 3303 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3304 | } |
| 3305 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3306 | /* This functions consumes and returns one line. If the channel is closed, |
| 3307 | * 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] | 3308 | * 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] | 3309 | * value. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3310 | * |
| 3311 | * From an action, All input data are considered. For a filter, the offset and |
| 3312 | * 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] | 3313 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3314 | __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] | 3315 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3316 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3317 | struct filter *filter; |
| 3318 | size_t l, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3319 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3320 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3321 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3322 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3323 | offset = co_data(chn); |
| 3324 | len = ci_data(chn); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3325 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3326 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3327 | if (filter && !hlua_filter_from_payload(filter)) |
| 3328 | WILL_LJMP(lua_error(L)); |
| 3329 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3330 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3331 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3332 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3333 | } |
| 3334 | |
| 3335 | for (l = 0; l < len; l++) { |
| 3336 | if (*(b_peek(&chn->buf, offset+l)) == '\n') { |
| 3337 | len = l+1; |
| 3338 | goto dup; |
| 3339 | } |
| 3340 | } |
| 3341 | |
| 3342 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3343 | /* Yield waiting for more data */ |
| 3344 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0)); |
| 3345 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3346 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3347 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3348 | ret = _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3349 | _hlua_channel_delete(chn, offset, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3350 | return 1; |
| 3351 | } |
| 3352 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3353 | /* [ DEPRECATED ] |
| 3354 | * |
| 3355 | * Check arguments for the function "hlua_channel_getline_yield". |
| 3356 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3357 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3358 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3359 | struct channel *chn; |
| 3360 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3361 | MAY_LJMP(check_args(L, 1, "getline")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3362 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3363 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3364 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3365 | WILL_LJMP(lua_error(L)); |
| 3366 | } |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3367 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3368 | } |
| 3369 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3370 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3371 | * available input data are returned. The offset may be negactive to start from |
| 3372 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3373 | * size. |
| 3374 | */ |
| 3375 | __LJMP static int hlua_channel_get_data(lua_State *L) |
| 3376 | { |
| 3377 | struct channel *chn; |
| 3378 | |
| 3379 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3380 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 3381 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3382 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3383 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3384 | WILL_LJMP(lua_error(L)); |
| 3385 | } |
| 3386 | return MAY_LJMP(hlua_channel_get_data_yield(L, 0, 0)); |
| 3387 | } |
| 3388 | |
| 3389 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3390 | * available input data are returned. The offset may be negactive to start from |
| 3391 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3392 | * size. |
| 3393 | */ |
| 3394 | __LJMP static int hlua_channel_get_line(lua_State *L) |
| 3395 | { |
| 3396 | struct channel *chn; |
| 3397 | |
| 3398 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3399 | WILL_LJMP(luaL_error(L, "'line' expects at most 2 arguments")); |
| 3400 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3401 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3402 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3403 | WILL_LJMP(lua_error(L)); |
| 3404 | } |
| 3405 | return MAY_LJMP(hlua_channel_get_line_yield(L, 0, 0)); |
| 3406 | } |
| 3407 | |
| 3408 | /* Appends a string into the input side of channel. It returns the length of the |
| 3409 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3410 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3411 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3412 | * |
| 3413 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3414 | */ |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3415 | __LJMP static int hlua_channel_append(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3416 | { |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3417 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3418 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3419 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3420 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3421 | int ret; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3422 | |
| 3423 | MAY_LJMP(check_args(L, 2, "append")); |
| 3424 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3425 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3426 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3427 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3428 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3429 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3430 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3431 | offset = co_data(chn); |
| 3432 | len = ci_data(chn); |
| 3433 | |
| 3434 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3435 | if (filter && !hlua_filter_from_payload(filter)) |
| 3436 | WILL_LJMP(lua_error(L)); |
| 3437 | |
| 3438 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3439 | if (ret > 0 && filter) { |
| 3440 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3441 | |
| 3442 | flt_update_offsets(filter, chn, ret); |
| 3443 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3444 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3445 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3446 | return 1; |
| 3447 | } |
| 3448 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3449 | /* Prepends a string into the input side of channel. It returns the length of the |
| 3450 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3451 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3452 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3453 | * |
| 3454 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3455 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3456 | __LJMP static int hlua_channel_prepend(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3457 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3458 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3459 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3460 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3461 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3462 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3463 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3464 | MAY_LJMP(check_args(L, 2, "prepend")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3465 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3466 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3467 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3468 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3469 | WILL_LJMP(lua_error(L)); |
| 3470 | } |
| 3471 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3472 | offset = co_data(chn); |
| 3473 | len = ci_data(chn); |
| 3474 | |
| 3475 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3476 | if (filter && !hlua_filter_from_payload(filter)) |
| 3477 | WILL_LJMP(lua_error(L)); |
| 3478 | |
| 3479 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3480 | if (ret > 0 && filter) { |
| 3481 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3482 | |
| 3483 | flt_update_offsets(filter, chn, ret); |
| 3484 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3485 | } |
| 3486 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3487 | lua_pushinteger(L, ret); |
| 3488 | return 1; |
| 3489 | } |
| 3490 | |
| 3491 | /* Inserts a given amount of input data at the given offset by a string |
| 3492 | * content. By default the string is appended at the end of input data. It |
| 3493 | * returns the length of the written string, or -1 if the channel is closed or |
| 3494 | * if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3495 | * |
| 3496 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3497 | */ |
| 3498 | __LJMP static int hlua_channel_insert_data(lua_State *L) |
| 3499 | { |
| 3500 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3501 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3502 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3503 | size_t sz, input, output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3504 | int ret, offset; |
| 3505 | |
| 3506 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 3507 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
| 3508 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3509 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3510 | |
| 3511 | output = co_data(chn); |
| 3512 | input = ci_data(chn); |
| 3513 | |
| 3514 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3515 | if (filter && !hlua_filter_from_payload(filter)) |
| 3516 | WILL_LJMP(lua_error(L)); |
| 3517 | |
| 3518 | offset = input + output; |
| 3519 | if (lua_gettop(L) > 2) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3520 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3521 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3522 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3523 | offset += output; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3524 | if (offset < output || offset > output + input) { |
| 3525 | lua_pushfstring(L, "offset out of range."); |
| 3526 | WILL_LJMP(lua_error(L)); |
| 3527 | } |
| 3528 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3529 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3530 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3531 | WILL_LJMP(lua_error(L)); |
| 3532 | } |
| 3533 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3534 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3535 | if (ret > 0 && filter) { |
| 3536 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3537 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3538 | flt_update_offsets(filter, chn, ret); |
| 3539 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3540 | } |
| 3541 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3542 | lua_pushinteger(L, ret); |
| 3543 | return 1; |
| 3544 | } |
| 3545 | /* Replaces a given amount of input data at the given offset by a string |
| 3546 | * content. By default all remaining data are removed (offset = 0 and len = |
| 3547 | * -1). It returns the length of the written string, or -1 if the channel is |
| 3548 | * closed or if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3549 | * |
| 3550 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3551 | */ |
| 3552 | __LJMP static int hlua_channel_set_data(lua_State *L) |
| 3553 | { |
| 3554 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3555 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3556 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3557 | size_t sz, input, output; |
| 3558 | int ret, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3559 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3560 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 3561 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 3562 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3563 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3564 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3565 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3566 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3567 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3568 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3569 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3570 | output = co_data(chn); |
| 3571 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3572 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3573 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3574 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3575 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3576 | |
| 3577 | offset = output; |
| 3578 | if (lua_gettop(L) > 2) { |
| 3579 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3580 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3581 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3582 | offset += output; |
| 3583 | if (offset < output || offset > input + output) { |
| 3584 | lua_pushfstring(L, "offset out of range."); |
| 3585 | WILL_LJMP(lua_error(L)); |
| 3586 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3587 | } |
| 3588 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3589 | len = output + input - offset; |
| 3590 | if (lua_gettop(L) == 4) { |
| 3591 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 3592 | if (!len) |
| 3593 | goto set; |
| 3594 | if (len == -1) |
| 3595 | len = output + input - offset; |
| 3596 | if (len < 0 || offset + len > output + input) { |
| 3597 | lua_pushfstring(L, "length out of range."); |
| 3598 | WILL_LJMP(lua_error(L)); |
| 3599 | } |
| 3600 | } |
| 3601 | |
| 3602 | set: |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3603 | /* 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] | 3604 | if (sz > c_room(chn) + len) |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3605 | lua_pushinteger(L, -1); |
| 3606 | else { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3607 | _hlua_channel_delete(chn, offset, len); |
| 3608 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3609 | if (filter) { |
| 3610 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3611 | |
| 3612 | len -= (ret > 0 ? ret : 0); |
| 3613 | flt_update_offsets(filter, chn, -len); |
| 3614 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3615 | } |
| 3616 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3617 | lua_pushinteger(L, ret); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3618 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3619 | return 1; |
| 3620 | } |
| 3621 | |
| 3622 | /* 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] | 3623 | * 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] | 3624 | * the removed data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3625 | * |
| 3626 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3627 | */ |
| 3628 | __LJMP static int hlua_channel_del_data(lua_State *L) |
| 3629 | { |
| 3630 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3631 | struct filter *filter; |
| 3632 | size_t input, output; |
| 3633 | int offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3634 | |
| 3635 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3636 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
| 3637 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3638 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3639 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3640 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3641 | WILL_LJMP(lua_error(L)); |
| 3642 | } |
| 3643 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3644 | output = co_data(chn); |
| 3645 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3646 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3647 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3648 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3649 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3650 | |
| 3651 | offset = output; |
| 3652 | if (lua_gettop(L) > 2) { |
| 3653 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3654 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3655 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3656 | offset += output; |
| 3657 | if (offset < output || offset > input + output) { |
| 3658 | lua_pushfstring(L, "offset out of range."); |
| 3659 | WILL_LJMP(lua_error(L)); |
| 3660 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3661 | } |
| 3662 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3663 | len = output + input - offset; |
| 3664 | if (lua_gettop(L) == 4) { |
| 3665 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 3666 | if (!len) |
| 3667 | goto end; |
| 3668 | if (len == -1) |
| 3669 | len = output + input - offset; |
| 3670 | if (len < 0 || offset + len > output + input) { |
| 3671 | lua_pushfstring(L, "length out of range."); |
| 3672 | WILL_LJMP(lua_error(L)); |
| 3673 | } |
| 3674 | } |
| 3675 | |
| 3676 | _hlua_channel_delete(chn, offset, len); |
| 3677 | if (filter) { |
| 3678 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3679 | |
| 3680 | flt_update_offsets(filter, chn, -len); |
| 3681 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3682 | } |
| 3683 | |
| 3684 | end: |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3685 | lua_pushinteger(L, len); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3686 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3687 | } |
| 3688 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3689 | /* Append data in the output side of the buffer. This data is immediately |
| 3690 | * sent. The function returns the amount of data written. If the buffer |
| 3691 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3692 | * if the channel is closed. |
| 3693 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3694 | __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] | 3695 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3696 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3697 | struct filter *filter; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3698 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3699 | size_t offset, len, sz; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3700 | int l, ret; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3701 | struct hlua *hlua; |
| 3702 | |
| 3703 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3704 | hlua = hlua_gethlua(L); |
| 3705 | if (!hlua) { |
| 3706 | lua_pushnil(L); |
| 3707 | return 1; |
| 3708 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3709 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3710 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3711 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3712 | l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3713 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3714 | offset = co_data(chn); |
| 3715 | len = ci_data(chn); |
| 3716 | |
| 3717 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3718 | if (filter && !hlua_filter_from_payload(filter)) |
| 3719 | WILL_LJMP(lua_error(L)); |
| 3720 | |
| 3721 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3722 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3723 | lua_pushinteger(L, -1); |
| 3724 | return 1; |
| 3725 | } |
| 3726 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3727 | len = c_room(chn); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3728 | if (len > sz -l) { |
| 3729 | if (filter) { |
| 3730 | lua_pushinteger(L, -1); |
| 3731 | return 1; |
| 3732 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3733 | len = sz - l; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3734 | } |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3735 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3736 | ret = _hlua_channel_insert(chn, L, ist2(str, len), offset); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3737 | if (ret == -1) { |
| 3738 | lua_pop(L, 1); |
| 3739 | lua_pushinteger(L, -1); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3740 | return 1; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3741 | } |
| 3742 | if (ret) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3743 | if (filter) { |
| 3744 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3745 | |
| 3746 | |
| 3747 | flt_update_offsets(filter, chn, ret); |
| 3748 | FLT_OFF(filter, chn) += ret; |
| 3749 | flt_ctx->cur_off[CHN_IDX(chn)] += ret; |
| 3750 | } |
| 3751 | else |
| 3752 | c_adv(chn, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3753 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3754 | l += ret; |
| 3755 | lua_pop(L, 1); |
| 3756 | lua_pushinteger(L, l); |
| 3757 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3758 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3759 | if (l < sz) { |
| 3760 | /* Yield only if the channel's output is not empty. |
| 3761 | * Otherwise it means we cannot add more data. */ |
| 3762 | if (co_data(chn) == 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3763 | return 1; |
| 3764 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3765 | /* If we are waiting for space in the response buffer, we |
| 3766 | * must set the flag WAKERESWR. This flag required the task |
| 3767 | * wake up if any activity is detected on the response buffer. |
| 3768 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3769 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3770 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3771 | else |
| 3772 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3773 | 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] | 3774 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3775 | |
| 3776 | return 1; |
| 3777 | } |
| 3778 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3779 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3780 | * yield the LUA process, and resume it without checking the |
| 3781 | * input arguments. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3782 | * |
| 3783 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3784 | */ |
| 3785 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3786 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3787 | struct channel *chn; |
| 3788 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3789 | MAY_LJMP(check_args(L, 2, "send")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3790 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3791 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3792 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3793 | WILL_LJMP(lua_error(L)); |
| 3794 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3795 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3796 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3797 | } |
| 3798 | |
| 3799 | /* This function forward and amount of butes. The data pass from |
| 3800 | * the input side of the buffer to the output side, and can be |
| 3801 | * forwarded. This function never fails. |
| 3802 | * |
| 3803 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3804 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3805 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3806 | __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] | 3807 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3808 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3809 | struct filter *filter; |
| 3810 | size_t offset, len, fwd; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3811 | int l, max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3812 | struct hlua *hlua; |
| 3813 | |
| 3814 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3815 | hlua = hlua_gethlua(L); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3816 | if (!hlua) { |
| 3817 | lua_pushnil(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3818 | return 1; |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3819 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3820 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3821 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3822 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3823 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3824 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3825 | offset = co_data(chn); |
| 3826 | len = ci_data(chn); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3827 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3828 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3829 | if (filter && !hlua_filter_from_payload(filter)) |
| 3830 | WILL_LJMP(lua_error(L)); |
| 3831 | |
| 3832 | max = fwd - l; |
| 3833 | if (max > len) |
| 3834 | max = len; |
| 3835 | |
| 3836 | if (filter) { |
| 3837 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3838 | |
| 3839 | FLT_OFF(filter, chn) += max; |
| 3840 | flt_ctx->cur_off[CHN_IDX(chn)] += max; |
| 3841 | flt_ctx->cur_len[CHN_IDX(chn)] -= max; |
| 3842 | } |
| 3843 | else |
| 3844 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3845 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3846 | l += max; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3847 | lua_pop(L, 1); |
| 3848 | lua_pushinteger(L, l); |
| 3849 | |
| 3850 | /* Check if it miss bytes to forward. */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3851 | if (l < fwd) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3852 | /* The the input channel or the output channel are closed, we |
| 3853 | * must return the amount of data forwarded. |
| 3854 | */ |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3855 | 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] | 3856 | return 1; |
| 3857 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3858 | /* If we are waiting for space data in the response buffer, we |
| 3859 | * must set the flag WAKERESWR. This flag required the task |
| 3860 | * wake up if any activity is detected on the response buffer. |
| 3861 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3862 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3863 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3864 | else |
| 3865 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3866 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3867 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3868 | 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] | 3869 | } |
| 3870 | |
| 3871 | return 1; |
| 3872 | } |
| 3873 | |
| 3874 | /* Just check the input and prepare the stack for the previous |
| 3875 | * function "hlua_channel_forward_yield" |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3876 | * |
| 3877 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3878 | */ |
| 3879 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3880 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3881 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3882 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3883 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3884 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3885 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3886 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3887 | WILL_LJMP(lua_error(L)); |
| 3888 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3889 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3890 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3891 | } |
| 3892 | |
| 3893 | /* Just returns the number of bytes available in the input |
| 3894 | * side of the buffer. This function never fails. |
| 3895 | */ |
| 3896 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3897 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3898 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3899 | struct filter *filter; |
| 3900 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3901 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3902 | MAY_LJMP(check_args(L, 1, "input")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3903 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3904 | |
| 3905 | output = co_data(chn); |
| 3906 | input = ci_data(chn); |
| 3907 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3908 | if (filter || !IS_HTX_STRM(chn_strm(chn))) |
| 3909 | lua_pushinteger(L, input); |
| 3910 | else { |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3911 | struct htx *htx = htxbuf(&chn->buf); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3912 | |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3913 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3914 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3915 | return 1; |
| 3916 | } |
| 3917 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3918 | /* Returns true if the channel is full. */ |
| 3919 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3920 | { |
| 3921 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3922 | |
| 3923 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3924 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3925 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3926 | * applet). |
| 3927 | */ |
| 3928 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3929 | return 1; |
| 3930 | } |
| 3931 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3932 | /* Returns true if the channel may still receive data. */ |
| 3933 | __LJMP static int hlua_channel_may_recv(lua_State *L) |
| 3934 | { |
| 3935 | struct channel *chn; |
| 3936 | |
| 3937 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 3938 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3939 | lua_pushboolean(L, (!channel_input_closed(chn) && channel_may_recv(chn))); |
| 3940 | return 1; |
| 3941 | } |
| 3942 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3943 | /* Returns true if the channel is the response channel. */ |
| 3944 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3945 | { |
| 3946 | struct channel *chn; |
| 3947 | |
| 3948 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3949 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3950 | |
| 3951 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3952 | return 1; |
| 3953 | } |
| 3954 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3955 | /* Just returns the number of bytes available in the output |
| 3956 | * side of the buffer. This function never fails. |
| 3957 | */ |
| 3958 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3959 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3960 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3961 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3962 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3963 | MAY_LJMP(check_args(L, 1, "output")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3964 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3965 | |
| 3966 | output = co_data(chn); |
| 3967 | input = ci_data(chn); |
| 3968 | hlua_channel_filter(L, 1, chn, &output, &input); |
| 3969 | |
| 3970 | lua_pushinteger(L, output); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3971 | return 1; |
| 3972 | } |
| 3973 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3974 | /* |
| 3975 | * |
| 3976 | * |
| 3977 | * Class Fetches |
| 3978 | * |
| 3979 | * |
| 3980 | */ |
| 3981 | |
| 3982 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3983 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3984 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3985 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3986 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3987 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3988 | } |
| 3989 | |
| 3990 | /* This function creates and push in the stack a fetch object according |
| 3991 | * with a current TXN. |
| 3992 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3993 | 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] | 3994 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3995 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3996 | |
| 3997 | /* Check stack size. */ |
| 3998 | if (!lua_checkstack(L, 3)) |
| 3999 | return 0; |
| 4000 | |
| 4001 | /* Create the object: obj[0] = userdata. |
| 4002 | * Note that the base of the Fetches object is the |
| 4003 | * transaction object. |
| 4004 | */ |
| 4005 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4006 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4007 | lua_rawseti(L, -2, 0); |
| 4008 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4009 | hsmp->s = txn->s; |
| 4010 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4011 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4012 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4013 | |
| 4014 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 4015 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 4016 | lua_setmetatable(L, -2); |
| 4017 | |
| 4018 | return 1; |
| 4019 | } |
| 4020 | |
| 4021 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 4022 | * It uses closure argument to store the associated sample-fetch. It |
| 4023 | * returns only one argument or throws an error. An error is thrown |
| 4024 | * only if an error is encountered during the argument parsing. If |
| 4025 | * the "sample-fetch" function fails, nil is returned. |
| 4026 | */ |
| 4027 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 4028 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4029 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4030 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4031 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4032 | int i; |
| 4033 | struct sample smp; |
| 4034 | |
| 4035 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4036 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4037 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4038 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4039 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4040 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 4041 | /* Check execution authorization. */ |
| 4042 | if (f->use & SMP_USE_HTTP_ANY && |
| 4043 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 4044 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 4045 | "is not available in Lua services", f->kw); |
| 4046 | WILL_LJMP(lua_error(L)); |
| 4047 | } |
| 4048 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4049 | /* Get extra arguments. */ |
| 4050 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 4051 | if (i >= ARGM_NBARGS) |
| 4052 | break; |
| 4053 | hlua_lua2arg(L, i + 2, &args[i]); |
| 4054 | } |
| 4055 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4056 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4057 | |
| 4058 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4059 | 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] | 4060 | |
| 4061 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4062 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4063 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4064 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4065 | } |
| 4066 | |
| 4067 | /* Initialise the sample. */ |
| 4068 | memset(&smp, 0, sizeof(smp)); |
| 4069 | |
| 4070 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4071 | 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] | 4072 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4073 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4074 | lua_pushstring(L, ""); |
| 4075 | else |
| 4076 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4077 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4078 | } |
| 4079 | |
| 4080 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4081 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4082 | hlua_smp2lua_str(L, &smp); |
| 4083 | else |
| 4084 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4085 | |
| 4086 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4087 | free_args(args); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4088 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4089 | |
| 4090 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4091 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4092 | WILL_LJMP(lua_error(L)); |
| 4093 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4094 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4095 | |
| 4096 | /* |
| 4097 | * |
| 4098 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4099 | * Class Converters |
| 4100 | * |
| 4101 | * |
| 4102 | */ |
| 4103 | |
| 4104 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4105 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4106 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4107 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4108 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4109 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4110 | } |
| 4111 | |
| 4112 | /* This function creates and push in the stack a Converters object |
| 4113 | * according with a current TXN. |
| 4114 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4115 | 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] | 4116 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4117 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4118 | |
| 4119 | /* Check stack size. */ |
| 4120 | if (!lua_checkstack(L, 3)) |
| 4121 | return 0; |
| 4122 | |
| 4123 | /* Create the object: obj[0] = userdata. |
| 4124 | * Note that the base of the Converters object is the |
| 4125 | * same than the TXN object. |
| 4126 | */ |
| 4127 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4128 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4129 | lua_rawseti(L, -2, 0); |
| 4130 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4131 | hsmp->s = txn->s; |
| 4132 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4133 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4134 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4135 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4136 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4137 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 4138 | lua_setmetatable(L, -2); |
| 4139 | |
| 4140 | return 1; |
| 4141 | } |
| 4142 | |
| 4143 | /* This function is an LUA binding. It is called with each converter. |
| 4144 | * It uses closure argument to store the associated converter. It |
| 4145 | * returns only one argument or throws an error. An error is thrown |
| 4146 | * only if an error is encountered during the argument parsing. If |
| 4147 | * the converter function function fails, nil is returned. |
| 4148 | */ |
| 4149 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 4150 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4151 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4152 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4153 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4154 | int i; |
| 4155 | struct sample smp; |
| 4156 | |
| 4157 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4158 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4159 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4160 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4161 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4162 | |
| 4163 | /* Get extra arguments. */ |
| 4164 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 4165 | if (i >= ARGM_NBARGS) |
| 4166 | break; |
| 4167 | hlua_lua2arg(L, i + 3, &args[i]); |
| 4168 | } |
| 4169 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4170 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4171 | |
| 4172 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4173 | 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] | 4174 | |
| 4175 | /* Run the special args checker. */ |
| 4176 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 4177 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4178 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4179 | } |
| 4180 | |
| 4181 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4182 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4183 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 4184 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4185 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4186 | } |
| 4187 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4188 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 4189 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4190 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4191 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4192 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4193 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4194 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4195 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4196 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 4197 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4198 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4199 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4200 | } |
| 4201 | |
| 4202 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4203 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4204 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4205 | lua_pushstring(L, ""); |
| 4206 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4207 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4208 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4209 | } |
| 4210 | |
| 4211 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4212 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4213 | hlua_smp2lua_str(L, &smp); |
| 4214 | else |
| 4215 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4216 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4217 | free_args(args); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4218 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4219 | |
| 4220 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4221 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4222 | WILL_LJMP(lua_error(L)); |
| 4223 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4224 | } |
| 4225 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4226 | /* |
| 4227 | * |
| 4228 | * |
| 4229 | * Class AppletTCP |
| 4230 | * |
| 4231 | * |
| 4232 | */ |
| 4233 | |
| 4234 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4235 | * a class stream, otherwise it throws an error. |
| 4236 | */ |
| 4237 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 4238 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4239 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4240 | } |
| 4241 | |
| 4242 | /* This function creates and push in the stack an Applet object |
| 4243 | * according with a current TXN. |
| 4244 | */ |
| 4245 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 4246 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4247 | struct hlua_appctx *luactx; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4248 | struct stream_interface *si = ctx->owner; |
| 4249 | struct stream *s = si_strm(si); |
| 4250 | struct proxy *p = s->be; |
| 4251 | |
| 4252 | /* Check stack size. */ |
| 4253 | if (!lua_checkstack(L, 3)) |
| 4254 | return 0; |
| 4255 | |
| 4256 | /* Create the object: obj[0] = userdata. |
| 4257 | * Note that the base of the Converters object is the |
| 4258 | * same than the TXN object. |
| 4259 | */ |
| 4260 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4261 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4262 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4263 | luactx->appctx = ctx; |
| 4264 | luactx->htxn.s = s; |
| 4265 | luactx->htxn.p = p; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4266 | |
| 4267 | /* Create the "f" field that contains a list of fetches. */ |
| 4268 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4269 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4270 | return 0; |
| 4271 | lua_settable(L, -3); |
| 4272 | |
| 4273 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4274 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4275 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4276 | return 0; |
| 4277 | lua_settable(L, -3); |
| 4278 | |
| 4279 | /* Create the "c" field that contains a list of converters. */ |
| 4280 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4281 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4282 | return 0; |
| 4283 | lua_settable(L, -3); |
| 4284 | |
| 4285 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4286 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4287 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4288 | return 0; |
| 4289 | lua_settable(L, -3); |
| 4290 | |
| 4291 | /* Pop a class stream metatable and affect it to the table. */ |
| 4292 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 4293 | lua_setmetatable(L, -2); |
| 4294 | |
| 4295 | return 1; |
| 4296 | } |
| 4297 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4298 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 4299 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4300 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4301 | struct stream *s; |
| 4302 | const char *name; |
| 4303 | size_t len; |
| 4304 | struct sample smp; |
| 4305 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4306 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4307 | 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] | 4308 | |
| 4309 | /* It is useles to retrieve the stream, but this function |
| 4310 | * runs only in a stream context. |
| 4311 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4312 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4313 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4314 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4315 | |
| 4316 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4317 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4318 | hlua_lua2smp(L, 3, &smp); |
| 4319 | |
| 4320 | /* Store the sample in a variable. */ |
| 4321 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4322 | |
| 4323 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4324 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4325 | else |
| 4326 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4327 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4328 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4329 | } |
| 4330 | |
| 4331 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 4332 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4333 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4334 | struct stream *s; |
| 4335 | const char *name; |
| 4336 | size_t len; |
| 4337 | struct sample smp; |
| 4338 | |
| 4339 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4340 | |
| 4341 | /* It is useles to retrieve the stream, but this function |
| 4342 | * runs only in a stream context. |
| 4343 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4344 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4345 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4346 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4347 | |
| 4348 | /* Unset the variable. */ |
| 4349 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4350 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4351 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4352 | } |
| 4353 | |
| 4354 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 4355 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4356 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4357 | struct stream *s; |
| 4358 | const char *name; |
| 4359 | size_t len; |
| 4360 | struct sample smp; |
| 4361 | |
| 4362 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4363 | |
| 4364 | /* It is useles to retrieve the stream, but this function |
| 4365 | * runs only in a stream context. |
| 4366 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4367 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4368 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4369 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4370 | |
| 4371 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4372 | if (!vars_get_by_name(name, len, &smp)) { |
| 4373 | lua_pushnil(L); |
| 4374 | return 1; |
| 4375 | } |
| 4376 | |
| 4377 | return hlua_smp2lua(L, &smp); |
| 4378 | } |
| 4379 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4380 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 4381 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4382 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4383 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4384 | struct hlua *hlua; |
| 4385 | |
| 4386 | /* 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] | 4387 | if (!s->hlua) |
| 4388 | return 0; |
| 4389 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4390 | |
| 4391 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4392 | |
| 4393 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4394 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4395 | |
| 4396 | /* Get and store new value. */ |
| 4397 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4398 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4399 | |
| 4400 | return 0; |
| 4401 | } |
| 4402 | |
| 4403 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 4404 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4405 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4406 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4407 | struct hlua *hlua; |
| 4408 | |
| 4409 | /* 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] | 4410 | if (!s->hlua) { |
| 4411 | lua_pushnil(L); |
| 4412 | return 1; |
| 4413 | } |
| 4414 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4415 | |
| 4416 | /* Push configuration index in the stack. */ |
| 4417 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4418 | |
| 4419 | return 1; |
| 4420 | } |
| 4421 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4422 | /* If expected data not yet available, it returns a yield. This function |
| 4423 | * consumes the data in the buffer. It returns a string containing the |
| 4424 | * data. This string can be empty. |
| 4425 | */ |
| 4426 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4427 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4428 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4429 | struct stream_interface *si = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4430 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4431 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4432 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4433 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4434 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4435 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4436 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4437 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4438 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4439 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4440 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4441 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4442 | 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] | 4443 | } |
| 4444 | |
| 4445 | /* End of data: commit the total strings and return. */ |
| 4446 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4447 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4448 | return 1; |
| 4449 | } |
| 4450 | |
| 4451 | /* Ensure that the block 2 length is usable. */ |
| 4452 | if (ret == 1) |
| 4453 | len2 = 0; |
| 4454 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4455 | /* don't check the max length read and don't check. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4456 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4457 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4458 | |
| 4459 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4460 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4461 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4462 | return 1; |
| 4463 | } |
| 4464 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4465 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4466 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 4467 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4468 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4469 | |
| 4470 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4471 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4472 | |
| 4473 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 4474 | } |
| 4475 | |
| 4476 | /* If expected data not yet available, it returns a yield. This function |
| 4477 | * consumes the data in the buffer. It returns a string containing the |
| 4478 | * data. This string can be empty. |
| 4479 | */ |
| 4480 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4481 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4482 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4483 | struct stream_interface *si = luactx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4484 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4485 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4486 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4487 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4488 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4489 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4490 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4491 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4492 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4493 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4494 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4495 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4496 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4497 | 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] | 4498 | } |
| 4499 | |
| 4500 | /* End of data: commit the total strings and return. */ |
| 4501 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4502 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4503 | return 1; |
| 4504 | } |
| 4505 | |
| 4506 | /* Ensure that the block 2 length is usable. */ |
| 4507 | if (ret == 1) |
| 4508 | len2 = 0; |
| 4509 | |
| 4510 | if (len == -1) { |
| 4511 | |
| 4512 | /* If len == -1, catenate all the data avalaile and |
| 4513 | * yield because we want to get all the data until |
| 4514 | * the end of data stream. |
| 4515 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4516 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4517 | luaL_addlstring(&luactx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4518 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4519 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4520 | 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] | 4521 | |
| 4522 | } else { |
| 4523 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4524 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4525 | if (len1 > len) |
| 4526 | len1 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4527 | luaL_addlstring(&luactx->b, blk1, len1); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4528 | len -= len1; |
| 4529 | |
| 4530 | /* Copy the second block. */ |
| 4531 | if (len2 > len) |
| 4532 | len2 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4533 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4534 | len -= len2; |
| 4535 | |
| 4536 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4537 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4538 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4539 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4540 | if (len > 0) { |
| 4541 | lua_pushinteger(L, len); |
| 4542 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4543 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4544 | 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] | 4545 | } |
| 4546 | |
| 4547 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4548 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4549 | return 1; |
| 4550 | } |
| 4551 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4552 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4553 | hlua_pusherror(L, "Lua: internal error"); |
| 4554 | WILL_LJMP(lua_error(L)); |
| 4555 | return 0; |
| 4556 | } |
| 4557 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4558 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4559 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 4560 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4561 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4562 | int len = -1; |
| 4563 | |
| 4564 | if (lua_gettop(L) > 2) |
| 4565 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4566 | if (lua_gettop(L) >= 2) { |
| 4567 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4568 | lua_pop(L, 1); |
| 4569 | } |
| 4570 | |
| 4571 | /* Confirm or set the required length */ |
| 4572 | lua_pushinteger(L, len); |
| 4573 | |
| 4574 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4575 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4576 | |
| 4577 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4578 | } |
| 4579 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4580 | /* Append data in the output side of the buffer. This data is immediately |
| 4581 | * sent. The function returns the amount of data written. If the buffer |
| 4582 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4583 | * if the channel is closed. |
| 4584 | */ |
| 4585 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4586 | { |
| 4587 | size_t len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4588 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4589 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4590 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4591 | struct stream_interface *si = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4592 | struct channel *chn = si_ic(si); |
| 4593 | int max; |
| 4594 | |
| 4595 | /* Get the max amount of data which can write as input in the channel. */ |
| 4596 | max = channel_recv_max(chn); |
| 4597 | if (max > (len - l)) |
| 4598 | max = len - l; |
| 4599 | |
| 4600 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4601 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4602 | |
| 4603 | /* update counters. */ |
| 4604 | l += max; |
| 4605 | lua_pop(L, 1); |
| 4606 | lua_pushinteger(L, l); |
| 4607 | |
| 4608 | /* If some data is not send, declares the situation to the |
| 4609 | * applet, and returns a yield. |
| 4610 | */ |
| 4611 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4612 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4613 | 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] | 4614 | } |
| 4615 | |
| 4616 | return 1; |
| 4617 | } |
| 4618 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4619 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4620 | * yield the LUA process, and resume it without checking the |
| 4621 | * input arguments. |
| 4622 | */ |
| 4623 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4624 | { |
| 4625 | MAY_LJMP(check_args(L, 2, "send")); |
| 4626 | lua_pushinteger(L, 0); |
| 4627 | |
| 4628 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4629 | } |
| 4630 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4631 | /* |
| 4632 | * |
| 4633 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4634 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4635 | * |
| 4636 | * |
| 4637 | */ |
| 4638 | |
| 4639 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4640 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4641 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4642 | __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] | 4643 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4644 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4645 | } |
| 4646 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4647 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4648 | * according with a current TXN. |
| 4649 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4650 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4651 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4652 | struct hlua_appctx *luactx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4653 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4654 | struct stream_interface *si = ctx->owner; |
| 4655 | struct stream *s = si_strm(si); |
| 4656 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4657 | struct htx *htx; |
| 4658 | struct htx_blk *blk; |
| 4659 | struct htx_sl *sl; |
| 4660 | struct ist path; |
| 4661 | unsigned long long len = 0; |
| 4662 | int32_t pos; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4663 | struct http_uri_parser parser; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4664 | |
| 4665 | /* Check stack size. */ |
| 4666 | if (!lua_checkstack(L, 3)) |
| 4667 | return 0; |
| 4668 | |
| 4669 | /* Create the object: obj[0] = userdata. |
| 4670 | * Note that the base of the Converters object is the |
| 4671 | * same than the TXN object. |
| 4672 | */ |
| 4673 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4674 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4675 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4676 | luactx->appctx = ctx; |
| 4677 | luactx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
| 4678 | luactx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */ |
| 4679 | luactx->htxn.s = s; |
| 4680 | luactx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4681 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4682 | /* Create the "f" field that contains a list of fetches. */ |
| 4683 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4684 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4685 | return 0; |
| 4686 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4687 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4688 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4689 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4690 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4691 | return 0; |
| 4692 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4693 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4694 | /* Create the "c" field that contains a list of converters. */ |
| 4695 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4696 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4697 | return 0; |
| 4698 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4699 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4700 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4701 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4702 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4703 | return 0; |
| 4704 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4705 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4706 | htx = htxbuf(&s->req.buf); |
| 4707 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4708 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4709 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4710 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4711 | /* Stores the request method. */ |
| 4712 | lua_pushstring(L, "method"); |
| 4713 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4714 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4715 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4716 | /* Stores the http version. */ |
| 4717 | lua_pushstring(L, "version"); |
| 4718 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4719 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4720 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4721 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4722 | * the array on the top of the stack. |
| 4723 | */ |
| 4724 | lua_pushstring(L, "headers"); |
| 4725 | htxn.s = s; |
| 4726 | htxn.p = px; |
| 4727 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4728 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4729 | return 0; |
| 4730 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4731 | |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4732 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 4733 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4734 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4735 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4736 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4737 | p = path.ptr; |
| 4738 | end = path.ptr + path.len; |
| 4739 | q = p; |
| 4740 | while (q < end && *q != '?') |
| 4741 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4742 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4743 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4744 | lua_pushstring(L, "path"); |
| 4745 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4746 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4747 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4748 | /* Stores the query string. */ |
| 4749 | lua_pushstring(L, "qs"); |
| 4750 | if (*q == '?') |
| 4751 | q++; |
| 4752 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4753 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4754 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4755 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4756 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4757 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4758 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4759 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4760 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4761 | break; |
| 4762 | if (type == HTX_BLK_DATA) |
| 4763 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4764 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4765 | if (htx->extra != ULLONG_MAX) |
| 4766 | len += htx->extra; |
| 4767 | |
| 4768 | /* Stores the request path. */ |
| 4769 | lua_pushstring(L, "length"); |
| 4770 | lua_pushinteger(L, len); |
| 4771 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4772 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4773 | /* Create an empty array of HTTP request headers. */ |
| 4774 | lua_pushstring(L, "response"); |
| 4775 | lua_newtable(L); |
| 4776 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4777 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4778 | /* Pop a class stream metatable and affect it to the table. */ |
| 4779 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4780 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4781 | |
| 4782 | return 1; |
| 4783 | } |
| 4784 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4785 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4786 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4787 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4788 | struct stream *s; |
| 4789 | const char *name; |
| 4790 | size_t len; |
| 4791 | struct sample smp; |
| 4792 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4793 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4794 | 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] | 4795 | |
| 4796 | /* It is useles to retrieve the stream, but this function |
| 4797 | * runs only in a stream context. |
| 4798 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4799 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4800 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4801 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4802 | |
| 4803 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4804 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4805 | hlua_lua2smp(L, 3, &smp); |
| 4806 | |
| 4807 | /* Store the sample in a variable. */ |
| 4808 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4809 | |
| 4810 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4811 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4812 | else |
| 4813 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4814 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4815 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4816 | } |
| 4817 | |
| 4818 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4819 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4820 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4821 | struct stream *s; |
| 4822 | const char *name; |
| 4823 | size_t len; |
| 4824 | struct sample smp; |
| 4825 | |
| 4826 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4827 | |
| 4828 | /* It is useles to retrieve the stream, but this function |
| 4829 | * runs only in a stream context. |
| 4830 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4831 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4832 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4833 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4834 | |
| 4835 | /* Unset the variable. */ |
| 4836 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4837 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4838 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4839 | } |
| 4840 | |
| 4841 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4842 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4843 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4844 | struct stream *s; |
| 4845 | const char *name; |
| 4846 | size_t len; |
| 4847 | struct sample smp; |
| 4848 | |
| 4849 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4850 | |
| 4851 | /* It is useles to retrieve the stream, but this function |
| 4852 | * runs only in a stream context. |
| 4853 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4854 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4855 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4856 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4857 | |
| 4858 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4859 | if (!vars_get_by_name(name, len, &smp)) { |
| 4860 | lua_pushnil(L); |
| 4861 | return 1; |
| 4862 | } |
| 4863 | |
| 4864 | return hlua_smp2lua(L, &smp); |
| 4865 | } |
| 4866 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4867 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4868 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4869 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4870 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4871 | struct hlua *hlua; |
| 4872 | |
| 4873 | /* 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] | 4874 | if (!s->hlua) |
| 4875 | return 0; |
| 4876 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4877 | |
| 4878 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4879 | |
| 4880 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4881 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4882 | |
| 4883 | /* Get and store new value. */ |
| 4884 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4885 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4886 | |
| 4887 | return 0; |
| 4888 | } |
| 4889 | |
| 4890 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4891 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4892 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4893 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4894 | struct hlua *hlua; |
| 4895 | |
| 4896 | /* 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] | 4897 | if (!s->hlua) { |
| 4898 | lua_pushnil(L); |
| 4899 | return 1; |
| 4900 | } |
| 4901 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4902 | |
| 4903 | /* Push configuration index in the stack. */ |
| 4904 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4905 | |
| 4906 | return 1; |
| 4907 | } |
| 4908 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4909 | /* If expected data not yet available, it returns a yield. This function |
| 4910 | * consumes the data in the buffer. It returns a string containing the |
| 4911 | * data. This string can be empty. |
| 4912 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4913 | __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] | 4914 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4915 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4916 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4917 | struct channel *req = si_oc(si); |
| 4918 | struct htx *htx; |
| 4919 | struct htx_blk *blk; |
| 4920 | size_t count; |
| 4921 | int stop = 0; |
| 4922 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4923 | htx = htx_from_buf(&req->buf); |
| 4924 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4925 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4926 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4927 | while (count && !stop && blk) { |
| 4928 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4929 | uint32_t sz = htx_get_blksz(blk); |
| 4930 | struct ist v; |
| 4931 | uint32_t vlen; |
| 4932 | char *nl; |
| 4933 | |
| 4934 | vlen = sz; |
| 4935 | if (vlen > count) { |
| 4936 | if (type != HTX_BLK_DATA) |
| 4937 | break; |
| 4938 | vlen = count; |
| 4939 | } |
| 4940 | |
| 4941 | switch (type) { |
| 4942 | case HTX_BLK_UNUSED: |
| 4943 | break; |
| 4944 | |
| 4945 | case HTX_BLK_DATA: |
| 4946 | v = htx_get_blk_value(htx, blk); |
| 4947 | v.len = vlen; |
| 4948 | nl = istchr(v, '\n'); |
| 4949 | if (nl != NULL) { |
| 4950 | stop = 1; |
| 4951 | vlen = nl - v.ptr + 1; |
| 4952 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4953 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4954 | break; |
| 4955 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4956 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4957 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4958 | stop = 1; |
| 4959 | break; |
| 4960 | |
| 4961 | default: |
| 4962 | break; |
| 4963 | } |
| 4964 | |
| 4965 | co_set_data(req, co_data(req) - vlen); |
| 4966 | count -= vlen; |
| 4967 | if (sz == vlen) |
| 4968 | blk = htx_remove_blk(htx, blk); |
| 4969 | else { |
| 4970 | htx_cut_data_blk(htx, blk, vlen); |
| 4971 | break; |
| 4972 | } |
| 4973 | } |
| 4974 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4975 | /* The message was fully consumed and no more data are expected |
| 4976 | * (EOM flag set). |
| 4977 | */ |
| 4978 | if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) |
| 4979 | stop = 1; |
| 4980 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4981 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4982 | if (!stop) { |
| 4983 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4984 | 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] | 4985 | } |
| 4986 | |
| 4987 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4988 | luaL_pushresult(&luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4989 | return 1; |
| 4990 | } |
| 4991 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4992 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4993 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4994 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4995 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4996 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4997 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4998 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4999 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5000 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5001 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5002 | } |
| 5003 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5004 | /* If expected data not yet available, it returns a yield. This function |
| 5005 | * consumes the data in the buffer. It returns a string containing the |
| 5006 | * data. This string can be empty. |
| 5007 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5008 | __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] | 5009 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5010 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5011 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5012 | struct channel *req = si_oc(si); |
| 5013 | struct htx *htx; |
| 5014 | struct htx_blk *blk; |
| 5015 | size_t count; |
| 5016 | int len; |
| 5017 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5018 | htx = htx_from_buf(&req->buf); |
| 5019 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5020 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 5021 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5022 | while (count && len && blk) { |
| 5023 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5024 | uint32_t sz = htx_get_blksz(blk); |
| 5025 | struct ist v; |
| 5026 | uint32_t vlen; |
| 5027 | |
| 5028 | vlen = sz; |
| 5029 | if (len > 0 && vlen > len) |
| 5030 | vlen = len; |
| 5031 | if (vlen > count) { |
| 5032 | if (type != HTX_BLK_DATA) |
| 5033 | break; |
| 5034 | vlen = count; |
| 5035 | } |
| 5036 | |
| 5037 | switch (type) { |
| 5038 | case HTX_BLK_UNUSED: |
| 5039 | break; |
| 5040 | |
| 5041 | case HTX_BLK_DATA: |
| 5042 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5043 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5044 | break; |
| 5045 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5046 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5047 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5048 | len = 0; |
| 5049 | break; |
| 5050 | |
| 5051 | default: |
| 5052 | break; |
| 5053 | } |
| 5054 | |
| 5055 | co_set_data(req, co_data(req) - vlen); |
| 5056 | count -= vlen; |
| 5057 | if (len > 0) |
| 5058 | len -= vlen; |
| 5059 | if (sz == vlen) |
| 5060 | blk = htx_remove_blk(htx, blk); |
| 5061 | else { |
| 5062 | htx_cut_data_blk(htx, blk, vlen); |
| 5063 | break; |
| 5064 | } |
| 5065 | } |
| 5066 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5067 | /* The message was fully consumed and no more data are expected |
| 5068 | * (EOM flag set). |
| 5069 | */ |
| 5070 | if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) |
| 5071 | len = 0; |
| 5072 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5073 | htx_to_buf(htx, &req->buf); |
| 5074 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5075 | /* If we are no other data available, yield waiting for new data. */ |
| 5076 | if (len) { |
| 5077 | if (len > 0) { |
| 5078 | lua_pushinteger(L, len); |
| 5079 | lua_replace(L, 2); |
| 5080 | } |
| 5081 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5082 | 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] | 5083 | } |
| 5084 | |
| 5085 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5086 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5087 | return 1; |
| 5088 | } |
| 5089 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5090 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5091 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 5092 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5093 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5094 | int len = -1; |
| 5095 | |
| 5096 | /* Check arguments. */ |
| 5097 | if (lua_gettop(L) > 2) |
| 5098 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 5099 | if (lua_gettop(L) >= 2) { |
| 5100 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5101 | lua_pop(L, 1); |
| 5102 | } |
| 5103 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5104 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5105 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5106 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5107 | luaL_buffinit(L, &luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5108 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5109 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5110 | } |
| 5111 | |
| 5112 | /* Append data in the output side of the buffer. This data is immediately |
| 5113 | * sent. The function returns the amount of data written. If the buffer |
| 5114 | * cannot contain the data, the function yields. The function returns -1 |
| 5115 | * if the channel is closed. |
| 5116 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5117 | __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] | 5118 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5119 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5120 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5121 | struct channel *res = si_ic(si); |
| 5122 | struct htx *htx = htx_from_buf(&res->buf); |
| 5123 | const char *data; |
| 5124 | size_t len; |
| 5125 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 5126 | int max; |
| 5127 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 5128 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5129 | if (!max) |
| 5130 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5131 | |
| 5132 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5133 | |
| 5134 | /* Get the max amount of data which can write as input in the channel. */ |
| 5135 | if (max > (len - l)) |
| 5136 | max = len - l; |
| 5137 | |
| 5138 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 5139 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5140 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5141 | |
| 5142 | /* update counters. */ |
| 5143 | l += max; |
| 5144 | lua_pop(L, 1); |
| 5145 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5146 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5147 | /* If some data is not send, declares the situation to the |
| 5148 | * applet, and returns a yield. |
| 5149 | */ |
| 5150 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5151 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5152 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5153 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5154 | 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] | 5155 | } |
| 5156 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5157 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5158 | return 1; |
| 5159 | } |
| 5160 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5161 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5162 | * yield the LUA process, and resume it without checking the |
| 5163 | * input arguments. |
| 5164 | */ |
| 5165 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 5166 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5167 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5168 | |
| 5169 | /* We want to send some data. Headers must be sent. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5170 | if (!(luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5171 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 5172 | WILL_LJMP(lua_error(L)); |
| 5173 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5174 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5175 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5176 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5177 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5178 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5179 | } |
| 5180 | |
| 5181 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 5182 | { |
| 5183 | const char *name; |
| 5184 | int ret; |
| 5185 | |
| 5186 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5187 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5188 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 5189 | |
| 5190 | /* Push in the stack the "response" entry. */ |
| 5191 | ret = lua_getfield(L, 1, "response"); |
| 5192 | if (ret != LUA_TTABLE) { |
| 5193 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 5194 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 5195 | WILL_LJMP(lua_error(L)); |
| 5196 | } |
| 5197 | |
| 5198 | /* check if the header is already registered if it is not |
| 5199 | * the case, register it. |
| 5200 | */ |
| 5201 | ret = lua_getfield(L, -1, name); |
| 5202 | if (ret == LUA_TNIL) { |
| 5203 | |
| 5204 | /* Entry not found. */ |
| 5205 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 5206 | |
| 5207 | /* Insert the new header name in the array in the top of the stack. |
| 5208 | * It left the new array in the top of the stack. |
| 5209 | */ |
| 5210 | lua_newtable(L); |
| 5211 | lua_pushvalue(L, 2); |
| 5212 | lua_pushvalue(L, -2); |
| 5213 | lua_settable(L, -4); |
| 5214 | |
| 5215 | } else if (ret != LUA_TTABLE) { |
| 5216 | |
| 5217 | /* corruption error. */ |
| 5218 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 5219 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 5220 | WILL_LJMP(lua_error(L)); |
| 5221 | } |
| 5222 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5223 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5224 | * the header value as new entry. |
| 5225 | */ |
| 5226 | lua_pushvalue(L, 3); |
| 5227 | ret = lua_rawlen(L, -2); |
| 5228 | lua_rawseti(L, -2, ret + 1); |
| 5229 | lua_pushboolean(L, 1); |
| 5230 | return 1; |
| 5231 | } |
| 5232 | |
| 5233 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 5234 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5235 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5236 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5237 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5238 | |
| 5239 | if (status < 100 || status > 599) { |
| 5240 | lua_pushboolean(L, 0); |
| 5241 | return 1; |
| 5242 | } |
| 5243 | |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5244 | luactx->appctx->ctx.hlua_apphttp.status = status; |
| 5245 | luactx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5246 | lua_pushboolean(L, 1); |
| 5247 | return 1; |
| 5248 | } |
| 5249 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5250 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5251 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5252 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5253 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5254 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5255 | struct channel *res = si_ic(si); |
| 5256 | struct htx *htx; |
| 5257 | struct htx_sl *sl; |
| 5258 | struct h1m h1m; |
| 5259 | const char *status, *reason; |
| 5260 | const char *name, *value; |
| 5261 | size_t nlen, vlen; |
| 5262 | unsigned int flags; |
| 5263 | |
| 5264 | /* Send the message at once. */ |
| 5265 | htx = htx_from_buf(&res->buf); |
| 5266 | h1m_init_res(&h1m); |
| 5267 | |
| 5268 | /* Use the same http version than the request. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5269 | status = ultoa_r(luactx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 5270 | reason = luactx->appctx->ctx.hlua_apphttp.reason; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5271 | if (reason == NULL) |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5272 | reason = http_get_reason(luactx->appctx->ctx.hlua_apphttp.status); |
| 5273 | if (luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5274 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5275 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 5276 | } |
| 5277 | else { |
| 5278 | flags = HTX_SL_F_IS_RESP; |
| 5279 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 5280 | } |
| 5281 | if (!sl) { |
| 5282 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5283 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5284 | WILL_LJMP(lua_error(L)); |
| 5285 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5286 | sl->info.res.status = luactx->appctx->ctx.hlua_apphttp.status; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5287 | |
| 5288 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 5289 | lua_pushvalue(L, 0); |
| 5290 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 5291 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5292 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5293 | WILL_LJMP(lua_error(L)); |
| 5294 | } |
| 5295 | |
| 5296 | /* Browse the list of headers. */ |
| 5297 | lua_pushnil(L); |
| 5298 | while(lua_next(L, -2) != 0) { |
| 5299 | /* We expect a string as -2. */ |
| 5300 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 5301 | 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] | 5302 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5303 | lua_typename(L, lua_type(L, -2))); |
| 5304 | WILL_LJMP(lua_error(L)); |
| 5305 | } |
| 5306 | name = lua_tolstring(L, -2, &nlen); |
| 5307 | |
| 5308 | /* We expect an array as -1. */ |
| 5309 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 5310 | 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] | 5311 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5312 | name, |
| 5313 | lua_typename(L, lua_type(L, -1))); |
| 5314 | WILL_LJMP(lua_error(L)); |
| 5315 | } |
| 5316 | |
| 5317 | /* Browse the table who is on the top of the stack. */ |
| 5318 | lua_pushnil(L); |
| 5319 | while(lua_next(L, -2) != 0) { |
| 5320 | int id; |
| 5321 | |
| 5322 | /* We expect a number as -2. */ |
| 5323 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 5324 | 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] | 5325 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5326 | name, |
| 5327 | lua_typename(L, lua_type(L, -2))); |
| 5328 | WILL_LJMP(lua_error(L)); |
| 5329 | } |
| 5330 | id = lua_tointeger(L, -2); |
| 5331 | |
| 5332 | /* We expect a string as -2. */ |
| 5333 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 5334 | 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] | 5335 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5336 | name, id, |
| 5337 | lua_typename(L, lua_type(L, -1))); |
| 5338 | WILL_LJMP(lua_error(L)); |
| 5339 | } |
| 5340 | value = lua_tolstring(L, -1, &vlen); |
| 5341 | |
| 5342 | /* Simple Protocol checks. */ |
| 5343 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5344 | h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5345 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 5346 | struct ist v = ist2(value, vlen); |
| 5347 | int ret; |
| 5348 | |
| 5349 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 5350 | if (ret < 0) { |
| 5351 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5352 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5353 | name); |
| 5354 | WILL_LJMP(lua_error(L)); |
| 5355 | } |
| 5356 | else if (ret == 0) |
| 5357 | goto next; /* Skip it */ |
| 5358 | } |
| 5359 | |
| 5360 | /* Add a new header */ |
| 5361 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 5362 | 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] | 5363 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5364 | name); |
| 5365 | WILL_LJMP(lua_error(L)); |
| 5366 | } |
| 5367 | next: |
| 5368 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5369 | lua_pop(L, 1); |
| 5370 | } |
| 5371 | |
| 5372 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5373 | lua_pop(L, 1); |
| 5374 | } |
| 5375 | |
| 5376 | if (h1m.flags & H1_MF_CHNK) |
| 5377 | h1m.flags &= ~H1_MF_CLEN; |
| 5378 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5379 | h1m.flags |= H1_MF_XFER_LEN; |
| 5380 | |
| 5381 | /* Uset HTX start-line flags */ |
| 5382 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5383 | flags |= HTX_SL_F_XFER_ENC; |
| 5384 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5385 | flags |= HTX_SL_F_XFER_LEN; |
| 5386 | if (h1m.flags & H1_MF_CHNK) |
| 5387 | flags |= HTX_SL_F_CHNK; |
| 5388 | else if (h1m.flags & H1_MF_CLEN) |
| 5389 | flags |= HTX_SL_F_CLEN; |
| 5390 | if (h1m.body_len == 0) |
| 5391 | flags |= HTX_SL_F_BODYLESS; |
| 5392 | } |
| 5393 | sl->flags |= flags; |
| 5394 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5395 | /* 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] | 5396 | * and the status code implies the presence of a message body, we must |
| 5397 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5398 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 5399 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5400 | */ |
| 5401 | 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] | 5402 | luactx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 5403 | luactx->appctx->ctx.hlua_apphttp.status != 204 && |
| 5404 | luactx->appctx->ctx.hlua_apphttp.status != 304) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5405 | /* Add a new header */ |
| 5406 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 5407 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 5408 | 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] | 5409 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5410 | WILL_LJMP(lua_error(L)); |
| 5411 | } |
| 5412 | } |
| 5413 | |
| 5414 | /* Finalize headers. */ |
| 5415 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 5416 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5417 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5418 | WILL_LJMP(lua_error(L)); |
| 5419 | } |
| 5420 | |
| 5421 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 5422 | b_reset(&res->buf); |
| 5423 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 5424 | WILL_LJMP(lua_error(L)); |
| 5425 | } |
| 5426 | |
| 5427 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5428 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5429 | |
| 5430 | /* Headers sent, set the flag. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5431 | luactx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5432 | return 0; |
| 5433 | |
| 5434 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5435 | /* We will build the status line and the headers of the HTTP response. |
| 5436 | * We will try send at once if its not possible, we give back the hand |
| 5437 | * waiting for more room. |
| 5438 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5439 | __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] | 5440 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5441 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5442 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5443 | struct channel *res = si_ic(si); |
| 5444 | |
| 5445 | if (co_data(res)) { |
| 5446 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5447 | 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] | 5448 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5449 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5450 | } |
| 5451 | |
| 5452 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5453 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5454 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5455 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5456 | } |
| 5457 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5458 | /* |
| 5459 | * |
| 5460 | * |
| 5461 | * Class HTTP |
| 5462 | * |
| 5463 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5464 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5465 | |
| 5466 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5467 | * a class stream, otherwise it throws an error. |
| 5468 | */ |
| 5469 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5470 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5471 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 5472 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5473 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5474 | /* This function creates and push in the stack a HTTP object |
| 5475 | * according with a current TXN. |
| 5476 | */ |
| 5477 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5478 | { |
| 5479 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5480 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5481 | /* Check stack size. */ |
| 5482 | if (!lua_checkstack(L, 3)) |
| 5483 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5484 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5485 | /* Create the object: obj[0] = userdata. |
| 5486 | * Note that the base of the Converters object is the |
| 5487 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5488 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5489 | lua_newtable(L); |
| 5490 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5491 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5492 | |
| 5493 | htxn->s = txn->s; |
| 5494 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 5495 | htxn->dir = txn->dir; |
| 5496 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5497 | |
| 5498 | /* Pop a class stream metatable and affect it to the table. */ |
| 5499 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5500 | lua_setmetatable(L, -2); |
| 5501 | |
| 5502 | return 1; |
| 5503 | } |
| 5504 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5505 | /* This function creates and returns an array containing the status-line |
| 5506 | * elements. This function does not fails. |
| 5507 | */ |
| 5508 | __LJMP static int hlua_http_get_stline(lua_State *L, struct htx_sl *sl) |
| 5509 | { |
| 5510 | /* Create the table. */ |
| 5511 | lua_newtable(L); |
| 5512 | |
| 5513 | if (sl->flags & HTX_SL_F_IS_RESP) { |
| 5514 | lua_pushstring(L, "version"); |
| 5515 | lua_pushlstring(L, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); |
| 5516 | lua_settable(L, -3); |
| 5517 | lua_pushstring(L, "code"); |
| 5518 | lua_pushlstring(L, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); |
| 5519 | lua_settable(L, -3); |
| 5520 | lua_pushstring(L, "reason"); |
| 5521 | lua_pushlstring(L, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); |
| 5522 | lua_settable(L, -3); |
| 5523 | } |
| 5524 | else { |
| 5525 | lua_pushstring(L, "method"); |
| 5526 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 5527 | lua_settable(L, -3); |
| 5528 | lua_pushstring(L, "uri"); |
| 5529 | lua_pushlstring(L, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); |
| 5530 | lua_settable(L, -3); |
| 5531 | lua_pushstring(L, "version"); |
| 5532 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 5533 | lua_settable(L, -3); |
| 5534 | } |
| 5535 | return 1; |
| 5536 | } |
| 5537 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5538 | /* This function creates ans returns an array of HTTP headers. |
| 5539 | * This function does not fails. It is used as wrapper with the |
| 5540 | * 2 following functions. |
| 5541 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5542 | __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] | 5543 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5544 | struct htx *htx; |
| 5545 | int32_t pos; |
| 5546 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5547 | /* Create the table. */ |
| 5548 | lua_newtable(L); |
| 5549 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5550 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5551 | htx = htxbuf(&msg->chn->buf); |
| 5552 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5553 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5554 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5555 | struct ist n, v; |
| 5556 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5557 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5558 | if (type == HTX_BLK_HDR) { |
| 5559 | n = htx_get_blk_name(htx,blk); |
| 5560 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5561 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5562 | else if (type == HTX_BLK_EOH) |
| 5563 | break; |
| 5564 | else |
| 5565 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5566 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5567 | /* Check for existing entry: |
| 5568 | * assume that the table is on the top of the stack, and |
| 5569 | * push the key in the stack, the function lua_gettable() |
| 5570 | * perform the lookup. |
| 5571 | */ |
| 5572 | lua_pushlstring(L, n.ptr, n.len); |
| 5573 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5574 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5575 | switch (lua_type(L, -1)) { |
| 5576 | case LUA_TNIL: |
| 5577 | /* Table not found, create it. */ |
| 5578 | lua_pop(L, 1); /* remove the nil value. */ |
| 5579 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5580 | lua_newtable(L); /* create and push empty table. */ |
| 5581 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5582 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5583 | 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] | 5584 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5585 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5586 | case LUA_TTABLE: |
| 5587 | /* Entry found: push the value in the table. */ |
| 5588 | len = lua_rawlen(L, -1); |
| 5589 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5590 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5591 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5592 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5593 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5594 | default: |
| 5595 | /* Other cases are errors. */ |
| 5596 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5597 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5598 | } |
| 5599 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5600 | return 1; |
| 5601 | } |
| 5602 | |
| 5603 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5604 | { |
| 5605 | struct hlua_txn *htxn; |
| 5606 | |
| 5607 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5608 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5609 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5610 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5611 | WILL_LJMP(lua_error(L)); |
| 5612 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5613 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5614 | } |
| 5615 | |
| 5616 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5617 | { |
| 5618 | struct hlua_txn *htxn; |
| 5619 | |
| 5620 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5621 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5622 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5623 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5624 | WILL_LJMP(lua_error(L)); |
| 5625 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5626 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5627 | } |
| 5628 | |
| 5629 | /* This function replace full header, or just a value in |
| 5630 | * the request or in the response. It is a wrapper fir the |
| 5631 | * 4 following functions. |
| 5632 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5633 | __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] | 5634 | { |
| 5635 | size_t name_len; |
| 5636 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5637 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5638 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5639 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5640 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5641 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5642 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5643 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5644 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5645 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5646 | 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] | 5647 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5648 | return 0; |
| 5649 | } |
| 5650 | |
| 5651 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5652 | { |
| 5653 | struct hlua_txn *htxn; |
| 5654 | |
| 5655 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5656 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5657 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5658 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5659 | WILL_LJMP(lua_error(L)); |
| 5660 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5661 | 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] | 5662 | } |
| 5663 | |
| 5664 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5665 | { |
| 5666 | struct hlua_txn *htxn; |
| 5667 | |
| 5668 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5669 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5670 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5671 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5672 | WILL_LJMP(lua_error(L)); |
| 5673 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5674 | 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] | 5675 | } |
| 5676 | |
| 5677 | __LJMP static int hlua_http_req_rep_val(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, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5688 | } |
| 5689 | |
| 5690 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5691 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5692 | struct hlua_txn *htxn; |
| 5693 | |
| 5694 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 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, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5701 | } |
| 5702 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5703 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5704 | * It is a wrapper for the 2 following functions. |
| 5705 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5706 | __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] | 5707 | { |
| 5708 | size_t len; |
| 5709 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5710 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5711 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5712 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5713 | ctx.blk = NULL; |
| 5714 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5715 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5716 | return 0; |
| 5717 | } |
| 5718 | |
| 5719 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5720 | { |
| 5721 | struct hlua_txn *htxn; |
| 5722 | |
| 5723 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5724 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5725 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5726 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5727 | WILL_LJMP(lua_error(L)); |
| 5728 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5729 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5730 | } |
| 5731 | |
| 5732 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5733 | { |
| 5734 | struct hlua_txn *htxn; |
| 5735 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5736 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5737 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5738 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5739 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5740 | WILL_LJMP(lua_error(L)); |
| 5741 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5742 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5743 | } |
| 5744 | |
| 5745 | /* This function adds an header. It is a wrapper used by |
| 5746 | * the 2 following functions. |
| 5747 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5748 | __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] | 5749 | { |
| 5750 | size_t name_len; |
| 5751 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5752 | size_t value_len; |
| 5753 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5754 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5755 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5756 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5757 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5758 | return 0; |
| 5759 | } |
| 5760 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5761 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5762 | { |
| 5763 | struct hlua_txn *htxn; |
| 5764 | |
| 5765 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5766 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5767 | |
| 5768 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5769 | WILL_LJMP(lua_error(L)); |
| 5770 | |
| 5771 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 5772 | } |
| 5773 | |
| 5774 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5775 | { |
| 5776 | struct hlua_txn *htxn; |
| 5777 | |
| 5778 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5779 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5780 | |
| 5781 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5782 | WILL_LJMP(lua_error(L)); |
| 5783 | |
| 5784 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 5785 | } |
| 5786 | |
| 5787 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5788 | { |
| 5789 | struct hlua_txn *htxn; |
| 5790 | |
| 5791 | MAY_LJMP(check_args(L, 3, "req_set_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 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5798 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 5799 | } |
| 5800 | |
| 5801 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5802 | { |
| 5803 | struct hlua_txn *htxn; |
| 5804 | |
| 5805 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5806 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5807 | |
| 5808 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5809 | WILL_LJMP(lua_error(L)); |
| 5810 | |
| 5811 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5812 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 5813 | } |
| 5814 | |
| 5815 | /* This function set the method. */ |
| 5816 | static int hlua_http_req_set_meth(lua_State *L) |
| 5817 | { |
| 5818 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5819 | size_t name_len; |
| 5820 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5821 | |
| 5822 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5823 | WILL_LJMP(lua_error(L)); |
| 5824 | |
| 5825 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1); |
| 5826 | return 1; |
| 5827 | } |
| 5828 | |
| 5829 | /* This function set the method. */ |
| 5830 | static int hlua_http_req_set_path(lua_State *L) |
| 5831 | { |
| 5832 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5833 | size_t name_len; |
| 5834 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5835 | |
| 5836 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5837 | WILL_LJMP(lua_error(L)); |
| 5838 | |
| 5839 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1); |
| 5840 | return 1; |
| 5841 | } |
| 5842 | |
| 5843 | /* This function set the query-string. */ |
| 5844 | static int hlua_http_req_set_query(lua_State *L) |
| 5845 | { |
| 5846 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5847 | size_t name_len; |
| 5848 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5849 | |
| 5850 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5851 | WILL_LJMP(lua_error(L)); |
| 5852 | |
| 5853 | /* Check length. */ |
| 5854 | if (name_len > trash.size - 1) { |
| 5855 | lua_pushboolean(L, 0); |
| 5856 | return 1; |
| 5857 | } |
| 5858 | |
| 5859 | /* Add the mark question as prefix. */ |
| 5860 | chunk_reset(&trash); |
| 5861 | trash.area[trash.data++] = '?'; |
| 5862 | memcpy(trash.area + trash.data, name, name_len); |
| 5863 | trash.data += name_len; |
| 5864 | |
| 5865 | lua_pushboolean(L, |
| 5866 | http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1); |
| 5867 | return 1; |
| 5868 | } |
| 5869 | |
| 5870 | /* This function set the uri. */ |
| 5871 | static int hlua_http_req_set_uri(lua_State *L) |
| 5872 | { |
| 5873 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5874 | size_t name_len; |
| 5875 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5876 | |
| 5877 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5878 | WILL_LJMP(lua_error(L)); |
| 5879 | |
| 5880 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1); |
| 5881 | return 1; |
| 5882 | } |
| 5883 | |
| 5884 | /* This function set the response code & optionally reason. */ |
| 5885 | static int hlua_http_res_set_status(lua_State *L) |
| 5886 | { |
| 5887 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5888 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5889 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5890 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
| 5891 | |
| 5892 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5893 | WILL_LJMP(lua_error(L)); |
| 5894 | |
| 5895 | http_res_set_status(code, reason, htxn->s); |
| 5896 | return 0; |
| 5897 | } |
| 5898 | |
| 5899 | /* |
| 5900 | * |
| 5901 | * |
| 5902 | * Class HTTPMessage |
| 5903 | * |
| 5904 | * |
| 5905 | */ |
| 5906 | |
| 5907 | /* Returns a struct http_msg if the stack entry "ud" is a class HTTPMessage, |
| 5908 | * otherwise it throws an error. |
| 5909 | */ |
| 5910 | __LJMP static struct http_msg *hlua_checkhttpmsg(lua_State *L, int ud) |
| 5911 | { |
| 5912 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_msg_ref)); |
| 5913 | } |
| 5914 | |
| 5915 | /* Creates and pushes on the stack a HTTP object according with a current TXN. |
| 5916 | */ |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 5917 | 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] | 5918 | { |
| 5919 | /* Check stack size. */ |
| 5920 | if (!lua_checkstack(L, 3)) |
| 5921 | return 0; |
| 5922 | |
| 5923 | lua_newtable(L); |
| 5924 | lua_pushlightuserdata(L, msg); |
| 5925 | lua_rawseti(L, -2, 0); |
| 5926 | |
| 5927 | /* Create the "channel" field that contains the request channel object. */ |
| 5928 | lua_pushstring(L, "channel"); |
| 5929 | if (!hlua_channel_new(L, msg->chn)) |
| 5930 | return 0; |
| 5931 | lua_rawset(L, -3); |
| 5932 | |
| 5933 | /* Pop a class stream metatable and affect it to the table. */ |
| 5934 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_msg_ref); |
| 5935 | lua_setmetatable(L, -2); |
| 5936 | |
| 5937 | return 1; |
| 5938 | } |
| 5939 | |
| 5940 | /* Helper function returning a filter attached to the HTTP message at the |
| 5941 | * position <ud> in the stack, filling the current offset and length of the |
| 5942 | * filter. If no filter is attached, NULL is returned and <offet> and <len> are |
| 5943 | * filled with output and input length respectively. |
| 5944 | */ |
| 5945 | static struct filter *hlua_http_msg_filter(lua_State *L, int ud, struct http_msg *msg, size_t *offset, size_t *len) |
| 5946 | { |
| 5947 | struct channel *chn = msg->chn; |
| 5948 | struct htx *htx = htxbuf(&chn->buf); |
| 5949 | struct filter *filter = NULL; |
| 5950 | |
| 5951 | *offset = co_data(msg->chn); |
| 5952 | *len = htx->data - co_data(msg->chn); |
| 5953 | |
| 5954 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 5955 | filter = lua_touserdata (L, -1); |
| 5956 | if (msg->msg_state >= HTTP_MSG_DATA) { |
| 5957 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 5958 | |
| 5959 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 5960 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 5961 | } |
| 5962 | } |
| 5963 | |
| 5964 | lua_pop(L, 1); |
| 5965 | return filter; |
| 5966 | } |
| 5967 | |
| 5968 | /* Returns true if the channel attached to the HTTP message is the response |
| 5969 | * channel. |
| 5970 | */ |
| 5971 | __LJMP static int hlua_http_msg_is_resp(lua_State *L) |
| 5972 | { |
| 5973 | struct http_msg *msg; |
| 5974 | |
| 5975 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 5976 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 5977 | |
| 5978 | lua_pushboolean(L, !!(msg->chn->flags & CF_ISRESP)); |
| 5979 | return 1; |
| 5980 | } |
| 5981 | |
| 5982 | /* Returns an array containing the elements status-line of the HTTP message. It relies |
| 5983 | * on hlua_http_get_stline(). |
| 5984 | */ |
| 5985 | __LJMP static int hlua_http_msg_get_stline(lua_State *L) |
| 5986 | { |
| 5987 | struct http_msg *msg; |
| 5988 | struct htx *htx; |
| 5989 | struct htx_sl *sl; |
| 5990 | |
| 5991 | MAY_LJMP(check_args(L, 1, "get_stline")); |
| 5992 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 5993 | |
| 5994 | if (msg->msg_state > HTTP_MSG_BODY) |
| 5995 | WILL_LJMP(lua_error(L)); |
| 5996 | |
| 5997 | htx = htxbuf(&msg->chn->buf); |
| 5998 | sl = http_get_stline(htx); |
| 5999 | if (!sl) |
| 6000 | return 0; |
| 6001 | return hlua_http_get_stline(L, sl); |
| 6002 | } |
| 6003 | |
| 6004 | /* Returns an array containing all headers of the HTTP message. it relies on |
| 6005 | * hlua_http_get_headers(). |
| 6006 | */ |
| 6007 | __LJMP static int hlua_http_msg_get_headers(lua_State *L) |
| 6008 | { |
| 6009 | struct http_msg *msg; |
| 6010 | |
| 6011 | MAY_LJMP(check_args(L, 1, "get_headers")); |
| 6012 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6013 | |
| 6014 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6015 | WILL_LJMP(lua_error(L)); |
| 6016 | |
| 6017 | return hlua_http_get_headers(L, msg); |
| 6018 | } |
| 6019 | |
| 6020 | /* Deletes all occurrences of an header in the HTTP message matching on its |
| 6021 | * name. It relies on hlua_http_del_hdr(). |
| 6022 | */ |
| 6023 | __LJMP static int hlua_http_msg_del_hdr(lua_State *L) |
| 6024 | { |
| 6025 | struct http_msg *msg; |
| 6026 | |
| 6027 | MAY_LJMP(check_args(L, 2, "del_header")); |
| 6028 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6029 | |
| 6030 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6031 | WILL_LJMP(lua_error(L)); |
| 6032 | |
| 6033 | return hlua_http_del_hdr(L, msg); |
| 6034 | } |
| 6035 | |
| 6036 | /* Matches the full value line of all occurences of an header in the HTTP |
| 6037 | * message given its name against a regex and replaces it if it matches. It |
| 6038 | * relies on hlua_http_rep_hdr(). |
| 6039 | */ |
| 6040 | __LJMP static int hlua_http_msg_rep_hdr(lua_State *L) |
| 6041 | { |
| 6042 | struct http_msg *msg; |
| 6043 | |
| 6044 | MAY_LJMP(check_args(L, 4, "rep_header")); |
| 6045 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6046 | |
| 6047 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6048 | WILL_LJMP(lua_error(L)); |
| 6049 | |
| 6050 | return hlua_http_rep_hdr(L, msg, 1); |
| 6051 | } |
| 6052 | |
| 6053 | /* Matches all comma-separated values of all occurences of an header in the HTTP |
| 6054 | * message given its name against a regex and replaces it if it matches. It |
| 6055 | * relies on hlua_http_rep_hdr(). |
| 6056 | */ |
| 6057 | __LJMP static int hlua_http_msg_rep_val(lua_State *L) |
| 6058 | { |
| 6059 | struct http_msg *msg; |
| 6060 | |
| 6061 | MAY_LJMP(check_args(L, 4, "rep_value")); |
| 6062 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6063 | |
| 6064 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6065 | WILL_LJMP(lua_error(L)); |
| 6066 | |
| 6067 | return hlua_http_rep_hdr(L, msg, 0); |
| 6068 | } |
| 6069 | |
| 6070 | /* Add an header in the HTTP message. It relies on hlua_http_add_hdr() */ |
| 6071 | __LJMP static int hlua_http_msg_add_hdr(lua_State *L) |
| 6072 | { |
| 6073 | struct http_msg *msg; |
| 6074 | |
| 6075 | MAY_LJMP(check_args(L, 3, "add_header")); |
| 6076 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6077 | |
| 6078 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6079 | WILL_LJMP(lua_error(L)); |
| 6080 | |
| 6081 | return hlua_http_add_hdr(L, msg); |
| 6082 | } |
| 6083 | |
| 6084 | /* Add an header in the HTTP message removing existing headers with the same |
| 6085 | * name. It relies on hlua_http_del_hdr() and hlua_http_add_hdr(). |
| 6086 | */ |
| 6087 | __LJMP static int hlua_http_msg_set_hdr(lua_State *L) |
| 6088 | { |
| 6089 | struct http_msg *msg; |
| 6090 | |
| 6091 | MAY_LJMP(check_args(L, 3, "set_header")); |
| 6092 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6093 | |
| 6094 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6095 | WILL_LJMP(lua_error(L)); |
| 6096 | |
| 6097 | hlua_http_del_hdr(L, msg); |
| 6098 | return hlua_http_add_hdr(L, msg); |
| 6099 | } |
| 6100 | |
| 6101 | /* Rewrites the request method. It relies on http_req_replace_stline(). */ |
| 6102 | __LJMP static int hlua_http_msg_set_meth(lua_State *L) |
| 6103 | { |
| 6104 | struct stream *s; |
| 6105 | struct http_msg *msg; |
| 6106 | const char *name; |
| 6107 | size_t name_len; |
| 6108 | |
| 6109 | MAY_LJMP(check_args(L, 2, "set_method")); |
| 6110 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6111 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6112 | |
| 6113 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6114 | WILL_LJMP(lua_error(L)); |
| 6115 | |
| 6116 | s = chn_strm(msg->chn); |
| 6117 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, s->be, s) != -1); |
| 6118 | return 1; |
| 6119 | } |
| 6120 | |
| 6121 | /* Rewrites the request path. It relies on http_req_replace_stline(). */ |
| 6122 | __LJMP static int hlua_http_msg_set_path(lua_State *L) |
| 6123 | { |
| 6124 | struct stream *s; |
| 6125 | struct http_msg *msg; |
| 6126 | const char *name; |
| 6127 | size_t name_len; |
| 6128 | |
| 6129 | MAY_LJMP(check_args(L, 2, "set_path")); |
| 6130 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6131 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6132 | |
| 6133 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6134 | WILL_LJMP(lua_error(L)); |
| 6135 | |
| 6136 | s = chn_strm(msg->chn); |
| 6137 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, s->be, s) != -1); |
| 6138 | return 1; |
| 6139 | } |
| 6140 | |
| 6141 | /* Rewrites the request query-string. It relies on http_req_replace_stline(). */ |
| 6142 | __LJMP static int hlua_http_msg_set_query(lua_State *L) |
| 6143 | { |
| 6144 | struct stream *s; |
| 6145 | struct http_msg *msg; |
| 6146 | const char *name; |
| 6147 | size_t name_len; |
| 6148 | |
| 6149 | MAY_LJMP(check_args(L, 2, "set_query")); |
| 6150 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6151 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6152 | |
| 6153 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6154 | WILL_LJMP(lua_error(L)); |
| 6155 | |
| 6156 | /* Check length. */ |
| 6157 | if (name_len > trash.size - 1) { |
| 6158 | lua_pushboolean(L, 0); |
| 6159 | return 1; |
| 6160 | } |
| 6161 | |
| 6162 | /* Add the mark question as prefix. */ |
| 6163 | chunk_reset(&trash); |
| 6164 | trash.area[trash.data++] = '?'; |
| 6165 | memcpy(trash.area + trash.data, name, name_len); |
| 6166 | trash.data += name_len; |
| 6167 | |
| 6168 | s = chn_strm(msg->chn); |
| 6169 | lua_pushboolean(L, http_req_replace_stline(2, trash.area, trash.data, s->be, s) != -1); |
| 6170 | return 1; |
| 6171 | } |
| 6172 | |
| 6173 | /* Rewrites the request URI. It relies on http_req_replace_stline(). */ |
| 6174 | __LJMP static int hlua_http_msg_set_uri(lua_State *L) |
| 6175 | { |
| 6176 | struct stream *s; |
| 6177 | struct http_msg *msg; |
| 6178 | const char *name; |
| 6179 | size_t name_len; |
| 6180 | |
| 6181 | MAY_LJMP(check_args(L, 2, "set_uri")); |
| 6182 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6183 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6184 | |
| 6185 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6186 | WILL_LJMP(lua_error(L)); |
| 6187 | |
| 6188 | s = chn_strm(msg->chn); |
| 6189 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, s->be, s) != -1); |
| 6190 | return 1; |
| 6191 | } |
| 6192 | |
| 6193 | /* Rewrites the response status code. It relies on http_res_set_status(). */ |
| 6194 | __LJMP static int hlua_http_msg_set_status(lua_State *L) |
| 6195 | { |
| 6196 | struct http_msg *msg; |
| 6197 | unsigned int code; |
| 6198 | const char *reason; |
| 6199 | size_t reason_len; |
| 6200 | |
| 6201 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6202 | code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6203 | reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, &reason_len)); |
| 6204 | |
| 6205 | if (!(msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6206 | WILL_LJMP(lua_error(L)); |
| 6207 | |
| 6208 | lua_pushboolean(L, http_res_set_status(code, ist2(reason, reason_len), chn_strm(msg->chn)) != -1); |
| 6209 | return 1; |
| 6210 | } |
| 6211 | |
| 6212 | /* Returns true if the HTTP message is full. */ |
| 6213 | __LJMP static int hlua_http_msg_is_full(lua_State *L) |
| 6214 | { |
| 6215 | struct http_msg *msg; |
| 6216 | |
| 6217 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 6218 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6219 | lua_pushboolean(L, channel_full(msg->chn, 0)); |
| 6220 | return 1; |
| 6221 | } |
| 6222 | |
| 6223 | /* Returns true if the HTTP message may still receive data. */ |
| 6224 | __LJMP static int hlua_http_msg_may_recv(lua_State *L) |
| 6225 | { |
| 6226 | struct http_msg *msg; |
| 6227 | struct htx *htx; |
| 6228 | |
| 6229 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6230 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6231 | htx = htxbuf(&msg->chn->buf); |
| 6232 | lua_pushboolean(L, (htx_expect_more(htx) && !channel_input_closed(msg->chn) && channel_may_recv(msg->chn))); |
| 6233 | return 1; |
| 6234 | } |
| 6235 | |
| 6236 | /* Returns true if the HTTP message EOM was received */ |
| 6237 | __LJMP static int hlua_http_msg_is_eom(lua_State *L) |
| 6238 | { |
| 6239 | struct http_msg *msg; |
| 6240 | struct htx *htx; |
| 6241 | |
| 6242 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6243 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6244 | htx = htxbuf(&msg->chn->buf); |
| 6245 | lua_pushboolean(L, !htx_expect_more(htx)); |
| 6246 | return 1; |
| 6247 | } |
| 6248 | |
| 6249 | /* Returns the number of bytes available in the input side of the HTTP |
| 6250 | * message. This function never fails. |
| 6251 | */ |
| 6252 | __LJMP static int hlua_http_msg_get_in_len(lua_State *L) |
| 6253 | { |
| 6254 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6255 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6256 | |
| 6257 | MAY_LJMP(check_args(L, 1, "input")); |
| 6258 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6259 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6260 | lua_pushinteger(L, input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6261 | return 1; |
| 6262 | } |
| 6263 | |
| 6264 | /* Returns the number of bytes available in the output side of the HTTP |
| 6265 | * message. This function never fails. |
| 6266 | */ |
| 6267 | __LJMP static int hlua_http_msg_get_out_len(lua_State *L) |
| 6268 | { |
| 6269 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6270 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6271 | |
| 6272 | MAY_LJMP(check_args(L, 1, "output")); |
| 6273 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6274 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6275 | lua_pushinteger(L, output); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6276 | return 1; |
| 6277 | } |
| 6278 | |
| 6279 | /* Copies at most <len> bytes of DATA blocks from the HTTP message <msg> |
| 6280 | * starting at the offset <offset> and put it in a string LUA variables. It |
| 6281 | * returns the length of the builded string. It stops on the first non-DATA HTX |
| 6282 | * block. This function is called during the payload filtering, so the headers |
| 6283 | * are already scheduled for output (from the filter point of view). |
| 6284 | */ |
| 6285 | static int _hlua_http_msg_dup(struct http_msg *msg, lua_State *L, size_t offset, size_t len) |
| 6286 | { |
| 6287 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 6288 | struct htx_blk *blk; |
| 6289 | struct htx_ret htxret; |
| 6290 | luaL_Buffer b; |
| 6291 | int ret = 0; |
| 6292 | |
| 6293 | luaL_buffinit(L, &b); |
| 6294 | htxret = htx_find_offset(htx, offset); |
| 6295 | for (blk = htxret.blk, offset = htxret.ret; blk && len; blk = htx_get_next_blk(htx, blk)) { |
| 6296 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6297 | struct ist v; |
| 6298 | |
| 6299 | switch (type) { |
| 6300 | case HTX_BLK_UNUSED: |
| 6301 | break; |
| 6302 | |
| 6303 | case HTX_BLK_DATA: |
| 6304 | v = htx_get_blk_value(htx, blk); |
| 6305 | v.ptr += offset; |
| 6306 | v.len -= offset; |
| 6307 | if (v.len > len) |
| 6308 | v.len = len; |
| 6309 | |
| 6310 | luaL_addlstring(&b, v.ptr, v.len); |
| 6311 | ret += v.len; |
| 6312 | break; |
| 6313 | |
| 6314 | default: |
| 6315 | if (!ret) { |
| 6316 | /* Remove the empty string and push nil on the stack */ |
| 6317 | lua_pop(L, 1); |
| 6318 | lua_pushnil(L); |
| 6319 | } |
| 6320 | goto end; |
| 6321 | } |
| 6322 | offset = 0; |
| 6323 | } |
| 6324 | |
| 6325 | luaL_pushresult(&b); |
| 6326 | |
| 6327 | end: |
| 6328 | return ret; |
| 6329 | } |
| 6330 | |
| 6331 | /* Copies the string <str> to the HTTP message <msg> at the offset |
| 6332 | * <offset>. This function returns -1 if data cannot be copied. Otherwise, it |
| 6333 | * returns the amount of data written. This function is responsibile to update |
| 6334 | * the filter context. |
| 6335 | */ |
| 6336 | static int _hlua_http_msg_insert(struct http_msg *msg, struct filter *filter, struct ist str, size_t offset) |
| 6337 | { |
| 6338 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6339 | struct htx_ret htxret; |
| 6340 | int /*max, */ret = 0; |
| 6341 | |
| 6342 | /* Nothing to do, just return */ |
| 6343 | if (unlikely(istlen(str) == 0)) |
| 6344 | goto end; |
| 6345 | |
| 6346 | if (istlen(str) > htx_free_data_space(htx)) { |
| 6347 | ret = -1; |
| 6348 | goto end; |
| 6349 | } |
| 6350 | |
| 6351 | htxret = htx_find_offset(htx, offset); |
| 6352 | if (!htxret.blk || htx_get_blk_type(htxret.blk) != HTX_BLK_DATA) { |
| 6353 | if (!htx_add_last_data(htx, str)) |
| 6354 | goto end; |
| 6355 | } |
| 6356 | else { |
| 6357 | struct ist v = htx_get_blk_value(htx, htxret.blk); |
| 6358 | v.ptr += htxret.ret; |
| 6359 | v.len = 0; |
| 6360 | if (!htx_replace_blk_value(htx, htxret.blk, v, str)) |
| 6361 | goto end; |
| 6362 | } |
| 6363 | ret = str.len; |
| 6364 | if (ret) { |
| 6365 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6366 | flt_update_offsets(filter, msg->chn, ret); |
| 6367 | flt_ctx->cur_len[CHN_IDX(msg->chn)] += ret; |
| 6368 | } |
| 6369 | |
| 6370 | end: |
| 6371 | htx_to_buf(htx, &msg->chn->buf); |
| 6372 | return ret; |
| 6373 | } |
| 6374 | |
| 6375 | /* Helper function removing at most <len> bytes of DATA blocks at the absolute |
| 6376 | * position <offset>. It stops on the first non-DATA HTX block. This function is |
| 6377 | * called during the payload filtering, so the headers are already scheduled for |
| 6378 | * output (from the filter point of view). This function is responsibile to |
| 6379 | * update the filter context. |
| 6380 | */ |
| 6381 | static void _hlua_http_msg_delete(struct http_msg *msg, struct filter *filter, size_t offset, size_t len) |
| 6382 | { |
| 6383 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6384 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6385 | struct htx_blk *blk; |
| 6386 | struct htx_ret htxret; |
| 6387 | size_t ret = 0; |
| 6388 | |
| 6389 | /* Be sure <len> is always the amount of DATA to remove */ |
| 6390 | if (htx->data == offset+len && htx_get_tail_type(htx) == HTX_BLK_DATA) { |
| 6391 | htx_truncate(htx, offset); |
| 6392 | ret = len; |
| 6393 | goto end; |
| 6394 | } |
| 6395 | |
| 6396 | htxret = htx_find_offset(htx, offset); |
| 6397 | blk = htxret.blk; |
| 6398 | if (htxret.ret) { |
| 6399 | struct ist v; |
| 6400 | |
| 6401 | if (htx_get_blk_type(blk) != HTX_BLK_DATA) |
| 6402 | goto end; |
| 6403 | v = htx_get_blk_value(htx, blk); |
| 6404 | v.ptr += htxret.ret; |
| 6405 | if (v.len > len) |
| 6406 | v.len = len; |
| 6407 | blk = htx_replace_blk_value(htx, blk, v, ist2(NULL, 0)); |
| 6408 | len -= v.len; |
| 6409 | ret += v.len; |
| 6410 | } |
| 6411 | |
| 6412 | |
| 6413 | while (blk && len) { |
| 6414 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6415 | uint32_t sz = htx_get_blksz(blk); |
| 6416 | |
| 6417 | switch (type) { |
| 6418 | case HTX_BLK_UNUSED: |
| 6419 | break; |
| 6420 | |
| 6421 | case HTX_BLK_DATA: |
| 6422 | if (len < sz) { |
| 6423 | htx_cut_data_blk(htx, blk, len); |
| 6424 | ret += len; |
| 6425 | goto end; |
| 6426 | } |
| 6427 | break; |
| 6428 | |
| 6429 | default: |
| 6430 | goto end; |
| 6431 | } |
| 6432 | |
| 6433 | /* Remove oll the data block */ |
| 6434 | len -= sz; |
| 6435 | ret += sz; |
| 6436 | blk = htx_remove_blk(htx, blk); |
| 6437 | } |
| 6438 | |
| 6439 | end: |
| 6440 | flt_update_offsets(filter, msg->chn, -ret); |
| 6441 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6442 | /* WARNING: we don't call htx_to_buf() on purpose, because we don't want |
| 6443 | * to loose the EOM flag if the message is empty. |
| 6444 | */ |
| 6445 | } |
| 6446 | |
| 6447 | /* Copies input data found in an HTTP message. Unlike the channel function used |
| 6448 | * to duplicate raw data, this one can only be called inside a filter, from |
| 6449 | * http_payload callback. So it cannot yield. An exception is returned if it is |
| 6450 | * called from another callback. If nothing was copied, a nil value is pushed on |
| 6451 | * the stack. |
| 6452 | */ |
| 6453 | __LJMP static int hlua_http_msg_get_body(lua_State *L) |
| 6454 | { |
| 6455 | struct http_msg *msg; |
| 6456 | struct filter *filter; |
| 6457 | size_t output, input; |
| 6458 | int offset, len; |
| 6459 | |
| 6460 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 6461 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 6462 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6463 | |
| 6464 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6465 | WILL_LJMP(lua_error(L)); |
| 6466 | |
| 6467 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6468 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6469 | WILL_LJMP(lua_error(L)); |
| 6470 | |
| 6471 | if (!ci_data(msg->chn) && channel_input_closed(msg->chn)) { |
| 6472 | lua_pushnil(L); |
| 6473 | return 1; |
| 6474 | } |
| 6475 | |
| 6476 | offset = output; |
| 6477 | if (lua_gettop(L) > 1) { |
| 6478 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6479 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6480 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6481 | offset += output; |
| 6482 | if (offset < output || offset > input + output) { |
| 6483 | lua_pushfstring(L, "offset out of range."); |
| 6484 | WILL_LJMP(lua_error(L)); |
| 6485 | } |
| 6486 | } |
| 6487 | len = output + input - offset; |
| 6488 | if (lua_gettop(L) == 3) { |
| 6489 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6490 | if (!len) |
| 6491 | goto dup; |
| 6492 | if (len == -1) |
| 6493 | len = global.tune.bufsize; |
| 6494 | if (len < 0) { |
| 6495 | lua_pushfstring(L, "length out of range."); |
| 6496 | WILL_LJMP(lua_error(L)); |
| 6497 | } |
| 6498 | } |
| 6499 | |
| 6500 | dup: |
| 6501 | _hlua_http_msg_dup(msg, L, offset, len); |
| 6502 | return 1; |
| 6503 | } |
| 6504 | |
| 6505 | /* Appends a string to the HTTP message, after all existing DATA blocks but |
| 6506 | * before the trailers, if any. It returns the amount of data written or -1 if |
| 6507 | * nothing was copied. Unlike the channel function used to append data, this one |
| 6508 | * can only be called inside a filter, from http_payload callback. So it cannot |
| 6509 | * yield. An exception is returned if it is called from another callback. |
| 6510 | */ |
| 6511 | __LJMP static int hlua_http_msg_append(lua_State *L) |
| 6512 | { |
| 6513 | struct http_msg *msg; |
| 6514 | struct filter *filter; |
| 6515 | const char *str; |
| 6516 | size_t offset, len, sz; |
| 6517 | int ret; |
| 6518 | |
| 6519 | MAY_LJMP(check_args(L, 2, "append")); |
| 6520 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6521 | |
| 6522 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6523 | WILL_LJMP(lua_error(L)); |
| 6524 | |
| 6525 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6526 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6527 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6528 | WILL_LJMP(lua_error(L)); |
| 6529 | |
| 6530 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset+len); |
| 6531 | lua_pushinteger(L, ret); |
| 6532 | return 1; |
| 6533 | } |
| 6534 | |
| 6535 | /* Prepends a string to the HTTP message, before all existing DATA blocks. It |
| 6536 | * returns the amount of data written or -1 if nothing was copied. Unlike the |
| 6537 | * channel function used to prepend data, this one can only be called inside a |
| 6538 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6539 | * returned if it is called from another callback. |
| 6540 | */ |
| 6541 | __LJMP static int hlua_http_msg_prepend(lua_State *L) |
| 6542 | { |
| 6543 | struct http_msg *msg; |
| 6544 | struct filter *filter; |
| 6545 | const char *str; |
| 6546 | size_t offset, len, sz; |
| 6547 | int ret; |
| 6548 | |
| 6549 | MAY_LJMP(check_args(L, 2, "prepend")); |
| 6550 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6551 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6552 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6553 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6554 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6555 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6556 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6557 | if (!filter || !hlua_filter_from_payload(filter)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6558 | WILL_LJMP(lua_error(L)); |
| 6559 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6560 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6561 | lua_pushinteger(L, ret); |
| 6562 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6563 | } |
| 6564 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6565 | /* Inserts a string to the HTTP message at a given offset. By default the string |
| 6566 | * is appended at the end of DATA blocks. It returns the amount of data written |
| 6567 | * or -1 if nothing was copied. Unlike the channel function used to insert data, |
| 6568 | * this one can only be called inside a filter, from http_payload callback. So |
| 6569 | * it cannot yield. An exception is returned if it is called from another |
| 6570 | * callback. |
| 6571 | */ |
| 6572 | __LJMP static int hlua_http_msg_insert_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6573 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6574 | struct http_msg *msg; |
| 6575 | struct filter *filter; |
| 6576 | const char *str; |
| 6577 | size_t input, output, sz; |
| 6578 | int offset; |
| 6579 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6580 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6581 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 6582 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
| 6583 | MAY_LJMP(check_args(L, 2, "insert")); |
| 6584 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6585 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6586 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6587 | WILL_LJMP(lua_error(L)); |
| 6588 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6589 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6590 | filter = hlua_http_msg_filter(L, 1, msg, &input, &output); |
| 6591 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6592 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6593 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6594 | offset = input + output; |
| 6595 | if (lua_gettop(L) > 2) { |
| 6596 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6597 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6598 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6599 | offset += output; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6600 | if (offset < output || offset > output + input) { |
| 6601 | lua_pushfstring(L, "offset out of range."); |
| 6602 | WILL_LJMP(lua_error(L)); |
| 6603 | } |
| 6604 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6605 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6606 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6607 | lua_pushinteger(L, ret); |
| 6608 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6609 | } |
| 6610 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6611 | /* Removes a given amount of data from the HTTP message at a given offset. By |
| 6612 | * default all DATA blocks are removed. It returns the amount of data |
| 6613 | * removed. Unlike the channel function used to remove data, this one can only |
| 6614 | * be called inside a filter, from http_payload callback. So it cannot yield. An |
| 6615 | * exception is returned if it is called from another callback. |
| 6616 | */ |
| 6617 | __LJMP static int hlua_http_msg_del_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6618 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6619 | struct http_msg *msg; |
| 6620 | struct filter *filter; |
| 6621 | size_t input, output; |
| 6622 | int offset, len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6623 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6624 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 6625 | WILL_LJMP(luaL_error(L, "'insert' expects at most 2 arguments")); |
| 6626 | MAY_LJMP(check_args(L, 2, "insert")); |
| 6627 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6628 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6629 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6630 | WILL_LJMP(lua_error(L)); |
| 6631 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6632 | filter = hlua_http_msg_filter(L, 1, msg, &input, &output); |
| 6633 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6634 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6635 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6636 | offset = input + output; |
| 6637 | if (lua_gettop(L) > 2) { |
| 6638 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6639 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6640 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6641 | offset += output; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6642 | if (offset < output || offset > output + input) { |
| 6643 | lua_pushfstring(L, "offset out of range."); |
| 6644 | WILL_LJMP(lua_error(L)); |
| 6645 | } |
| 6646 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6647 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6648 | len = output + input - offset; |
| 6649 | if (lua_gettop(L) == 4) { |
| 6650 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6651 | if (!len) |
| 6652 | goto end; |
| 6653 | if (len == -1) |
| 6654 | len = output + input - offset; |
| 6655 | if (len < 0 || offset + len > output + input) { |
| 6656 | lua_pushfstring(L, "length out of range."); |
| 6657 | WILL_LJMP(lua_error(L)); |
| 6658 | } |
| 6659 | } |
| 6660 | |
| 6661 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6662 | |
| 6663 | end: |
| 6664 | lua_pushinteger(L, len); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6665 | return 1; |
| 6666 | } |
| 6667 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6668 | /* Replaces a given amount of data at the given offet by a string. By default, |
| 6669 | * all remaining data are removed, accordingly to the filter context. It returns |
| 6670 | * the amount of data written or -1 if nothing was copied. Unlike the channel |
| 6671 | * function used to replace data, this one can only be called inside a filter, |
| 6672 | * from http_payload callback. So it cannot yield. An exception is returned if |
| 6673 | * it is called from another callback. |
| 6674 | */ |
| 6675 | __LJMP static int hlua_http_msg_set_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6676 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6677 | struct http_msg *msg; |
| 6678 | struct filter *filter; |
| 6679 | struct htx *htx; |
| 6680 | const char *str; |
| 6681 | size_t input, output, sz; |
| 6682 | int offset, len; |
| 6683 | int ret; |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 6684 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6685 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 6686 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 6687 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6688 | |
| 6689 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6690 | WILL_LJMP(lua_error(L)); |
| 6691 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6692 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6693 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6694 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6695 | WILL_LJMP(lua_error(L)); |
| 6696 | |
| 6697 | offset = output; |
| 6698 | if (lua_gettop(L) > 2) { |
| 6699 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6700 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6701 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6702 | offset += output; |
| 6703 | if (offset < output || offset > input + output) { |
| 6704 | lua_pushfstring(L, "offset out of range."); |
| 6705 | WILL_LJMP(lua_error(L)); |
| 6706 | } |
| 6707 | } |
| 6708 | |
| 6709 | len = output + input - offset; |
| 6710 | if (lua_gettop(L) == 4) { |
| 6711 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6712 | if (!len) |
| 6713 | goto set; |
| 6714 | if (len == -1) |
| 6715 | len = output + input - offset; |
| 6716 | if (len < 0 || offset + len > output + input) { |
| 6717 | lua_pushfstring(L, "length out of range."); |
| 6718 | WILL_LJMP(lua_error(L)); |
| 6719 | } |
| 6720 | } |
| 6721 | |
| 6722 | set: |
| 6723 | /* Be sure we can copied the string once input data will be removed. */ |
| 6724 | htx = htx_from_buf(&msg->chn->buf); |
| 6725 | if (sz > htx_free_data_space(htx) + len) |
| 6726 | lua_pushinteger(L, -1); |
| 6727 | else { |
| 6728 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6729 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6730 | lua_pushinteger(L, ret); |
| 6731 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6732 | return 1; |
| 6733 | } |
| 6734 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6735 | /* Prepends data into an HTTP message and forward it, from the filter point of |
| 6736 | * view. It returns the amount of data written or -1 if nothing was sent. Unlike |
| 6737 | * the channel function used to send data, this one can only be called inside a |
| 6738 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6739 | * returned if it is called from another callback. |
| 6740 | */ |
| 6741 | __LJMP static int hlua_http_msg_send(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6742 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6743 | struct http_msg *msg; |
| 6744 | struct filter *filter; |
| 6745 | struct htx *htx; |
| 6746 | const char *str; |
| 6747 | size_t offset, len, sz; |
| 6748 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6749 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6750 | MAY_LJMP(check_args(L, 2, "send")); |
| 6751 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6752 | |
| 6753 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6754 | WILL_LJMP(lua_error(L)); |
| 6755 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6756 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6757 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6758 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6759 | WILL_LJMP(lua_error(L)); |
| 6760 | |
| 6761 | /* Return an error if the channel's output is closed */ |
| 6762 | if (unlikely(channel_output_closed(msg->chn))) { |
| 6763 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6764 | return 1; |
| 6765 | } |
| 6766 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6767 | htx = htx_from_buf(&msg->chn->buf); |
| 6768 | if (sz > htx_free_data_space(htx)) { |
| 6769 | lua_pushinteger(L, -1); |
| 6770 | return 1; |
| 6771 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6772 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6773 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6774 | if (ret > 0) { |
| 6775 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6776 | |
| 6777 | FLT_OFF(filter, msg->chn) += ret; |
| 6778 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6779 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 6780 | } |
| 6781 | |
| 6782 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6783 | return 1; |
| 6784 | } |
| 6785 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6786 | /* Forwards a given amount of bytes. It return -1 if the channel's output is |
| 6787 | * closed. Otherwise, it returns the number of bytes forwarded. Unlike the |
| 6788 | * channel function used to forward data, this one can only be called inside a |
| 6789 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6790 | * returned if it is called from another callback. All other functions deal with |
| 6791 | * DATA block, this one not. |
| 6792 | */ |
| 6793 | __LJMP static int hlua_http_msg_forward(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6794 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6795 | struct http_msg *msg; |
| 6796 | struct filter *filter; |
| 6797 | size_t offset, len; |
| 6798 | int fwd, ret = 0; |
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 | MAY_LJMP(check_args(L, 2, "forward")); |
| 6801 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6802 | |
| 6803 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6804 | WILL_LJMP(lua_error(L)); |
| 6805 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6806 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6807 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6808 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6809 | WILL_LJMP(lua_error(L)); |
| 6810 | |
| 6811 | /* Nothing to do, just return */ |
| 6812 | if (!fwd) |
| 6813 | goto end; |
| 6814 | |
| 6815 | /* Return an error if the channel's output is closed */ |
| 6816 | if (unlikely(channel_output_closed(msg->chn))) { |
| 6817 | ret = -1; |
| 6818 | goto end; |
| 6819 | } |
| 6820 | |
| 6821 | ret = fwd; |
| 6822 | if (ret > len) |
| 6823 | ret = len; |
| 6824 | |
| 6825 | if (ret) { |
| 6826 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6827 | |
| 6828 | FLT_OFF(filter, msg->chn) += ret; |
| 6829 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 6830 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6831 | } |
| 6832 | |
| 6833 | end: |
| 6834 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6835 | return 1; |
| 6836 | } |
| 6837 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6838 | /* Set EOM flag on the HTX message. |
| 6839 | * |
| 6840 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 6841 | * really know how to do without this feature. |
| 6842 | */ |
| 6843 | __LJMP static int hlua_http_msg_set_eom(lua_State *L) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6844 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6845 | struct http_msg *msg; |
| 6846 | struct htx *htx; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6847 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6848 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 6849 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6850 | htx = htxbuf(&msg->chn->buf); |
| 6851 | htx->flags |= HTX_FL_EOM; |
| 6852 | return 0; |
| 6853 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6854 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6855 | /* Unset EOM flag on the HTX message. |
| 6856 | * |
| 6857 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 6858 | * really know how to do without this feature. |
| 6859 | */ |
| 6860 | __LJMP static int hlua_http_msg_unset_eom(lua_State *L) |
| 6861 | { |
| 6862 | struct http_msg *msg; |
| 6863 | struct htx *htx; |
| 6864 | |
| 6865 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 6866 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6867 | htx = htxbuf(&msg->chn->buf); |
| 6868 | htx->flags &= ~HTX_FL_EOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6869 | return 0; |
| 6870 | } |
| 6871 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6872 | /* |
| 6873 | * |
| 6874 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 6875 | * Class TXN |
| 6876 | * |
| 6877 | * |
| 6878 | */ |
| 6879 | |
| 6880 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6881 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 6882 | */ |
| 6883 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 6884 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6885 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 6886 | } |
| 6887 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 6888 | __LJMP static int hlua_set_var(lua_State *L) |
| 6889 | { |
| 6890 | struct hlua_txn *htxn; |
| 6891 | const char *name; |
| 6892 | size_t len; |
| 6893 | struct sample smp; |
| 6894 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 6895 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 6896 | 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] | 6897 | |
| 6898 | /* It is useles to retrieve the stream, but this function |
| 6899 | * runs only in a stream context. |
| 6900 | */ |
| 6901 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6902 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 6903 | |
| 6904 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 6905 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 6906 | hlua_lua2smp(L, 3, &smp); |
| 6907 | |
| 6908 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 6909 | 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] | 6910 | |
| 6911 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 6912 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 6913 | else |
| 6914 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 6915 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 6916 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 6917 | } |
| 6918 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 6919 | __LJMP static int hlua_unset_var(lua_State *L) |
| 6920 | { |
| 6921 | struct hlua_txn *htxn; |
| 6922 | const char *name; |
| 6923 | size_t len; |
| 6924 | struct sample smp; |
| 6925 | |
| 6926 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 6927 | |
| 6928 | /* It is useles to retrieve the stream, but this function |
| 6929 | * runs only in a stream context. |
| 6930 | */ |
| 6931 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6932 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 6933 | |
| 6934 | /* Unset the variable. */ |
| 6935 | 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] | 6936 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 6937 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 6938 | } |
| 6939 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 6940 | __LJMP static int hlua_get_var(lua_State *L) |
| 6941 | { |
| 6942 | struct hlua_txn *htxn; |
| 6943 | const char *name; |
| 6944 | size_t len; |
| 6945 | struct sample smp; |
| 6946 | |
| 6947 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 6948 | |
| 6949 | /* It is useles to retrieve the stream, but this function |
| 6950 | * runs only in a stream context. |
| 6951 | */ |
| 6952 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6953 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 6954 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 6955 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 6956 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 6957 | lua_pushnil(L); |
| 6958 | return 1; |
| 6959 | } |
| 6960 | |
| 6961 | return hlua_smp2lua(L, &smp); |
| 6962 | } |
| 6963 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6964 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 6965 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6966 | struct hlua *hlua; |
| 6967 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 6968 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 6969 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6970 | /* It is useles to retrieve the stream, but this function |
| 6971 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 6972 | */ |
| 6973 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6974 | |
| 6975 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6976 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6977 | if (!hlua) |
| 6978 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 6979 | |
| 6980 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 6981 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 6982 | |
| 6983 | /* Get and store new value. */ |
| 6984 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 6985 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 6986 | |
| 6987 | return 0; |
| 6988 | } |
| 6989 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6990 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 6991 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6992 | struct hlua *hlua; |
| 6993 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 6994 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 6995 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6996 | /* It is useles to retrieve the stream, but this function |
| 6997 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 6998 | */ |
| 6999 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7000 | |
| 7001 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7002 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7003 | if (!hlua) { |
| 7004 | lua_pushnil(L); |
| 7005 | return 1; |
| 7006 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7007 | |
| 7008 | /* Push configuration index in the stack. */ |
| 7009 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 7010 | |
| 7011 | return 1; |
| 7012 | } |
| 7013 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7014 | /* Create stack entry containing a class TXN. This function |
| 7015 | * return 0 if the stack does not contains free slots, |
| 7016 | * otherwise it returns 1. |
| 7017 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7018 | 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] | 7019 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7020 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7021 | |
| 7022 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7023 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7024 | return 0; |
| 7025 | |
| 7026 | /* NOTE: The allocation never fails. The failure |
| 7027 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7028 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7029 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7030 | /* Create the object: obj[0] = userdata. */ |
| 7031 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7032 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7033 | lua_rawseti(L, -2, 0); |
| 7034 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7035 | htxn->s = s; |
| 7036 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 7037 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7038 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7039 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7040 | /* Create the "f" field that contains a list of fetches. */ |
| 7041 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7042 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7043 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7044 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7045 | |
| 7046 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 7047 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7048 | 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] | 7049 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7050 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7051 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7052 | /* Create the "c" field that contains a list of converters. */ |
| 7053 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7054 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7055 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7056 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7057 | |
| 7058 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 7059 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 7060 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7061 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7062 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7063 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7064 | /* Create the "req" field that contains the request channel object. */ |
| 7065 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7066 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7067 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7068 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7069 | |
| 7070 | /* Create the "res" field that contains the response channel object. */ |
| 7071 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7072 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7073 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7074 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7075 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7076 | /* Creates the HTTP object is the current proxy allows http. */ |
| 7077 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 7078 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7079 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7080 | return 0; |
| 7081 | } |
| 7082 | else |
| 7083 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7084 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7085 | |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 7086 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) { |
| 7087 | /* HTTPMessage object are created when a lua TXN is created from |
| 7088 | * a filter context only |
| 7089 | */ |
| 7090 | |
| 7091 | /* Creates the HTTP-Request object is the current proxy allows http. */ |
| 7092 | lua_pushstring(L, "http_req"); |
| 7093 | if (p->mode == PR_MODE_HTTP) { |
| 7094 | if (!hlua_http_msg_new(L, &s->txn->req)) |
| 7095 | return 0; |
| 7096 | } |
| 7097 | else |
| 7098 | lua_pushnil(L); |
| 7099 | lua_rawset(L, -3); |
| 7100 | |
| 7101 | /* Creates the HTTP-Response object is the current proxy allows http. */ |
| 7102 | lua_pushstring(L, "http_res"); |
| 7103 | if (p->mode == PR_MODE_HTTP) { |
| 7104 | if (!hlua_http_msg_new(L, &s->txn->rsp)) |
| 7105 | return 0; |
| 7106 | } |
| 7107 | else |
| 7108 | lua_pushnil(L); |
| 7109 | lua_rawset(L, -3); |
| 7110 | } |
| 7111 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7112 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 7113 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 7114 | lua_setmetatable(L, -2); |
| 7115 | |
| 7116 | return 1; |
| 7117 | } |
| 7118 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7119 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 7120 | { |
| 7121 | const char *msg; |
| 7122 | struct hlua_txn *htxn; |
| 7123 | |
| 7124 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 7125 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7126 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7127 | |
| 7128 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 7129 | return 0; |
| 7130 | } |
| 7131 | |
| 7132 | __LJMP static int hlua_txn_log(lua_State *L) |
| 7133 | { |
| 7134 | int level; |
| 7135 | const char *msg; |
| 7136 | struct hlua_txn *htxn; |
| 7137 | |
| 7138 | MAY_LJMP(check_args(L, 3, "log")); |
| 7139 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7140 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7141 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 7142 | |
| 7143 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 7144 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 7145 | |
| 7146 | hlua_sendlog(htxn->s->be, level, msg); |
| 7147 | return 0; |
| 7148 | } |
| 7149 | |
| 7150 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 7151 | { |
| 7152 | const char *msg; |
| 7153 | struct hlua_txn *htxn; |
| 7154 | |
| 7155 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 7156 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7157 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7158 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 7159 | return 0; |
| 7160 | } |
| 7161 | |
| 7162 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 7163 | { |
| 7164 | const char *msg; |
| 7165 | struct hlua_txn *htxn; |
| 7166 | |
| 7167 | MAY_LJMP(check_args(L, 2, "Info")); |
| 7168 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7169 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7170 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 7171 | return 0; |
| 7172 | } |
| 7173 | |
| 7174 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 7175 | { |
| 7176 | const char *msg; |
| 7177 | struct hlua_txn *htxn; |
| 7178 | |
| 7179 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 7180 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7181 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7182 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 7183 | return 0; |
| 7184 | } |
| 7185 | |
| 7186 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 7187 | { |
| 7188 | const char *msg; |
| 7189 | struct hlua_txn *htxn; |
| 7190 | |
| 7191 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 7192 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7193 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7194 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 7195 | return 0; |
| 7196 | } |
| 7197 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7198 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 7199 | { |
| 7200 | struct hlua_txn *htxn; |
| 7201 | int ll; |
| 7202 | |
| 7203 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 7204 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7205 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7206 | |
| 7207 | if (ll < 0 || ll > 7) |
| 7208 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 7209 | |
| 7210 | htxn->s->logs.level = ll; |
| 7211 | return 0; |
| 7212 | } |
| 7213 | |
| 7214 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 7215 | { |
| 7216 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7217 | int tos; |
| 7218 | |
| 7219 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 7220 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7221 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7222 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 7223 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7224 | return 0; |
| 7225 | } |
| 7226 | |
| 7227 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 7228 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7229 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7230 | int mark; |
| 7231 | |
| 7232 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 7233 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7234 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7235 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 7236 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7237 | return 0; |
| 7238 | } |
| 7239 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 7240 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 7241 | { |
| 7242 | struct hlua_txn *htxn; |
| 7243 | |
| 7244 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 7245 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7246 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 7247 | return 0; |
| 7248 | } |
| 7249 | |
| 7250 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 7251 | { |
| 7252 | struct hlua_txn *htxn; |
| 7253 | |
| 7254 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 7255 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7256 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 7257 | return 0; |
| 7258 | } |
| 7259 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7260 | /* 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] | 7261 | * 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] | 7262 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 7263 | * error. The Reply must be on top of the stack. |
| 7264 | */ |
| 7265 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 7266 | { |
| 7267 | struct htx *htx; |
| 7268 | struct htx_sl *sl; |
| 7269 | struct h1m h1m; |
| 7270 | const char *status, *reason, *body; |
| 7271 | size_t status_len, reason_len, body_len; |
| 7272 | int ret, code, flags; |
| 7273 | |
| 7274 | code = 200; |
| 7275 | status = "200"; |
| 7276 | status_len = 3; |
| 7277 | ret = lua_getfield(L, -1, "status"); |
| 7278 | if (ret == LUA_TNUMBER) { |
| 7279 | code = lua_tointeger(L, -1); |
| 7280 | status = lua_tolstring(L, -1, &status_len); |
| 7281 | } |
| 7282 | lua_pop(L, 1); |
| 7283 | |
| 7284 | reason = http_get_reason(code); |
| 7285 | reason_len = strlen(reason); |
| 7286 | ret = lua_getfield(L, -1, "reason"); |
| 7287 | if (ret == LUA_TSTRING) |
| 7288 | reason = lua_tolstring(L, -1, &reason_len); |
| 7289 | lua_pop(L, 1); |
| 7290 | |
| 7291 | body = NULL; |
| 7292 | body_len = 0; |
| 7293 | ret = lua_getfield(L, -1, "body"); |
| 7294 | if (ret == LUA_TSTRING) |
| 7295 | body = lua_tolstring(L, -1, &body_len); |
| 7296 | lua_pop(L, 1); |
| 7297 | |
| 7298 | /* Prepare the response before inserting the headers */ |
| 7299 | h1m_init_res(&h1m); |
| 7300 | htx = htx_from_buf(&s->res.buf); |
| 7301 | channel_htx_truncate(&s->res, htx); |
| 7302 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 7303 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 7304 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 7305 | ist2(status, status_len), ist2(reason, reason_len)); |
| 7306 | } |
| 7307 | else { |
| 7308 | flags = HTX_SL_F_IS_RESP; |
| 7309 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 7310 | ist2(status, status_len), ist2(reason, reason_len)); |
| 7311 | } |
| 7312 | if (!sl) |
| 7313 | goto fail; |
| 7314 | sl->info.res.status = code; |
| 7315 | |
| 7316 | /* Push in the stack the "headers" entry. */ |
| 7317 | ret = lua_getfield(L, -1, "headers"); |
| 7318 | if (ret != LUA_TTABLE) |
| 7319 | goto skip_headers; |
| 7320 | |
| 7321 | lua_pushnil(L); |
| 7322 | while (lua_next(L, -2) != 0) { |
| 7323 | struct ist name, value; |
| 7324 | const char *n, *v; |
| 7325 | size_t nlen, vlen; |
| 7326 | |
| 7327 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 7328 | /* Skip element if the key is not a string or if the value is not a table */ |
| 7329 | goto next_hdr; |
| 7330 | } |
| 7331 | |
| 7332 | n = lua_tolstring(L, -2, &nlen); |
| 7333 | name = ist2(n, nlen); |
| 7334 | if (isteqi(name, ist("content-length"))) { |
| 7335 | /* Always skip content-length header. It will be added |
| 7336 | * later with the correct len |
| 7337 | */ |
| 7338 | goto next_hdr; |
| 7339 | } |
| 7340 | |
| 7341 | /* Loop on header's values */ |
| 7342 | lua_pushnil(L); |
| 7343 | while (lua_next(L, -2)) { |
| 7344 | if (!lua_isstring(L, -1)) { |
| 7345 | /* Skip the value if it is not a string */ |
| 7346 | goto next_value; |
| 7347 | } |
| 7348 | |
| 7349 | v = lua_tolstring(L, -1, &vlen); |
| 7350 | value = ist2(v, vlen); |
| 7351 | |
| 7352 | if (isteqi(name, ist("transfer-encoding"))) |
| 7353 | h1_parse_xfer_enc_header(&h1m, value); |
| 7354 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 7355 | goto fail; |
| 7356 | |
| 7357 | next_value: |
| 7358 | lua_pop(L, 1); |
| 7359 | } |
| 7360 | |
| 7361 | next_hdr: |
| 7362 | lua_pop(L, 1); |
| 7363 | } |
| 7364 | skip_headers: |
| 7365 | lua_pop(L, 1); |
| 7366 | |
| 7367 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 7368 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 7369 | const char *clen = ultoa(body_len); |
| 7370 | |
| 7371 | h1m.flags |= H1_MF_CLEN; |
| 7372 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 7373 | goto fail; |
| 7374 | } |
| 7375 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 7376 | h1m.flags |= H1_MF_XFER_LEN; |
| 7377 | |
| 7378 | /* Update HTX start-line flags */ |
| 7379 | if (h1m.flags & H1_MF_XFER_ENC) |
| 7380 | flags |= HTX_SL_F_XFER_ENC; |
| 7381 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 7382 | flags |= HTX_SL_F_XFER_LEN; |
| 7383 | if (h1m.flags & H1_MF_CHNK) |
| 7384 | flags |= HTX_SL_F_CHNK; |
| 7385 | else if (h1m.flags & H1_MF_CLEN) |
| 7386 | flags |= HTX_SL_F_CLEN; |
| 7387 | if (h1m.body_len == 0) |
| 7388 | flags |= HTX_SL_F_BODYLESS; |
| 7389 | } |
| 7390 | sl->flags |= flags; |
| 7391 | |
| 7392 | |
| 7393 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7394 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7395 | goto fail; |
| 7396 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7397 | htx->flags |= HTX_FL_EOM; |
| 7398 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7399 | /* Now, forward the response and terminate the transaction */ |
| 7400 | s->txn->status = code; |
| 7401 | htx_to_buf(htx, &s->res.buf); |
| 7402 | if (!http_forward_proxy_resp(s, 1)) |
| 7403 | goto fail; |
| 7404 | |
| 7405 | return 1; |
| 7406 | |
| 7407 | fail: |
| 7408 | channel_htx_truncate(&s->res, htx); |
| 7409 | return 0; |
| 7410 | } |
| 7411 | |
| 7412 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 7413 | * processing is just aborted. Nothing is returned to the client and all |
| 7414 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 7415 | * is forwarded to the client before terminating the transaction. On success, |
| 7416 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 7417 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 7418 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7419 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7420 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7421 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 7422 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7423 | struct stream *s; |
| 7424 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7425 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 7426 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7427 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7428 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 7429 | * just end the execution of the current lua code. */ |
| 7430 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7431 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7432 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7433 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 7434 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7435 | struct channel *req = &s->req; |
| 7436 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 7437 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7438 | channel_auto_read(req); |
| 7439 | channel_abort(req); |
| 7440 | channel_auto_close(req); |
| 7441 | channel_erase(req); |
| 7442 | |
| 7443 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 7444 | channel_auto_read(res); |
| 7445 | channel_auto_close(res); |
| 7446 | channel_shutr_now(res); |
| 7447 | |
| 7448 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 7449 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 7450 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 7451 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7452 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 7453 | /* No reply or invalid reply */ |
| 7454 | s->txn->status = 0; |
| 7455 | http_reply_and_close(s, 0, NULL); |
| 7456 | } |
| 7457 | else { |
| 7458 | /* Remove extra args to have the reply on top of the stack */ |
| 7459 | if (lua_gettop(L) > 2) |
| 7460 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7461 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7462 | if (!hlua_txn_forward_reply(L, s)) { |
| 7463 | if (!(s->flags & SF_ERR_MASK)) |
| 7464 | s->flags |= SF_ERR_PRXCOND; |
| 7465 | lua_pushinteger(L, ACT_RET_ERR); |
| 7466 | WILL_LJMP(hlua_done(L)); |
| 7467 | return 0; /* Never reached */ |
| 7468 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 7469 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7470 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7471 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 7472 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 7473 | /* let's log the request time */ |
| 7474 | s->logs.tv_request = now; |
| 7475 | 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] | 7476 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7477 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 7478 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7479 | done: |
| 7480 | if (!(s->flags & SF_ERR_MASK)) |
| 7481 | s->flags |= SF_ERR_LOCAL; |
| 7482 | if (!(s->flags & SF_FINST_MASK)) |
| 7483 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 7484 | |
Christopher Faulet | e48d1dc | 2021-08-13 14:11:17 +0200 | [diff] [blame] | 7485 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) |
| 7486 | lua_pushinteger(L, -1); |
| 7487 | else |
| 7488 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7489 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7490 | return 0; |
| 7491 | } |
| 7492 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7493 | /* |
| 7494 | * |
| 7495 | * |
| 7496 | * Class REPLY |
| 7497 | * |
| 7498 | * |
| 7499 | */ |
| 7500 | |
| 7501 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 7502 | * free slots, the function fails and returns 0; |
| 7503 | */ |
| 7504 | static int hlua_txn_reply_new(lua_State *L) |
| 7505 | { |
| 7506 | struct hlua_txn *htxn; |
| 7507 | const char *reason, *body = NULL; |
| 7508 | int ret, status; |
| 7509 | |
| 7510 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 7511 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7512 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 7513 | WILL_LJMP(lua_error(L)); |
| 7514 | } |
| 7515 | |
| 7516 | /* Default value */ |
| 7517 | status = 200; |
| 7518 | reason = http_get_reason(status); |
| 7519 | |
| 7520 | if (lua_istable(L, 2)) { |
| 7521 | /* load status and reason from the table argument at index 2 */ |
| 7522 | ret = lua_getfield(L, 2, "status"); |
| 7523 | if (ret == LUA_TNIL) |
| 7524 | goto reason; |
| 7525 | else if (ret != LUA_TNUMBER) { |
| 7526 | /* invalid status: ignore the reason */ |
| 7527 | goto body; |
| 7528 | } |
| 7529 | status = lua_tointeger(L, -1); |
| 7530 | |
| 7531 | reason: |
| 7532 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 7533 | ret = lua_getfield(L, 2, "reason"); |
| 7534 | if (ret == LUA_TSTRING) |
| 7535 | reason = lua_tostring(L, -1); |
| 7536 | |
| 7537 | body: |
| 7538 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 7539 | ret = lua_getfield(L, 2, "body"); |
| 7540 | if (ret == LUA_TSTRING) |
| 7541 | body = lua_tostring(L, -1); |
| 7542 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 7543 | } |
| 7544 | |
| 7545 | /* Create the Reply table */ |
| 7546 | lua_newtable(L); |
| 7547 | |
| 7548 | /* Add status element */ |
| 7549 | lua_pushstring(L, "status"); |
| 7550 | lua_pushinteger(L, status); |
| 7551 | lua_settable(L, -3); |
| 7552 | |
| 7553 | /* Add reason element */ |
| 7554 | reason = http_get_reason(status); |
| 7555 | lua_pushstring(L, "reason"); |
| 7556 | lua_pushstring(L, reason); |
| 7557 | lua_settable(L, -3); |
| 7558 | |
| 7559 | /* Add body element, nil if undefined */ |
| 7560 | lua_pushstring(L, "body"); |
| 7561 | if (body) |
| 7562 | lua_pushstring(L, body); |
| 7563 | else |
| 7564 | lua_pushnil(L); |
| 7565 | lua_settable(L, -3); |
| 7566 | |
| 7567 | /* Add headers element */ |
| 7568 | lua_pushstring(L, "headers"); |
| 7569 | lua_newtable(L); |
| 7570 | |
| 7571 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 7572 | if (lua_istable(L, 2)) { |
| 7573 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 7574 | ret = lua_getfield(L, 2, "headers"); |
| 7575 | if (ret == LUA_TTABLE) { |
| 7576 | /* stack: [ ... <headers:table>, <table> ] */ |
| 7577 | lua_pushnil(L); |
| 7578 | while (lua_next(L, -2) != 0) { |
| 7579 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 7580 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 7581 | /* invalid value type, skip it */ |
| 7582 | lua_pop(L, 1); |
| 7583 | continue; |
| 7584 | } |
| 7585 | |
| 7586 | |
| 7587 | /* Duplicate the key and swap it with the value. */ |
| 7588 | lua_pushvalue(L, -2); |
| 7589 | lua_insert(L, -2); |
| 7590 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 7591 | |
| 7592 | lua_newtable(L); |
| 7593 | lua_insert(L, -2); |
| 7594 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 7595 | |
| 7596 | if (lua_isstring(L, -1)) { |
| 7597 | /* push the value in the inner table */ |
| 7598 | lua_rawseti(L, -2, 1); |
| 7599 | } |
| 7600 | else { /* table */ |
| 7601 | lua_pushnil(L); |
| 7602 | while (lua_next(L, -2) != 0) { |
| 7603 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 7604 | if (!lua_isstring(L, -1)) { |
| 7605 | /* invalid value type, skip it*/ |
| 7606 | lua_pop(L, 1); |
| 7607 | continue; |
| 7608 | } |
| 7609 | /* push the value in the inner table */ |
| 7610 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 7611 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 7612 | } |
| 7613 | lua_pop(L, 1); |
| 7614 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 7615 | } |
| 7616 | |
| 7617 | /* push (k,v) on the stack in the headers table: |
| 7618 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 7619 | */ |
| 7620 | lua_settable(L, -5); |
| 7621 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 7622 | } |
| 7623 | } |
| 7624 | lua_pop(L, 1); |
| 7625 | } |
| 7626 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 7627 | lua_settable(L, -3); |
| 7628 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 7629 | |
| 7630 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 7631 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 7632 | lua_setmetatable(L, -2); |
| 7633 | return 1; |
| 7634 | } |
| 7635 | |
| 7636 | /* Set the reply status code, and optionally the reason. If no reason is |
| 7637 | * provided, the default one corresponding to the status code is used. |
| 7638 | */ |
| 7639 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 7640 | { |
| 7641 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7642 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 7643 | |
| 7644 | /* First argument (self) must be a table */ |
| 7645 | luaL_checktype(L, 1, LUA_TTABLE); |
| 7646 | |
| 7647 | if (status < 100 || status > 599) { |
| 7648 | lua_pushboolean(L, 0); |
| 7649 | return 1; |
| 7650 | } |
| 7651 | if (!reason) |
| 7652 | reason = http_get_reason(status); |
| 7653 | |
| 7654 | lua_pushinteger(L, status); |
| 7655 | lua_setfield(L, 1, "status"); |
| 7656 | |
| 7657 | lua_pushstring(L, reason); |
| 7658 | lua_setfield(L, 1, "reason"); |
| 7659 | |
| 7660 | lua_pushboolean(L, 1); |
| 7661 | return 1; |
| 7662 | } |
| 7663 | |
| 7664 | /* Add a header into the reply object. Each header name is associated to an |
| 7665 | * array of values in the "headers" table. If the header name is not found, a |
| 7666 | * new entry is created. |
| 7667 | */ |
| 7668 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 7669 | { |
| 7670 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7671 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 7672 | int ret; |
| 7673 | |
| 7674 | /* First argument (self) must be a table */ |
| 7675 | luaL_checktype(L, 1, LUA_TTABLE); |
| 7676 | |
| 7677 | /* Push in the stack the "headers" entry. */ |
| 7678 | ret = lua_getfield(L, 1, "headers"); |
| 7679 | if (ret != LUA_TTABLE) { |
| 7680 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 7681 | WILL_LJMP(lua_error(L)); |
| 7682 | } |
| 7683 | |
| 7684 | /* check if the header is already registered. If not, register it. */ |
| 7685 | ret = lua_getfield(L, -1, name); |
| 7686 | if (ret == LUA_TNIL) { |
| 7687 | /* Entry not found. */ |
| 7688 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 7689 | |
| 7690 | /* Insert the new header name in the array in the top of the stack. |
| 7691 | * It left the new array in the top of the stack. |
| 7692 | */ |
| 7693 | lua_newtable(L); |
| 7694 | lua_pushstring(L, name); |
| 7695 | lua_pushvalue(L, -2); |
| 7696 | lua_settable(L, -4); |
| 7697 | } |
| 7698 | else if (ret != LUA_TTABLE) { |
| 7699 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 7700 | WILL_LJMP(lua_error(L)); |
| 7701 | } |
| 7702 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 7703 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7704 | * the header value as new entry. |
| 7705 | */ |
| 7706 | lua_pushstring(L, value); |
| 7707 | ret = lua_rawlen(L, -2); |
| 7708 | lua_rawseti(L, -2, ret + 1); |
| 7709 | |
| 7710 | lua_pushboolean(L, 1); |
| 7711 | return 1; |
| 7712 | } |
| 7713 | |
| 7714 | /* Remove all occurrences of a given header name. */ |
| 7715 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 7716 | { |
| 7717 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7718 | int ret; |
| 7719 | |
| 7720 | /* First argument (self) must be a table */ |
| 7721 | luaL_checktype(L, 1, LUA_TTABLE); |
| 7722 | |
| 7723 | /* Push in the stack the "headers" entry. */ |
| 7724 | ret = lua_getfield(L, 1, "headers"); |
| 7725 | if (ret != LUA_TTABLE) { |
| 7726 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 7727 | WILL_LJMP(lua_error(L)); |
| 7728 | } |
| 7729 | |
| 7730 | lua_pushstring(L, name); |
| 7731 | lua_pushnil(L); |
| 7732 | lua_settable(L, -3); |
| 7733 | |
| 7734 | lua_pushboolean(L, 1); |
| 7735 | return 1; |
| 7736 | } |
| 7737 | |
| 7738 | /* Set the reply's body. Overwrite any existing entry. */ |
| 7739 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 7740 | { |
| 7741 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7742 | |
| 7743 | /* First argument (self) must be a table */ |
| 7744 | luaL_checktype(L, 1, LUA_TTABLE); |
| 7745 | |
| 7746 | lua_pushstring(L, payload); |
| 7747 | lua_setfield(L, 1, "body"); |
| 7748 | |
| 7749 | lua_pushboolean(L, 1); |
| 7750 | return 1; |
| 7751 | } |
| 7752 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7753 | __LJMP static int hlua_log(lua_State *L) |
| 7754 | { |
| 7755 | int level; |
| 7756 | const char *msg; |
| 7757 | |
| 7758 | MAY_LJMP(check_args(L, 2, "log")); |
| 7759 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 7760 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7761 | |
| 7762 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 7763 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 7764 | |
| 7765 | hlua_sendlog(NULL, level, msg); |
| 7766 | return 0; |
| 7767 | } |
| 7768 | |
| 7769 | __LJMP static int hlua_log_debug(lua_State *L) |
| 7770 | { |
| 7771 | const char *msg; |
| 7772 | |
| 7773 | MAY_LJMP(check_args(L, 1, "debug")); |
| 7774 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7775 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 7776 | return 0; |
| 7777 | } |
| 7778 | |
| 7779 | __LJMP static int hlua_log_info(lua_State *L) |
| 7780 | { |
| 7781 | const char *msg; |
| 7782 | |
| 7783 | MAY_LJMP(check_args(L, 1, "info")); |
| 7784 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7785 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 7786 | return 0; |
| 7787 | } |
| 7788 | |
| 7789 | __LJMP static int hlua_log_warning(lua_State *L) |
| 7790 | { |
| 7791 | const char *msg; |
| 7792 | |
| 7793 | MAY_LJMP(check_args(L, 1, "warning")); |
| 7794 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7795 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 7796 | return 0; |
| 7797 | } |
| 7798 | |
| 7799 | __LJMP static int hlua_log_alert(lua_State *L) |
| 7800 | { |
| 7801 | const char *msg; |
| 7802 | |
| 7803 | MAY_LJMP(check_args(L, 1, "alert")); |
| 7804 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7805 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7806 | return 0; |
| 7807 | } |
| 7808 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 7809 | __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] | 7810 | { |
| 7811 | int wakeup_ms = lua_tointeger(L, -1); |
| 7812 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 7813 | 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] | 7814 | return 0; |
| 7815 | } |
| 7816 | |
| 7817 | __LJMP static int hlua_sleep(lua_State *L) |
| 7818 | { |
| 7819 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 7820 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7821 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 7822 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7823 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 7824 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 7825 | wakeup_ms = tick_add(now_ms, delay); |
| 7826 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7827 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 7828 | 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] | 7829 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7830 | } |
| 7831 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 7832 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7833 | { |
| 7834 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 7835 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7836 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 7837 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7838 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 7839 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 7840 | wakeup_ms = tick_add(now_ms, delay); |
| 7841 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7842 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 7843 | 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] | 7844 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7845 | } |
| 7846 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 7847 | /* This functionis an LUA binding. it permits to give back |
| 7848 | * the hand at the HAProxy scheduler. It is used when the |
| 7849 | * LUA processing consumes a lot of time. |
| 7850 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 7851 | __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] | 7852 | { |
| 7853 | return 0; |
| 7854 | } |
| 7855 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 7856 | __LJMP static int hlua_yield(lua_State *L) |
| 7857 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 7858 | 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] | 7859 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 7860 | } |
| 7861 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 7862 | /* This function change the nice of the currently executed |
| 7863 | * task. It is used set low or high priority at the current |
| 7864 | * task. |
| 7865 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7866 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 7867 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7868 | struct hlua *hlua; |
| 7869 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 7870 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7871 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7872 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 7873 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7874 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 7875 | hlua = hlua_gethlua(L); |
| 7876 | |
| 7877 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 7878 | if (!hlua || !hlua->task) |
| 7879 | return 0; |
| 7880 | |
| 7881 | if (nice < -1024) |
| 7882 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7883 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 7884 | nice = 1024; |
| 7885 | |
| 7886 | hlua->task->nice = nice; |
| 7887 | return 0; |
| 7888 | } |
| 7889 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7890 | /* 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] | 7891 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 7892 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 7893 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7894 | * |
| 7895 | * Task wrapper are longjmp safe because the only one Lua code |
| 7896 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7897 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 7898 | 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] | 7899 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 7900 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7901 | enum hlua_exec status; |
| 7902 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 7903 | if (task->thread_mask == MAX_THREADS_MASK) |
| 7904 | task_set_affinity(task, tid_bit); |
| 7905 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7906 | /* If it is the first call to the task, we must initialize the |
| 7907 | * execution timeouts. |
| 7908 | */ |
| 7909 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7910 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7911 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7912 | /* Execute the Lua code. */ |
| 7913 | status = hlua_ctx_resume(hlua, 1); |
| 7914 | |
| 7915 | switch (status) { |
| 7916 | /* finished or yield */ |
| 7917 | case HLUA_E_OK: |
| 7918 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7919 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 7920 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7921 | break; |
| 7922 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 7923 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 7924 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 7925 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7926 | break; |
| 7927 | |
| 7928 | /* finished with error. */ |
| 7929 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7930 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7931 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7932 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 7933 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7934 | break; |
| 7935 | |
| 7936 | case HLUA_E_ERR: |
| 7937 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7938 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7939 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7940 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 7941 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7942 | break; |
| 7943 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 7944 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7945 | } |
| 7946 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7947 | /* This function is an LUA binding that register LUA function to be |
| 7948 | * executed after the HAProxy configuration parsing and before the |
| 7949 | * HAProxy scheduler starts. This function expect only one LUA |
| 7950 | * argument that is a function. This function returns nothing, but |
| 7951 | * throws if an error is encountered. |
| 7952 | */ |
| 7953 | __LJMP static int hlua_register_init(lua_State *L) |
| 7954 | { |
| 7955 | struct hlua_init_function *init; |
| 7956 | int ref; |
| 7957 | |
| 7958 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 7959 | |
| 7960 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 7961 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7962 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7963 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7964 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7965 | |
| 7966 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 7967 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7968 | return 0; |
| 7969 | } |
| 7970 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7971 | /* This functio is an LUA binding. It permits to register a task |
| 7972 | * executed in parallel of the main HAroxy activity. The task is |
| 7973 | * created and it is set in the HAProxy scheduler. It can be called |
| 7974 | * from the "init" section, "post init" or during the runtime. |
| 7975 | * |
| 7976 | * Lua prototype: |
| 7977 | * |
| 7978 | * <none> core.register_task(<function>) |
| 7979 | */ |
| 7980 | static int hlua_register_task(lua_State *L) |
| 7981 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 7982 | struct hlua *hlua = NULL; |
| 7983 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7984 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 7985 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7986 | |
| 7987 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 7988 | |
| 7989 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 7990 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 7991 | /* Get the reference state. If the reference is NULL, L is the master |
| 7992 | * state, otherwise hlua->T is. |
| 7993 | */ |
| 7994 | hlua = hlua_gethlua(L); |
| 7995 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 7996 | /* we are in runtime processing */ |
| 7997 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 7998 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 7999 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8000 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8001 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8002 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8003 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8004 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8005 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8006 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8007 | /* We are in the common lua state, execute the task anywhere, |
| 8008 | * otherwise, inherit the current thread identifier |
| 8009 | */ |
| 8010 | if (state_id == 0) |
| 8011 | task = task_new(MAX_THREADS_MASK); |
| 8012 | else |
| 8013 | task = task_new(tid_bit); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8014 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8015 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8016 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8017 | task->context = hlua; |
| 8018 | task->process = hlua_process_task; |
| 8019 | |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8020 | if (!hlua_ctx_init(hlua, state_id, task, 1)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8021 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8022 | |
| 8023 | /* Restore the function in the stack. */ |
| 8024 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 8025 | hlua->nargs = 0; |
| 8026 | |
| 8027 | /* Schedule task. */ |
| 8028 | task_schedule(task, now_ms); |
| 8029 | |
| 8030 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8031 | |
| 8032 | alloc_error: |
| 8033 | task_destroy(task); |
| 8034 | hlua_ctx_destroy(hlua); |
| 8035 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8036 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8037 | } |
| 8038 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8039 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 8040 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 8041 | * resume converters. |
| 8042 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8043 | 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] | 8044 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8045 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8046 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8047 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8048 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8049 | if (!stream) |
| 8050 | return 0; |
| 8051 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8052 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8053 | * Lua context can be not initialized. This behavior |
| 8054 | * permits to save performances because a systematic |
| 8055 | * Lua initialization cause 5% performances loss. |
| 8056 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8057 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8058 | struct hlua *hlua; |
| 8059 | |
| 8060 | hlua = pool_alloc(pool_head_hlua); |
| 8061 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8062 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8063 | return 0; |
| 8064 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8065 | HLUA_INIT(hlua); |
| 8066 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8067 | 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] | 8068 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8069 | return 0; |
| 8070 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8071 | } |
| 8072 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8073 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8074 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8075 | |
| 8076 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8077 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8078 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8079 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8080 | else |
| 8081 | error = "critical error"; |
| 8082 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8083 | return 0; |
| 8084 | } |
| 8085 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8086 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8087 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8088 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8089 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8090 | return 0; |
| 8091 | } |
| 8092 | |
| 8093 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8094 | 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] | 8095 | |
| 8096 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8097 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8098 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8099 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8100 | return 0; |
| 8101 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8102 | hlua_smp2lua(stream->hlua->T, smp); |
| 8103 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8104 | |
| 8105 | /* push keywords in the stack. */ |
| 8106 | if (arg_p) { |
| 8107 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8108 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8109 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8110 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8111 | return 0; |
| 8112 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8113 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8114 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8115 | } |
| 8116 | } |
| 8117 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8118 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8119 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8120 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8121 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8122 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8123 | } |
| 8124 | |
| 8125 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8126 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8127 | /* finished. */ |
| 8128 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8129 | /* If the stack is empty, the function fails. */ |
| 8130 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8131 | return 0; |
| 8132 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8133 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8134 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8135 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8136 | return 1; |
| 8137 | |
| 8138 | /* yield. */ |
| 8139 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8140 | 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] | 8141 | return 0; |
| 8142 | |
| 8143 | /* finished with error. */ |
| 8144 | case HLUA_E_ERRMSG: |
| 8145 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8146 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8147 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8148 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8149 | return 0; |
| 8150 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8151 | case HLUA_E_ETMOUT: |
| 8152 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 8153 | return 0; |
| 8154 | |
| 8155 | case HLUA_E_NOMEM: |
| 8156 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 8157 | return 0; |
| 8158 | |
| 8159 | case HLUA_E_YIELD: |
| 8160 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 8161 | return 0; |
| 8162 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8163 | case HLUA_E_ERR: |
| 8164 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8165 | 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] | 8166 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8167 | |
| 8168 | default: |
| 8169 | return 0; |
| 8170 | } |
| 8171 | } |
| 8172 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8173 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 8174 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8175 | * resume sample-fetches. This function will be called by the sample |
| 8176 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8177 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8178 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 8179 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8180 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8181 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8182 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8183 | const char *error; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 8184 | unsigned int hflags = HLUA_TXN_NOTERM | HLUA_TXN_SMP_CTX; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8185 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8186 | if (!stream) |
| 8187 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8188 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8189 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8190 | * Lua context can be not initialized. This behavior |
| 8191 | * permits to save performances because a systematic |
| 8192 | * Lua initialization cause 5% performances loss. |
| 8193 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8194 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8195 | struct hlua *hlua; |
| 8196 | |
| 8197 | hlua = pool_alloc(pool_head_hlua); |
| 8198 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8199 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8200 | return 0; |
| 8201 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8202 | hlua->T = NULL; |
| 8203 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8204 | 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] | 8205 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8206 | return 0; |
| 8207 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8208 | } |
| 8209 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8210 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8211 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8212 | |
| 8213 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8214 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8215 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8216 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8217 | else |
| 8218 | error = "critical error"; |
| 8219 | 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] | 8220 | return 0; |
| 8221 | } |
| 8222 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8223 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8224 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8225 | 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] | 8226 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8227 | return 0; |
| 8228 | } |
| 8229 | |
| 8230 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8231 | 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] | 8232 | |
| 8233 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 8234 | 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] | 8235 | 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] | 8236 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8237 | return 0; |
| 8238 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8239 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8240 | |
| 8241 | /* push keywords in the stack. */ |
| 8242 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 8243 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8244 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8245 | 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] | 8246 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8247 | return 0; |
| 8248 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8249 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8250 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8251 | } |
| 8252 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8253 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8254 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8255 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8256 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8257 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8258 | } |
| 8259 | |
| 8260 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8261 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8262 | /* finished. */ |
| 8263 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8264 | /* If the stack is empty, the function fails. */ |
| 8265 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8266 | return 0; |
| 8267 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8268 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8269 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8270 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8271 | |
| 8272 | /* Set the end of execution flag. */ |
| 8273 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 8274 | return 1; |
| 8275 | |
| 8276 | /* yield. */ |
| 8277 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8278 | 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] | 8279 | return 0; |
| 8280 | |
| 8281 | /* finished with error. */ |
| 8282 | case HLUA_E_ERRMSG: |
| 8283 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8284 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8285 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8286 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8287 | return 0; |
| 8288 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8289 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8290 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 8291 | return 0; |
| 8292 | |
| 8293 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8294 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 8295 | return 0; |
| 8296 | |
| 8297 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8298 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 8299 | return 0; |
| 8300 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8301 | case HLUA_E_ERR: |
| 8302 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8303 | 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] | 8304 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8305 | |
| 8306 | default: |
| 8307 | return 0; |
| 8308 | } |
| 8309 | } |
| 8310 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8311 | /* This function is an LUA binding used for registering |
| 8312 | * "sample-conv" functions. It expects a converter name used |
| 8313 | * in the haproxy configuration file, and an LUA function. |
| 8314 | */ |
| 8315 | __LJMP static int hlua_register_converters(lua_State *L) |
| 8316 | { |
| 8317 | struct sample_conv_kw_list *sck; |
| 8318 | const char *name; |
| 8319 | int ref; |
| 8320 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8321 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8322 | struct sample_conv *sc; |
| 8323 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8324 | |
| 8325 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 8326 | |
| 8327 | /* First argument : converter name. */ |
| 8328 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8329 | |
| 8330 | /* Second argument : lua function. */ |
| 8331 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 8332 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8333 | /* Check if the converter is already registered */ |
| 8334 | trash = get_trash_chunk(); |
| 8335 | chunk_printf(trash, "lua.%s", name); |
| 8336 | sc = find_sample_conv(trash->area, trash->data); |
| 8337 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8338 | fcn = sc->private; |
| 8339 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8340 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 8341 | "This will become a hard error in version 2.5.\n", name); |
| 8342 | } |
| 8343 | fcn->function_ref[hlua_state_id] = ref; |
| 8344 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8345 | } |
| 8346 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8347 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8348 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8349 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8350 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8351 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8352 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8353 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8354 | |
| 8355 | /* Fill fcn. */ |
| 8356 | fcn->name = strdup(name); |
| 8357 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8358 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8359 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8360 | |
| 8361 | /* List head */ |
| 8362 | sck->list.n = sck->list.p = NULL; |
| 8363 | |
| 8364 | /* converter keyword. */ |
| 8365 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8366 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8367 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8368 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8369 | |
| 8370 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 8371 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 8372 | 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] | 8373 | sck->kw[0].val_args = NULL; |
| 8374 | sck->kw[0].in_type = SMP_T_STR; |
| 8375 | sck->kw[0].out_type = SMP_T_STR; |
| 8376 | sck->kw[0].private = fcn; |
| 8377 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8378 | /* Register this new converter */ |
| 8379 | sample_register_convs(sck); |
| 8380 | |
| 8381 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8382 | |
| 8383 | alloc_error: |
| 8384 | release_hlua_function(fcn); |
| 8385 | ha_free(&sck); |
| 8386 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8387 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8388 | } |
| 8389 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8390 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8391 | * "sample-fetch" functions. It expects a converter name used |
| 8392 | * in the haproxy configuration file, and an LUA function. |
| 8393 | */ |
| 8394 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 8395 | { |
| 8396 | const char *name; |
| 8397 | int ref; |
| 8398 | int len; |
| 8399 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8400 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8401 | struct sample_fetch *sf; |
| 8402 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8403 | |
| 8404 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 8405 | |
| 8406 | /* First argument : sample-fetch name. */ |
| 8407 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8408 | |
| 8409 | /* Second argument : lua function. */ |
| 8410 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 8411 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8412 | /* Check if the sample-fetch is already registered */ |
| 8413 | trash = get_trash_chunk(); |
| 8414 | chunk_printf(trash, "lua.%s", name); |
| 8415 | sf = find_sample_fetch(trash->area, trash->data); |
| 8416 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8417 | fcn = sf->private; |
| 8418 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8419 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 8420 | "This will become a hard error in version 2.5.\n", name); |
| 8421 | } |
| 8422 | fcn->function_ref[hlua_state_id] = ref; |
| 8423 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8424 | } |
| 8425 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8426 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8427 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8428 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8429 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8430 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8431 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8432 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8433 | |
| 8434 | /* Fill fcn. */ |
| 8435 | fcn->name = strdup(name); |
| 8436 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8437 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8438 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8439 | |
| 8440 | /* List head */ |
| 8441 | sfk->list.n = sfk->list.p = NULL; |
| 8442 | |
| 8443 | /* sample-fetch keyword. */ |
| 8444 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8445 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8446 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8447 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8448 | |
| 8449 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 8450 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 8451 | 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] | 8452 | sfk->kw[0].val_args = NULL; |
| 8453 | sfk->kw[0].out_type = SMP_T_STR; |
| 8454 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 8455 | sfk->kw[0].val = 0; |
| 8456 | sfk->kw[0].private = fcn; |
| 8457 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8458 | /* Register this new fetch. */ |
| 8459 | sample_register_fetches(sfk); |
| 8460 | |
| 8461 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8462 | |
| 8463 | alloc_error: |
| 8464 | release_hlua_function(fcn); |
| 8465 | ha_free(&sfk); |
| 8466 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8467 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8468 | } |
| 8469 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 8470 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8471 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 8472 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8473 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8474 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8475 | unsigned int delay; |
| 8476 | unsigned int wakeup_ms; |
| 8477 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8478 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 8479 | hlua = hlua_gethlua(L); |
| 8480 | if (!hlua) { |
| 8481 | return 0; |
| 8482 | } |
| 8483 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8484 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 8485 | |
| 8486 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 8487 | wakeup_ms = tick_add(now_ms, delay); |
| 8488 | hlua->wake_time = wakeup_ms; |
| 8489 | return 0; |
| 8490 | } |
| 8491 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8492 | /* This function is a wrapper to execute each LUA function declared as an action |
| 8493 | * wrapper during the initialisation period. This function may return any |
| 8494 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 8495 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 8496 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8497 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 8498 | 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] | 8499 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8500 | { |
| 8501 | char **arg; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 8502 | unsigned int hflags = HLUA_TXN_ACT_CTX; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8503 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8504 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 8505 | |
| 8506 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8507 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 8508 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 8509 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 8510 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 8511 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8512 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8513 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 8514 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8515 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8516 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8517 | * Lua context can be not initialized. This behavior |
| 8518 | * permits to save performances because a systematic |
| 8519 | * Lua initialization cause 5% performances loss. |
| 8520 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8521 | if (!s->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8522 | struct hlua *hlua; |
| 8523 | |
| 8524 | hlua = pool_alloc(pool_head_hlua); |
| 8525 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8526 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8527 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8528 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8529 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8530 | HLUA_INIT(hlua); |
| 8531 | s->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8532 | 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] | 8533 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8534 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8535 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8536 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8537 | } |
| 8538 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8539 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8540 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8541 | |
| 8542 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8543 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8544 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 8545 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8546 | else |
| 8547 | error = "critical error"; |
| 8548 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8549 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8550 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8551 | } |
| 8552 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8553 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8554 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8555 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8556 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8557 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8558 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8559 | } |
| 8560 | |
| 8561 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8562 | 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] | 8563 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8564 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 8565 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8566 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8567 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8568 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8569 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8570 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8571 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8572 | |
| 8573 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 8574 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8575 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8576 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8577 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8578 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8579 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8580 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8581 | lua_pushstring(s->hlua->T, *arg); |
| 8582 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8583 | } |
| 8584 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8585 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8586 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8587 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8588 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8589 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8590 | } |
| 8591 | |
| 8592 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 8593 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8594 | /* finished. */ |
| 8595 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8596 | /* Catch the return value */ |
| 8597 | if (lua_gettop(s->hlua->T) > 0) |
| 8598 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8599 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8600 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 8601 | if (act_ret == ACT_RET_YIELD) { |
| 8602 | if (flags & ACT_OPT_FINAL) |
| 8603 | goto err_yield; |
| 8604 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 8605 | if (dir == SMP_OPT_DIR_REQ) |
| 8606 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 8607 | s->hlua->wake_time); |
| 8608 | else |
| 8609 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 8610 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8611 | } |
| 8612 | goto end; |
| 8613 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8614 | /* yield. */ |
| 8615 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 8616 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 8617 | if (dir == SMP_OPT_DIR_REQ) |
| 8618 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 8619 | s->hlua->wake_time); |
| 8620 | else |
| 8621 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 8622 | s->hlua->wake_time); |
| 8623 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8624 | /* Some actions can be wake up when a "write" event |
| 8625 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8626 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8627 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 8628 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8629 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8630 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8631 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8632 | act_ret = ACT_RET_YIELD; |
| 8633 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8634 | |
| 8635 | /* finished with error. */ |
| 8636 | case HLUA_E_ERRMSG: |
| 8637 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8638 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8639 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8640 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8641 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8642 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8643 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8644 | 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] | 8645 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8646 | |
| 8647 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8648 | 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] | 8649 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8650 | |
| 8651 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 8652 | err_yield: |
| 8653 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8654 | 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] | 8655 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8656 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8657 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8658 | case HLUA_E_ERR: |
| 8659 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8660 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8661 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8662 | |
| 8663 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8664 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8665 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8666 | |
| 8667 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 8668 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 8669 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8670 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8671 | } |
| 8672 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 8673 | 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] | 8674 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 8675 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8676 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8677 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 8678 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 8679 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8680 | } |
| 8681 | |
| 8682 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 8683 | { |
| 8684 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8685 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8686 | struct task *task; |
| 8687 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8688 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8689 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8690 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8691 | if (!hlua) { |
| 8692 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8693 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8694 | return 0; |
| 8695 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8696 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8697 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8698 | ctx->ctx.hlua_apptcp.flags = 0; |
| 8699 | |
| 8700 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 8701 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8702 | if (!task) { |
| 8703 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8704 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8705 | return 0; |
| 8706 | } |
| 8707 | task->nice = 0; |
| 8708 | task->context = ctx; |
| 8709 | task->process = hlua_applet_wakeup; |
| 8710 | ctx->ctx.hlua_apptcp.task = task; |
| 8711 | |
| 8712 | /* In the execution wrappers linked with a stream, the |
| 8713 | * Lua context can be not initialized. This behavior |
| 8714 | * permits to save performances because a systematic |
| 8715 | * Lua initialization cause 5% performances loss. |
| 8716 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8717 | 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] | 8718 | 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] | 8719 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8720 | return 0; |
| 8721 | } |
| 8722 | |
| 8723 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 8724 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8725 | |
| 8726 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8727 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8728 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 8729 | error = lua_tostring(hlua->T, -1); |
| 8730 | else |
| 8731 | error = "critical error"; |
| 8732 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8733 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8734 | return 0; |
| 8735 | } |
| 8736 | |
| 8737 | /* Check stack available size. */ |
| 8738 | if (!lua_checkstack(hlua->T, 1)) { |
| 8739 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8740 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8741 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8742 | return 0; |
| 8743 | } |
| 8744 | |
| 8745 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8746 | 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] | 8747 | |
| 8748 | /* Create and and push object stream in the stack. */ |
| 8749 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 8750 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8751 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8752 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8753 | return 0; |
| 8754 | } |
| 8755 | hlua->nargs = 1; |
| 8756 | |
| 8757 | /* push keywords in the stack. */ |
| 8758 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 8759 | if (!lua_checkstack(hlua->T, 1)) { |
| 8760 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8761 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8762 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8763 | return 0; |
| 8764 | } |
| 8765 | lua_pushstring(hlua->T, *arg); |
| 8766 | hlua->nargs++; |
| 8767 | } |
| 8768 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8769 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8770 | |
| 8771 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 8772 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 8773 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8774 | |
| 8775 | return 1; |
| 8776 | } |
| 8777 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 8778 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8779 | { |
| 8780 | struct stream_interface *si = ctx->owner; |
| 8781 | struct stream *strm = si_strm(si); |
| 8782 | struct channel *res = si_ic(si); |
| 8783 | struct act_rule *rule = ctx->rule; |
| 8784 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8785 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8786 | |
| 8787 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 8788 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 8789 | /* eat the whole request */ |
| 8790 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8791 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 8792 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8793 | |
| 8794 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 8795 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 8796 | return; |
| 8797 | |
| 8798 | /* Execute the function. */ |
| 8799 | switch (hlua_ctx_resume(hlua, 1)) { |
| 8800 | /* finished. */ |
| 8801 | case HLUA_E_OK: |
| 8802 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 8803 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8804 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 8805 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8806 | res->flags |= CF_READ_NULL; |
| 8807 | si_shutr(si); |
| 8808 | return; |
| 8809 | |
| 8810 | /* yield. */ |
| 8811 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 8812 | if (hlua->wake_time != TICK_ETERNITY) |
| 8813 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8814 | return; |
| 8815 | |
| 8816 | /* finished with error. */ |
| 8817 | case HLUA_E_ERRMSG: |
| 8818 | /* Display log. */ |
| 8819 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8820 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8821 | lua_pop(hlua->T, 1); |
| 8822 | goto error; |
| 8823 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8824 | case HLUA_E_ETMOUT: |
| 8825 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8826 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8827 | goto error; |
| 8828 | |
| 8829 | case HLUA_E_NOMEM: |
| 8830 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8831 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8832 | goto error; |
| 8833 | |
| 8834 | case HLUA_E_YIELD: /* unexpected */ |
| 8835 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8836 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8837 | goto error; |
| 8838 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8839 | case HLUA_E_ERR: |
| 8840 | /* Display log. */ |
| 8841 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8842 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8843 | goto error; |
| 8844 | |
| 8845 | default: |
| 8846 | goto error; |
| 8847 | } |
| 8848 | |
| 8849 | error: |
| 8850 | |
| 8851 | /* For all other cases, just close the stream. */ |
| 8852 | si_shutw(si); |
| 8853 | si_shutr(si); |
| 8854 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 8855 | } |
| 8856 | |
| 8857 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 8858 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8859 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8860 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8861 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8862 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8863 | } |
| 8864 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8865 | /* The function returns 1 if the initialisation is complete, 0 if |
| 8866 | * an errors occurs and -1 if more data are required for initializing |
| 8867 | * the applet. |
| 8868 | */ |
| 8869 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 8870 | { |
| 8871 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8872 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8873 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8874 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8875 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8876 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8877 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8878 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8879 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8880 | if (!hlua) { |
| 8881 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8882 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8883 | return 0; |
| 8884 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8885 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8886 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8887 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 8888 | ctx->ctx.hlua_apphttp.flags = 0; |
| 8889 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 8890 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 8891 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 8892 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8893 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 8894 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8895 | if (!task) { |
| 8896 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8897 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8898 | return 0; |
| 8899 | } |
| 8900 | task->nice = 0; |
| 8901 | task->context = ctx; |
| 8902 | task->process = hlua_applet_wakeup; |
| 8903 | ctx->ctx.hlua_apphttp.task = task; |
| 8904 | |
| 8905 | /* In the execution wrappers linked with a stream, the |
| 8906 | * Lua context can be not initialized. This behavior |
| 8907 | * permits to save performances because a systematic |
| 8908 | * Lua initialization cause 5% performances loss. |
| 8909 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8910 | 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] | 8911 | 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] | 8912 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8913 | return 0; |
| 8914 | } |
| 8915 | |
| 8916 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 8917 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8918 | |
| 8919 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8920 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8921 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 8922 | error = lua_tostring(hlua->T, -1); |
| 8923 | else |
| 8924 | error = "critical error"; |
| 8925 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8926 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8927 | return 0; |
| 8928 | } |
| 8929 | |
| 8930 | /* Check stack available size. */ |
| 8931 | if (!lua_checkstack(hlua->T, 1)) { |
| 8932 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8933 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8934 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8935 | return 0; |
| 8936 | } |
| 8937 | |
| 8938 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8939 | 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] | 8940 | |
| 8941 | /* Create and and push object stream in the stack. */ |
| 8942 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 8943 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8944 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8945 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8946 | return 0; |
| 8947 | } |
| 8948 | hlua->nargs = 1; |
| 8949 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8950 | /* push keywords in the stack. */ |
| 8951 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 8952 | if (!lua_checkstack(hlua->T, 1)) { |
| 8953 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8954 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8955 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8956 | return 0; |
| 8957 | } |
| 8958 | lua_pushstring(hlua->T, *arg); |
| 8959 | hlua->nargs++; |
| 8960 | } |
| 8961 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8962 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8963 | |
| 8964 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 8965 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8966 | |
| 8967 | return 1; |
| 8968 | } |
| 8969 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 8970 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 8971 | { |
| 8972 | struct stream_interface *si = ctx->owner; |
| 8973 | struct stream *strm = si_strm(si); |
| 8974 | struct channel *req = si_oc(si); |
| 8975 | struct channel *res = si_ic(si); |
| 8976 | struct act_rule *rule = ctx->rule; |
| 8977 | struct proxy *px = strm->be; |
| 8978 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 8979 | struct htx *req_htx, *res_htx; |
| 8980 | |
| 8981 | res_htx = htx_from_buf(&res->buf); |
| 8982 | |
| 8983 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 8984 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 8985 | goto out; |
| 8986 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 8987 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 8988 | if (!b_size(&res->buf)) { |
| 8989 | si_rx_room_blk(si); |
| 8990 | goto out; |
| 8991 | } |
| 8992 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 8993 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 8994 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 8995 | |
| 8996 | /* Set the currently running flag. */ |
| 8997 | if (!HLUA_IS_RUNNING(hlua) && |
| 8998 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 8999 | if (!co_data(req)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9000 | si_cant_get(si); |
| 9001 | goto out; |
| 9002 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9003 | } |
| 9004 | |
| 9005 | /* Executes The applet if it is not done. */ |
| 9006 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 9007 | |
| 9008 | /* Execute the function. */ |
| 9009 | switch (hlua_ctx_resume(hlua, 1)) { |
| 9010 | /* finished. */ |
| 9011 | case HLUA_E_OK: |
| 9012 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 9013 | break; |
| 9014 | |
| 9015 | /* yield. */ |
| 9016 | case HLUA_E_AGAIN: |
| 9017 | if (hlua->wake_time != TICK_ETERNITY) |
| 9018 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9019 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9020 | |
| 9021 | /* finished with error. */ |
| 9022 | case HLUA_E_ERRMSG: |
| 9023 | /* Display log. */ |
| 9024 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9025 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9026 | lua_pop(hlua->T, 1); |
| 9027 | goto error; |
| 9028 | |
| 9029 | case HLUA_E_ETMOUT: |
| 9030 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9031 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9032 | goto error; |
| 9033 | |
| 9034 | case HLUA_E_NOMEM: |
| 9035 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9036 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9037 | goto error; |
| 9038 | |
| 9039 | case HLUA_E_YIELD: /* unexpected */ |
| 9040 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9041 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9042 | goto error; |
| 9043 | |
| 9044 | case HLUA_E_ERR: |
| 9045 | /* Display log. */ |
| 9046 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9047 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9048 | goto error; |
| 9049 | |
| 9050 | default: |
| 9051 | goto error; |
| 9052 | } |
| 9053 | } |
| 9054 | |
| 9055 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9056 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 9057 | goto done; |
| 9058 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9059 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 9060 | goto error; |
| 9061 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 9062 | /* no more data are expected. Don't add TLR because mux-h1 will take care of it */ |
| 9063 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9064 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 9065 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9066 | } |
| 9067 | |
| 9068 | done: |
| 9069 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9070 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9071 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9072 | si_shutr(si); |
| 9073 | } |
| 9074 | |
| 9075 | /* eat the whole request */ |
| 9076 | if (co_data(req)) { |
| 9077 | req_htx = htx_from_buf(&req->buf); |
| 9078 | co_htx_skip(req, req_htx, co_data(req)); |
| 9079 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9080 | } |
| 9081 | } |
| 9082 | |
| 9083 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9084 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9085 | return; |
| 9086 | |
| 9087 | error: |
| 9088 | |
| 9089 | /* If we are in HTTP mode, and we are not send any |
| 9090 | * data, return a 500 server error in best effort: |
| 9091 | * if there is no room available in the buffer, |
| 9092 | * just close the connection. |
| 9093 | */ |
| 9094 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 9095 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9096 | |
| 9097 | channel_erase(res); |
| 9098 | res->buf.data = b_data(err); |
| 9099 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 9100 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 9101 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9102 | } |
| 9103 | if (!(strm->flags & SF_ERR_MASK)) |
| 9104 | strm->flags |= SF_ERR_RESOURCE; |
| 9105 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 9106 | goto done; |
| 9107 | } |
| 9108 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9109 | static void hlua_applet_http_release(struct appctx *ctx) |
| 9110 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 9111 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9112 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9113 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9114 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9115 | } |
| 9116 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9117 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9118 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9119 | * |
| 9120 | * This function can fail with an abort() due to an Lua critical error. |
| 9121 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 9122 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9123 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9124 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 9125 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9126 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9127 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9128 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9129 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9130 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9131 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9132 | if (!rule->arg.hlua_rule) { |
| 9133 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9134 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9135 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9136 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9137 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 9138 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 9139 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9140 | if (!rule->arg.hlua_rule->args) { |
| 9141 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9142 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9143 | } |
| 9144 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9145 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9146 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9147 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9148 | /* Expect some arguments */ |
| 9149 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9150 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9151 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9152 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9153 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9154 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9155 | if (!rule->arg.hlua_rule->args[i]) { |
| 9156 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9157 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9158 | } |
| 9159 | (*cur_arg)++; |
| 9160 | } |
| 9161 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9162 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 9163 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9164 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9165 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9166 | |
| 9167 | error: |
| 9168 | if (rule->arg.hlua_rule) { |
| 9169 | if (rule->arg.hlua_rule->args) { |
| 9170 | for (i = 0; i < fcn->nargs; i++) |
| 9171 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 9172 | ha_free(&rule->arg.hlua_rule->args); |
| 9173 | } |
| 9174 | ha_free(&rule->arg.hlua_rule); |
| 9175 | } |
| 9176 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9177 | } |
| 9178 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9179 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 9180 | struct act_rule *rule, char **err) |
| 9181 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9182 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9183 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9184 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9185 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9186 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9187 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9188 | * the call of this analyzer. |
| 9189 | */ |
| 9190 | if (rule->from != ACT_F_HTTP_REQ) { |
| 9191 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 9192 | return ACT_RET_PRS_ERR; |
| 9193 | } |
| 9194 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9195 | /* Memory for the rule. */ |
| 9196 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 9197 | if (!rule->arg.hlua_rule) { |
| 9198 | memprintf(err, "out of memory error"); |
| 9199 | return ACT_RET_PRS_ERR; |
| 9200 | } |
| 9201 | |
| 9202 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9203 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9204 | |
| 9205 | /* TODO: later accept arguments. */ |
| 9206 | rule->arg.hlua_rule->args = NULL; |
| 9207 | |
| 9208 | /* Add applet pointer in the rule. */ |
| 9209 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 9210 | rule->applet.name = fcn->name; |
| 9211 | rule->applet.init = hlua_applet_http_init; |
| 9212 | rule->applet.fct = hlua_applet_http_fct; |
| 9213 | rule->applet.release = hlua_applet_http_release; |
| 9214 | rule->applet.timeout = hlua_timeout_applet; |
| 9215 | |
| 9216 | return ACT_RET_PRS_OK; |
| 9217 | } |
| 9218 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9219 | /* This function is an LUA binding used for registering |
| 9220 | * "sample-conv" functions. It expects a converter name used |
| 9221 | * in the haproxy configuration file, and an LUA function. |
| 9222 | */ |
| 9223 | __LJMP static int hlua_register_action(lua_State *L) |
| 9224 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9225 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9226 | const char *name; |
| 9227 | int ref; |
| 9228 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9229 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9230 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9231 | struct buffer *trash; |
| 9232 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9233 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9234 | /* Initialise the number of expected arguments at 0. */ |
| 9235 | nargs = 0; |
| 9236 | |
| 9237 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 9238 | 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] | 9239 | |
| 9240 | /* First argument : converter name. */ |
| 9241 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9242 | |
| 9243 | /* Second argument : environment. */ |
| 9244 | if (lua_type(L, 2) != LUA_TTABLE) |
| 9245 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9246 | |
| 9247 | /* Third argument : lua function. */ |
| 9248 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 9249 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9250 | /* 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] | 9251 | if (lua_gettop(L) >= 4) |
| 9252 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 9253 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9254 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9255 | lua_pushnil(L); |
| 9256 | while (lua_next(L, 2) != 0) { |
| 9257 | if (lua_type(L, -1) != LUA_TSTRING) |
| 9258 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9259 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9260 | /* Check if action exists */ |
| 9261 | trash = get_trash_chunk(); |
| 9262 | chunk_printf(trash, "lua.%s", name); |
| 9263 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 9264 | akw = tcp_req_cont_action(trash->area); |
| 9265 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 9266 | akw = tcp_res_cont_action(trash->area); |
| 9267 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 9268 | akw = action_http_req_custom(trash->area); |
| 9269 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 9270 | akw = action_http_res_custom(trash->area); |
| 9271 | } else { |
| 9272 | akw = NULL; |
| 9273 | } |
| 9274 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9275 | fcn = akw->private; |
| 9276 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9277 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 9278 | "This will become a hard error in version 2.5.\n", name); |
| 9279 | } |
| 9280 | fcn->function_ref[hlua_state_id] = ref; |
| 9281 | |
| 9282 | /* pop the environment string. */ |
| 9283 | lua_pop(L, 1); |
| 9284 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9285 | } |
| 9286 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9287 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 9288 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9289 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9290 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9291 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9292 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9293 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9294 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9295 | |
| 9296 | /* Fill fcn. */ |
| 9297 | fcn->name = strdup(name); |
| 9298 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9299 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9300 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9301 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 9302 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9303 | fcn->nargs = nargs; |
| 9304 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9305 | /* List head */ |
| 9306 | akl->list.n = akl->list.p = NULL; |
| 9307 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9308 | /* action keyword. */ |
| 9309 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9310 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9311 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9312 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9313 | |
| 9314 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 9315 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 9316 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9317 | akl->kw[0].private = fcn; |
| 9318 | akl->kw[0].parse = action_register_lua; |
| 9319 | |
| 9320 | /* select the action registering point. */ |
| 9321 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 9322 | tcp_req_cont_keywords_register(akl); |
| 9323 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 9324 | tcp_res_cont_keywords_register(akl); |
| 9325 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 9326 | http_req_keywords_register(akl); |
| 9327 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 9328 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9329 | else { |
| 9330 | release_hlua_function(fcn); |
| 9331 | if (akl) |
| 9332 | ha_free((char **)&(akl->kw[0].kw)); |
| 9333 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 9334 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9335 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 9336 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9337 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9338 | |
| 9339 | /* pop the environment string. */ |
| 9340 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9341 | |
| 9342 | /* reset for next loop */ |
| 9343 | akl = NULL; |
| 9344 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9345 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9346 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9347 | |
| 9348 | alloc_error: |
| 9349 | release_hlua_function(fcn); |
| 9350 | ha_free(&akl); |
| 9351 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9352 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9353 | } |
| 9354 | |
| 9355 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 9356 | struct act_rule *rule, char **err) |
| 9357 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9358 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9359 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 9360 | if (px->mode == PR_MODE_HTTP) { |
| 9361 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 9362 | return ACT_RET_PRS_ERR; |
| 9363 | } |
| 9364 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9365 | /* Memory for the rule. */ |
| 9366 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 9367 | if (!rule->arg.hlua_rule) { |
| 9368 | memprintf(err, "out of memory error"); |
| 9369 | return ACT_RET_PRS_ERR; |
| 9370 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9371 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9372 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9373 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9374 | |
| 9375 | /* TODO: later accept arguments. */ |
| 9376 | rule->arg.hlua_rule->args = NULL; |
| 9377 | |
| 9378 | /* Add applet pointer in the rule. */ |
| 9379 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 9380 | rule->applet.name = fcn->name; |
| 9381 | rule->applet.init = hlua_applet_tcp_init; |
| 9382 | rule->applet.fct = hlua_applet_tcp_fct; |
| 9383 | rule->applet.release = hlua_applet_tcp_release; |
| 9384 | rule->applet.timeout = hlua_timeout_applet; |
| 9385 | |
| 9386 | return 0; |
| 9387 | } |
| 9388 | |
| 9389 | /* This function is an LUA binding used for registering |
| 9390 | * "sample-conv" functions. It expects a converter name used |
| 9391 | * in the haproxy configuration file, and an LUA function. |
| 9392 | */ |
| 9393 | __LJMP static int hlua_register_service(lua_State *L) |
| 9394 | { |
| 9395 | struct action_kw_list *akl; |
| 9396 | const char *name; |
| 9397 | const char *env; |
| 9398 | int ref; |
| 9399 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9400 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9401 | struct buffer *trash; |
| 9402 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9403 | |
| 9404 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 9405 | |
| 9406 | /* First argument : converter name. */ |
| 9407 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9408 | |
| 9409 | /* Second argument : environment. */ |
| 9410 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 9411 | |
| 9412 | /* Third argument : lua function. */ |
| 9413 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 9414 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9415 | /* Check for service already registered */ |
| 9416 | trash = get_trash_chunk(); |
| 9417 | chunk_printf(trash, "lua.%s", name); |
| 9418 | akw = service_find(trash->area); |
| 9419 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9420 | fcn = akw->private; |
| 9421 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9422 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 9423 | "This will become a hard error in version 2.5.\n", name); |
| 9424 | } |
| 9425 | fcn->function_ref[hlua_state_id] = ref; |
| 9426 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9427 | } |
| 9428 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9429 | /* Allocate and fill the sample fetch keyword struct. */ |
| 9430 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 9431 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9432 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9433 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9434 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9435 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9436 | |
| 9437 | /* Fill fcn. */ |
| 9438 | len = strlen("<lua.>") + strlen(name) + 1; |
| 9439 | fcn->name = calloc(1, len); |
| 9440 | if (!fcn->name) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9441 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9442 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9443 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9444 | |
| 9445 | /* List head */ |
| 9446 | akl->list.n = akl->list.p = NULL; |
| 9447 | |
| 9448 | /* converter keyword. */ |
| 9449 | len = strlen("lua.") + strlen(name) + 1; |
| 9450 | akl->kw[0].kw = calloc(1, len); |
| 9451 | if (!akl->kw[0].kw) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9452 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9453 | |
| 9454 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 9455 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 9456 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9457 | if (strcmp(env, "tcp") == 0) |
| 9458 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9459 | else if (strcmp(env, "http") == 0) |
| 9460 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9461 | else { |
| 9462 | release_hlua_function(fcn); |
| 9463 | if (akl) |
| 9464 | ha_free((char **)&(akl->kw[0].kw)); |
| 9465 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 9466 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 9467 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9468 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9469 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 9470 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9471 | akl->kw[0].private = fcn; |
| 9472 | |
| 9473 | /* End of array. */ |
| 9474 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 9475 | |
| 9476 | /* Register this new converter */ |
| 9477 | service_keywords_register(akl); |
| 9478 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9479 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9480 | |
| 9481 | alloc_error: |
| 9482 | release_hlua_function(fcn); |
| 9483 | ha_free(&akl); |
| 9484 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9485 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9486 | } |
| 9487 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9488 | /* This function initialises Lua cli handler. It copies the |
| 9489 | * arguments in the Lua stack and create channel IO objects. |
| 9490 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 9491 | 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] | 9492 | { |
| 9493 | struct hlua *hlua; |
| 9494 | struct hlua_function *fcn; |
| 9495 | int i; |
| 9496 | const char *error; |
| 9497 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9498 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9499 | appctx->ctx.hlua_cli.fcn = private; |
| 9500 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9501 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9502 | if (!hlua) { |
| 9503 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 9504 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9505 | } |
| 9506 | HLUA_INIT(hlua); |
| 9507 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9508 | |
| 9509 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9510 | * 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] | 9511 | * applet_http. It is absolutely compatible. |
| 9512 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 9513 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9514 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 9515 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 9516 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9517 | } |
| 9518 | appctx->ctx.hlua_cli.task->nice = 0; |
| 9519 | appctx->ctx.hlua_cli.task->context = appctx; |
| 9520 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 9521 | |
| 9522 | /* Initialises the Lua context */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9523 | 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] | 9524 | 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] | 9525 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9526 | } |
| 9527 | |
| 9528 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9529 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9530 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9531 | error = lua_tostring(hlua->T, -1); |
| 9532 | else |
| 9533 | error = "critical error"; |
| 9534 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 9535 | goto error; |
| 9536 | } |
| 9537 | |
| 9538 | /* Check stack available size. */ |
| 9539 | if (!lua_checkstack(hlua->T, 2)) { |
| 9540 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 9541 | goto error; |
| 9542 | } |
| 9543 | |
| 9544 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9545 | 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] | 9546 | |
| 9547 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 9548 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9549 | */ |
| 9550 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 9551 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 9552 | goto error; |
| 9553 | } |
| 9554 | hlua->nargs = 1; |
| 9555 | |
| 9556 | /* push keywords in the stack. */ |
| 9557 | for (i = 0; *args[i]; i++) { |
| 9558 | /* Check stack available size. */ |
| 9559 | if (!lua_checkstack(hlua->T, 1)) { |
| 9560 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 9561 | goto error; |
| 9562 | } |
| 9563 | lua_pushstring(hlua->T, args[i]); |
| 9564 | hlua->nargs++; |
| 9565 | } |
| 9566 | |
| 9567 | /* We must initialize the execution timeouts. */ |
| 9568 | hlua->max_time = hlua_timeout_session; |
| 9569 | |
| 9570 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9571 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9572 | |
| 9573 | /* It's ok */ |
| 9574 | return 0; |
| 9575 | |
| 9576 | /* It's not ok. */ |
| 9577 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9578 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9579 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 9580 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9581 | return 1; |
| 9582 | } |
| 9583 | |
| 9584 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 9585 | { |
| 9586 | struct hlua *hlua; |
| 9587 | struct stream_interface *si; |
| 9588 | struct hlua_function *fcn; |
| 9589 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9590 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9591 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 9592 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9593 | |
| 9594 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 9595 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 9596 | return 1; |
| 9597 | |
| 9598 | /* Execute the function. */ |
| 9599 | switch (hlua_ctx_resume(hlua, 1)) { |
| 9600 | |
| 9601 | /* finished. */ |
| 9602 | case HLUA_E_OK: |
| 9603 | return 1; |
| 9604 | |
| 9605 | /* yield. */ |
| 9606 | case HLUA_E_AGAIN: |
| 9607 | /* We want write. */ |
| 9608 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 9609 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9610 | /* Set the timeout. */ |
| 9611 | if (hlua->wake_time != TICK_ETERNITY) |
| 9612 | task_schedule(hlua->task, hlua->wake_time); |
| 9613 | return 0; |
| 9614 | |
| 9615 | /* finished with error. */ |
| 9616 | case HLUA_E_ERRMSG: |
| 9617 | /* Display log. */ |
| 9618 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 9619 | fcn->name, lua_tostring(hlua->T, -1)); |
| 9620 | lua_pop(hlua->T, 1); |
| 9621 | return 1; |
| 9622 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9623 | case HLUA_E_ETMOUT: |
| 9624 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 9625 | fcn->name); |
| 9626 | return 1; |
| 9627 | |
| 9628 | case HLUA_E_NOMEM: |
| 9629 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 9630 | fcn->name); |
| 9631 | return 1; |
| 9632 | |
| 9633 | case HLUA_E_YIELD: /* unexpected */ |
| 9634 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 9635 | fcn->name); |
| 9636 | return 1; |
| 9637 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9638 | case HLUA_E_ERR: |
| 9639 | /* Display log. */ |
| 9640 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 9641 | fcn->name); |
| 9642 | return 1; |
| 9643 | |
| 9644 | default: |
| 9645 | return 1; |
| 9646 | } |
| 9647 | |
| 9648 | return 1; |
| 9649 | } |
| 9650 | |
| 9651 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 9652 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9653 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9654 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9655 | } |
| 9656 | |
| 9657 | /* This function is an LUA binding used for registering |
| 9658 | * new keywords in the cli. It expects a list of keywords |
| 9659 | * which are the "path". It is limited to 5 keywords. A |
| 9660 | * description of the command, a function to be executed |
| 9661 | * for the parsing and a function for io handlers. |
| 9662 | */ |
| 9663 | __LJMP static int hlua_register_cli(lua_State *L) |
| 9664 | { |
| 9665 | struct cli_kw_list *cli_kws; |
| 9666 | const char *message; |
| 9667 | int ref_io; |
| 9668 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 9669 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9670 | int index; |
| 9671 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9672 | struct buffer *trash; |
| 9673 | const char *kw[5]; |
| 9674 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 9675 | const char *errmsg; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9676 | |
| 9677 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 9678 | |
| 9679 | /* First argument : an array of maximum 5 keywords. */ |
| 9680 | if (!lua_istable(L, 1)) |
| 9681 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 9682 | |
| 9683 | /* Second argument : string with contextual message. */ |
| 9684 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 9685 | |
| 9686 | /* Third and fourth argument : lua function. */ |
| 9687 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 9688 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9689 | /* Check for CLI service already registered */ |
| 9690 | trash = get_trash_chunk(); |
| 9691 | index = 0; |
| 9692 | lua_pushnil(L); |
| 9693 | memset(kw, 0, sizeof(kw)); |
| 9694 | while (lua_next(L, 1) != 0) { |
| 9695 | if (index >= CLI_PREFIX_KW_NB) |
| 9696 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 9697 | if (lua_type(L, -1) != LUA_TSTRING) |
| 9698 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 9699 | kw[index] = lua_tostring(L, -1); |
| 9700 | if (index == 0) |
| 9701 | chunk_printf(trash, "%s", kw[index]); |
| 9702 | else |
| 9703 | chunk_appendf(trash, " %s", kw[index]); |
| 9704 | index++; |
| 9705 | lua_pop(L, 1); |
| 9706 | } |
| 9707 | cli_kw = cli_find_kw_exact((char **)kw); |
| 9708 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9709 | fcn = cli_kw->private; |
| 9710 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9711 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 9712 | "This will become a hard error in version 2.5.\n", trash->area); |
| 9713 | } |
| 9714 | fcn->function_ref[hlua_state_id] = ref_io; |
| 9715 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9716 | } |
| 9717 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9718 | /* Allocate and fill the sample fetch keyword struct. */ |
| 9719 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 9720 | if (!cli_kws) { |
| 9721 | errmsg = "Lua out of memory error."; |
| 9722 | goto error; |
| 9723 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9724 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 9725 | if (!fcn) { |
| 9726 | errmsg = "Lua out of memory error."; |
| 9727 | goto error; |
| 9728 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9729 | |
| 9730 | /* Fill path. */ |
| 9731 | index = 0; |
| 9732 | lua_pushnil(L); |
| 9733 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 9734 | if (index >= 5) { |
| 9735 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 9736 | goto error; |
| 9737 | } |
| 9738 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 9739 | errmsg = "1st argument must be a table filled with strings"; |
| 9740 | goto error; |
| 9741 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9742 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 9743 | if (!cli_kws->kw[0].str_kw[index]) { |
| 9744 | errmsg = "Lua out of memory error."; |
| 9745 | goto error; |
| 9746 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9747 | index++; |
| 9748 | lua_pop(L, 1); |
| 9749 | } |
| 9750 | |
| 9751 | /* Copy help message. */ |
| 9752 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 9753 | if (!cli_kws->kw[0].usage) { |
| 9754 | errmsg = "Lua out of memory error."; |
| 9755 | goto error; |
| 9756 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9757 | |
| 9758 | /* Fill fcn io handler. */ |
| 9759 | len = strlen("<lua.cli>") + 1; |
| 9760 | for (i = 0; i < index; i++) |
| 9761 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 9762 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 9763 | if (!fcn->name) { |
| 9764 | errmsg = "Lua out of memory error."; |
| 9765 | goto error; |
| 9766 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9767 | strncat((char *)fcn->name, "<lua.cli", len); |
| 9768 | for (i = 0; i < index; i++) { |
| 9769 | strncat((char *)fcn->name, ".", len); |
| 9770 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 9771 | } |
| 9772 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9773 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9774 | |
| 9775 | /* Fill last entries. */ |
| 9776 | cli_kws->kw[0].private = fcn; |
| 9777 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 9778 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 9779 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 9780 | |
| 9781 | /* Register this new converter */ |
| 9782 | cli_register_kw(cli_kws); |
| 9783 | |
| 9784 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 9785 | |
| 9786 | error: |
| 9787 | release_hlua_function(fcn); |
| 9788 | if (cli_kws) { |
| 9789 | for (i = 0; i < index; i++) |
| 9790 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 9791 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 9792 | } |
| 9793 | ha_free(&cli_kws); |
| 9794 | WILL_LJMP(luaL_error(L, errmsg)); |
| 9795 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9796 | } |
| 9797 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 9798 | static int hlua_filter_init_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 9799 | { |
| 9800 | struct hlua_flt_config *conf = fconf->conf; |
| 9801 | lua_State *L; |
| 9802 | int error, pos, state_id, flt_ref; |
| 9803 | |
| 9804 | state_id = reg_flt_to_stack_id(conf->reg); |
| 9805 | L = hlua_states[state_id]; |
| 9806 | pos = lua_gettop(L); |
| 9807 | |
| 9808 | /* The filter parsing function */ |
| 9809 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->fun_ref[state_id]); |
| 9810 | |
| 9811 | /* Push the filter class on the stack and resolve all callbacks */ |
| 9812 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->flt_ref[state_id]); |
| 9813 | |
| 9814 | /* Duplicate the filter class so each filter will have its own copy */ |
| 9815 | lua_newtable(L); |
| 9816 | lua_pushnil(L); |
| 9817 | |
| 9818 | while (lua_next(L, pos+2)) { |
| 9819 | lua_pushvalue(L, -2); |
| 9820 | lua_insert(L, -2); |
| 9821 | lua_settable(L, -4); |
| 9822 | } |
| 9823 | flt_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
| 9824 | |
| 9825 | /* Remove the original lua filter class from the stack */ |
| 9826 | lua_pop(L, 1); |
| 9827 | |
| 9828 | /* Push the copy on the stack */ |
| 9829 | lua_rawgeti(L, LUA_REGISTRYINDEX, flt_ref); |
| 9830 | |
| 9831 | /* extra args are pushed in a table */ |
| 9832 | lua_newtable(L); |
| 9833 | for (pos = 0; conf->args[pos]; pos++) { |
| 9834 | /* Check stack available size. */ |
| 9835 | if (!lua_checkstack(L, 1)) { |
| 9836 | ha_alert("Lua filter '%s' : Lua error : full stack.", conf->reg->name); |
| 9837 | goto error; |
| 9838 | } |
| 9839 | lua_pushstring(L, conf->args[pos]); |
| 9840 | lua_rawseti(L, -2, lua_rawlen(L, -2) + 1); |
| 9841 | } |
| 9842 | |
| 9843 | error = lua_pcall(L, 2, LUA_MULTRET, 0); |
| 9844 | switch (error) { |
| 9845 | case LUA_OK: |
| 9846 | /* replace the filter ref */ |
| 9847 | conf->ref[state_id] = flt_ref; |
| 9848 | break; |
| 9849 | case LUA_ERRRUN: |
| 9850 | ha_alert("Lua filter '%s' : runtime error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 9851 | goto error; |
| 9852 | case LUA_ERRMEM: |
| 9853 | ha_alert("Lua filter '%s' : out of memory error", conf->reg->name); |
| 9854 | goto error; |
| 9855 | case LUA_ERRERR: |
| 9856 | ha_alert("Lua filter '%s' : message handler error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 9857 | goto error; |
| 9858 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
| 9859 | case LUA_ERRGCMM: |
| 9860 | ha_alert("Lua filter '%s' : garbage collector error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 9861 | goto error; |
| 9862 | #endif |
| 9863 | default: |
| 9864 | ha_alert("Lua filter '%s' : unknonwn error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 9865 | goto error; |
| 9866 | } |
| 9867 | |
| 9868 | lua_settop(L, 0); |
| 9869 | return 0; |
| 9870 | |
| 9871 | error: |
| 9872 | lua_settop(L, 0); |
| 9873 | return -1; |
| 9874 | } |
| 9875 | |
| 9876 | static void hlua_filter_deinit_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 9877 | { |
| 9878 | struct hlua_flt_config *conf = fconf->conf; |
| 9879 | lua_State *L; |
| 9880 | int state_id; |
| 9881 | |
| 9882 | if (!conf) |
| 9883 | return; |
| 9884 | |
| 9885 | state_id = reg_flt_to_stack_id(conf->reg); |
| 9886 | L = hlua_states[state_id]; |
| 9887 | luaL_unref(L, LUA_REGISTRYINDEX, conf->ref[state_id]); |
| 9888 | } |
| 9889 | |
| 9890 | static int hlua_filter_init(struct proxy *px, struct flt_conf *fconf) |
| 9891 | { |
| 9892 | struct hlua_flt_config *conf = fconf->conf; |
| 9893 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 9894 | |
| 9895 | /* Rely on per-thread init for global scripts */ |
| 9896 | if (!state_id) |
| 9897 | return hlua_filter_init_per_thread(px, fconf); |
| 9898 | return 0; |
| 9899 | } |
| 9900 | |
| 9901 | static void hlua_filter_deinit(struct proxy *px, struct flt_conf *fconf) |
| 9902 | { |
| 9903 | |
| 9904 | if (fconf->conf) { |
| 9905 | struct hlua_flt_config *conf = fconf->conf; |
| 9906 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 9907 | int pos; |
| 9908 | |
| 9909 | /* Rely on per-thread deinit for global scripts */ |
| 9910 | if (!state_id) |
| 9911 | hlua_filter_deinit_per_thread(px, fconf); |
| 9912 | |
| 9913 | for (pos = 0; conf->args[pos]; pos++) |
| 9914 | free(conf->args[pos]); |
| 9915 | free(conf->args); |
| 9916 | } |
| 9917 | ha_free(&fconf->conf); |
| 9918 | ha_free((char **)&fconf->id); |
| 9919 | ha_free(&fconf->ops); |
| 9920 | } |
| 9921 | |
| 9922 | static int hlua_filter_new(struct stream *s, struct filter *filter) |
| 9923 | { |
| 9924 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 9925 | struct hlua_flt_ctx *flt_ctx = NULL; |
| 9926 | int ret = 1; |
| 9927 | |
| 9928 | /* In the execution wrappers linked with a stream, the |
| 9929 | * Lua context can be not initialized. This behavior |
| 9930 | * permits to save performances because a systematic |
| 9931 | * Lua initialization cause 5% performances loss. |
| 9932 | */ |
| 9933 | if (!s->hlua) { |
| 9934 | struct hlua *hlua; |
| 9935 | |
| 9936 | hlua = pool_alloc(pool_head_hlua); |
| 9937 | if (!hlua) { |
| 9938 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 9939 | conf->reg->name); |
| 9940 | ret = 0; |
| 9941 | goto end; |
| 9942 | } |
| 9943 | HLUA_INIT(hlua); |
| 9944 | s->hlua = hlua; |
| 9945 | if (!hlua_ctx_init(s->hlua, reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 9946 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 9947 | conf->reg->name); |
| 9948 | ret = 0; |
| 9949 | goto end; |
| 9950 | } |
| 9951 | } |
| 9952 | |
| 9953 | flt_ctx = pool_zalloc(pool_head_hlua_flt_ctx); |
| 9954 | if (!flt_ctx) { |
| 9955 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 9956 | conf->reg->name); |
| 9957 | ret = 0; |
| 9958 | goto end; |
| 9959 | } |
| 9960 | flt_ctx->hlua[0] = pool_alloc(pool_head_hlua); |
| 9961 | flt_ctx->hlua[1] = pool_alloc(pool_head_hlua); |
| 9962 | if (!flt_ctx->hlua[0] || !flt_ctx->hlua[1]) { |
| 9963 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 9964 | conf->reg->name); |
| 9965 | ret = 0; |
| 9966 | goto end; |
| 9967 | } |
| 9968 | HLUA_INIT(flt_ctx->hlua[0]); |
| 9969 | HLUA_INIT(flt_ctx->hlua[1]); |
| 9970 | if (!hlua_ctx_init(flt_ctx->hlua[0], reg_flt_to_stack_id(conf->reg), s->task, 0) || |
| 9971 | !hlua_ctx_init(flt_ctx->hlua[1], reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 9972 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 9973 | conf->reg->name); |
| 9974 | ret = 0; |
| 9975 | goto end; |
| 9976 | } |
| 9977 | |
| 9978 | if (!HLUA_IS_RUNNING(s->hlua)) { |
| 9979 | /* The following Lua calls can fail. */ |
| 9980 | if (!SET_SAFE_LJMP(s->hlua)) { |
| 9981 | const char *error; |
| 9982 | |
| 9983 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 9984 | error = lua_tostring(s->hlua->T, -1); |
| 9985 | else |
| 9986 | error = "critical error"; |
| 9987 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 9988 | ret = 0; |
| 9989 | goto end; |
| 9990 | } |
| 9991 | |
| 9992 | /* Check stack size. */ |
| 9993 | if (!lua_checkstack(s->hlua->T, 1)) { |
| 9994 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 9995 | ret = 0; |
| 9996 | goto end; |
| 9997 | } |
| 9998 | |
| 9999 | lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, conf->ref[s->hlua->state_id]); |
| 10000 | if (lua_getfield(s->hlua->T, -1, "new") != LUA_TFUNCTION) { |
| 10001 | SEND_ERR(s->be, "Lua filter '%s': 'new' field is not a function.\n", |
| 10002 | conf->reg->name); |
| 10003 | RESET_SAFE_LJMP(s->hlua); |
| 10004 | ret = 0; |
| 10005 | goto end; |
| 10006 | } |
| 10007 | lua_insert(s->hlua->T, -2); |
| 10008 | |
| 10009 | /* Push the copy on the stack */ |
| 10010 | s->hlua->nargs = 1; |
| 10011 | |
| 10012 | /* We must initialize the execution timeouts. */ |
| 10013 | s->hlua->max_time = hlua_timeout_session; |
| 10014 | |
| 10015 | /* At this point the execution is safe. */ |
| 10016 | RESET_SAFE_LJMP(s->hlua); |
| 10017 | } |
| 10018 | |
| 10019 | switch (hlua_ctx_resume(s->hlua, 0)) { |
| 10020 | case HLUA_E_OK: |
| 10021 | /* Nothing returned or not a table, ignore the filter for current stream */ |
| 10022 | if (!lua_gettop(s->hlua->T) || !lua_istable(s->hlua->T, 1)) { |
| 10023 | ret = 0; |
| 10024 | goto end; |
| 10025 | } |
| 10026 | |
| 10027 | /* Attached the filter pointer to the ctx */ |
| 10028 | lua_pushstring(s->hlua->T, "__filter"); |
| 10029 | lua_pushlightuserdata(s->hlua->T, filter); |
| 10030 | lua_settable(s->hlua->T, -3); |
| 10031 | |
| 10032 | /* Save a ref on the filter ctx */ |
| 10033 | lua_pushvalue(s->hlua->T, 1); |
| 10034 | flt_ctx->ref = luaL_ref(s->hlua->T, LUA_REGISTRYINDEX); |
| 10035 | filter->ctx = flt_ctx; |
| 10036 | break; |
| 10037 | case HLUA_E_ERRMSG: |
| 10038 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(s->hlua->T, -1)); |
| 10039 | ret = -1; |
| 10040 | goto end; |
| 10041 | case HLUA_E_ETMOUT: |
| 10042 | SEND_ERR(s->be, "Lua filter '%s' : 'new' execution timeout.\n", conf->reg->name); |
| 10043 | ret = 0; |
| 10044 | goto end; |
| 10045 | case HLUA_E_NOMEM: |
| 10046 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10047 | ret = 0; |
| 10048 | goto end; |
| 10049 | case HLUA_E_AGAIN: |
| 10050 | case HLUA_E_YIELD: |
| 10051 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10052 | " are not allowed from 'new' function.\n", conf->reg->name); |
| 10053 | ret = 0; |
| 10054 | goto end; |
| 10055 | case HLUA_E_ERR: |
| 10056 | SEND_ERR(s->be, "Lua filter '%s': 'new' returns an unknown error.\n", conf->reg->name); |
| 10057 | ret = 0; |
| 10058 | goto end; |
| 10059 | default: |
| 10060 | ret = 0; |
| 10061 | goto end; |
| 10062 | } |
| 10063 | |
| 10064 | end: |
| 10065 | if (s->hlua) |
| 10066 | lua_settop(s->hlua->T, 0); |
| 10067 | if (ret <= 0) { |
| 10068 | if (flt_ctx) { |
| 10069 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10070 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10071 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10072 | } |
| 10073 | } |
| 10074 | return ret; |
| 10075 | } |
| 10076 | |
| 10077 | static void hlua_filter_delete(struct stream *s, struct filter *filter) |
| 10078 | { |
| 10079 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10080 | |
| 10081 | luaL_unref(s->hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 10082 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10083 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10084 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10085 | filter->ctx = NULL; |
| 10086 | } |
| 10087 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 10088 | static int hlua_filter_from_payload(struct filter *filter) |
| 10089 | { |
| 10090 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10091 | |
| 10092 | return (flt_ctx && !!(flt_ctx->flags & HLUA_FLT_CTX_FL_PAYLOAD)); |
| 10093 | } |
| 10094 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10095 | static int hlua_filter_callback(struct stream *s, struct filter *filter, const char *fun, |
| 10096 | int dir, unsigned int flags) |
| 10097 | { |
| 10098 | struct hlua *flt_hlua; |
| 10099 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 10100 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10101 | unsigned int hflags = HLUA_TXN_FLT_CTX; |
| 10102 | int ret = 1; |
| 10103 | |
| 10104 | flt_hlua = flt_ctx->hlua[(dir == SMP_OPT_DIR_REQ ? 0 : 1)]; |
| 10105 | if (!flt_hlua) |
| 10106 | goto end; |
| 10107 | |
| 10108 | if (!HLUA_IS_RUNNING(flt_hlua)) { |
| 10109 | int extra_idx = lua_gettop(flt_hlua->T); |
| 10110 | |
| 10111 | /* The following Lua calls can fail. */ |
| 10112 | if (!SET_SAFE_LJMP(flt_hlua)) { |
| 10113 | const char *error; |
| 10114 | |
| 10115 | if (lua_type(flt_hlua->T, -1) == LUA_TSTRING) |
| 10116 | error = lua_tostring(flt_hlua->T, -1); |
| 10117 | else |
| 10118 | error = "critical error"; |
| 10119 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 10120 | goto end; |
| 10121 | } |
| 10122 | |
| 10123 | /* Check stack size. */ |
| 10124 | if (!lua_checkstack(flt_hlua->T, 3)) { |
| 10125 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10126 | RESET_SAFE_LJMP(flt_hlua); |
| 10127 | goto end; |
| 10128 | } |
| 10129 | |
| 10130 | lua_rawgeti(flt_hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 10131 | if (lua_getfield(flt_hlua->T, -1, fun) != LUA_TFUNCTION) { |
| 10132 | RESET_SAFE_LJMP(flt_hlua); |
| 10133 | goto end; |
| 10134 | } |
| 10135 | lua_insert(flt_hlua->T, -2); |
| 10136 | |
| 10137 | if (!hlua_txn_new(flt_hlua->T, s, s->be, dir, hflags)) { |
| 10138 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10139 | RESET_SAFE_LJMP(flt_hlua); |
| 10140 | goto end; |
| 10141 | } |
| 10142 | flt_hlua->nargs = 2; |
| 10143 | |
| 10144 | if (flags & HLUA_FLT_CB_ARG_CHN) { |
| 10145 | if (dir == SMP_OPT_DIR_REQ) |
| 10146 | lua_getfield(flt_hlua->T, -1, "req"); |
| 10147 | else |
| 10148 | lua_getfield(flt_hlua->T, -1, "res"); |
| 10149 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10150 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10151 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10152 | lua_settable(flt_hlua->T, -3); |
| 10153 | } |
| 10154 | flt_hlua->nargs++; |
| 10155 | } |
| 10156 | else if (flags & HLUA_FLT_CB_ARG_HTTP_MSG) { |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10157 | if (dir == SMP_OPT_DIR_REQ) |
| 10158 | lua_getfield(flt_hlua->T, -1, "http_req"); |
| 10159 | else |
| 10160 | lua_getfield(flt_hlua->T, -1, "http_res"); |
| 10161 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10162 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10163 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10164 | lua_settable(flt_hlua->T, -3); |
| 10165 | } |
| 10166 | flt_hlua->nargs++; |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10167 | } |
| 10168 | |
| 10169 | /* Check stack size. */ |
| 10170 | if (!lua_checkstack(flt_hlua->T, 1)) { |
| 10171 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10172 | RESET_SAFE_LJMP(flt_hlua); |
| 10173 | goto end; |
| 10174 | } |
| 10175 | |
| 10176 | while (extra_idx--) { |
| 10177 | lua_pushvalue(flt_hlua->T, 1); |
| 10178 | lua_remove(flt_hlua->T, 1); |
| 10179 | flt_hlua->nargs++; |
| 10180 | } |
| 10181 | |
| 10182 | /* We must initialize the execution timeouts. */ |
| 10183 | flt_hlua->max_time = hlua_timeout_session; |
| 10184 | |
| 10185 | /* At this point the execution is safe. */ |
| 10186 | RESET_SAFE_LJMP(flt_hlua); |
| 10187 | } |
| 10188 | |
| 10189 | switch (hlua_ctx_resume(flt_hlua, !(flags & HLUA_FLT_CB_FINAL))) { |
| 10190 | case HLUA_E_OK: |
| 10191 | /* Catch the return value if it required */ |
| 10192 | if ((flags & HLUA_FLT_CB_RETVAL) && lua_gettop(flt_hlua->T) > 0) { |
| 10193 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10194 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10195 | } |
| 10196 | |
| 10197 | /* Set timeout in the required channel. */ |
| 10198 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10199 | if (dir == SMP_OPT_DIR_REQ) |
| 10200 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10201 | else |
| 10202 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10203 | } |
| 10204 | break; |
| 10205 | case HLUA_E_AGAIN: |
| 10206 | /* Set timeout in the required channel. */ |
| 10207 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10208 | if (dir == SMP_OPT_DIR_REQ) |
| 10209 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10210 | else |
| 10211 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10212 | } |
| 10213 | /* Some actions can be wake up when a "write" event |
| 10214 | * is detected on a response channel. This is useful |
| 10215 | * only for actions targeted on the requests. |
| 10216 | */ |
| 10217 | if (HLUA_IS_WAKERESWR(flt_hlua)) |
| 10218 | s->res.flags |= CF_WAKE_WRITE; |
| 10219 | if (HLUA_IS_WAKEREQWR(flt_hlua)) |
| 10220 | s->req.flags |= CF_WAKE_WRITE; |
| 10221 | ret = 0; |
| 10222 | goto end; |
| 10223 | case HLUA_E_ERRMSG: |
| 10224 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(flt_hlua->T, -1)); |
| 10225 | ret = -1; |
| 10226 | goto end; |
| 10227 | case HLUA_E_ETMOUT: |
| 10228 | SEND_ERR(s->be, "Lua filter '%s' : '%s' callback execution timeout.\n", conf->reg->name, fun); |
| 10229 | goto end; |
| 10230 | case HLUA_E_NOMEM: |
| 10231 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10232 | goto end; |
| 10233 | case HLUA_E_YIELD: |
| 10234 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10235 | " are not allowed from '%s' callback.\n", conf->reg->name, fun); |
| 10236 | goto end; |
| 10237 | case HLUA_E_ERR: |
| 10238 | SEND_ERR(s->be, "Lua filter '%s': '%s' returns an unknown error.\n", conf->reg->name, fun); |
| 10239 | goto end; |
| 10240 | default: |
| 10241 | goto end; |
| 10242 | } |
| 10243 | |
| 10244 | |
| 10245 | end: |
| 10246 | return ret; |
| 10247 | } |
| 10248 | |
| 10249 | static int hlua_filter_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 10250 | { |
| 10251 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10252 | |
| 10253 | flt_ctx->flags = 0; |
| 10254 | return hlua_filter_callback(s, filter, "start_analyze", |
| 10255 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10256 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 10257 | } |
| 10258 | |
| 10259 | static int hlua_filter_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 10260 | { |
| 10261 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10262 | |
| 10263 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10264 | return hlua_filter_callback(s, filter, "end_analyze", |
| 10265 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10266 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 10267 | } |
| 10268 | |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10269 | static int hlua_filter_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 10270 | { |
| 10271 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10272 | |
| 10273 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10274 | return hlua_filter_callback(s, filter, "http_headers", |
| 10275 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10276 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10277 | } |
| 10278 | |
| 10279 | static int hlua_filter_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg, |
| 10280 | unsigned int offset, unsigned int len) |
| 10281 | { |
| 10282 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10283 | struct hlua *flt_hlua; |
| 10284 | int dir = (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 10285 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 10286 | int ret; |
| 10287 | |
| 10288 | flt_hlua = flt_ctx->hlua[idx]; |
| 10289 | flt_ctx->cur_off[idx] = offset; |
| 10290 | flt_ctx->cur_len[idx] = len; |
| 10291 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 10292 | ret = hlua_filter_callback(s, filter, "http_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10293 | if (ret != -1) { |
| 10294 | ret = flt_ctx->cur_len[idx]; |
| 10295 | if (lua_gettop(flt_hlua->T) > 0) { |
| 10296 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10297 | if (ret > flt_ctx->cur_len[idx]) |
| 10298 | ret = flt_ctx->cur_len[idx]; |
| 10299 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10300 | } |
| 10301 | } |
| 10302 | return ret; |
| 10303 | } |
| 10304 | |
| 10305 | static int hlua_filter_http_end(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 10306 | { |
| 10307 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10308 | |
| 10309 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10310 | return hlua_filter_callback(s, filter, "http_end", |
| 10311 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10312 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10313 | } |
| 10314 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10315 | static int hlua_filter_tcp_payload(struct stream *s, struct filter *filter, struct channel *chn, |
| 10316 | unsigned int offset, unsigned int len) |
| 10317 | { |
| 10318 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10319 | struct hlua *flt_hlua; |
| 10320 | int dir = (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 10321 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 10322 | int ret; |
| 10323 | |
| 10324 | flt_hlua = flt_ctx->hlua[idx]; |
| 10325 | flt_ctx->cur_off[idx] = offset; |
| 10326 | flt_ctx->cur_len[idx] = len; |
| 10327 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 10328 | ret = hlua_filter_callback(s, filter, "tcp_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_CHN)); |
| 10329 | if (ret != -1) { |
| 10330 | ret = flt_ctx->cur_len[idx]; |
| 10331 | if (lua_gettop(flt_hlua->T) > 0) { |
| 10332 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10333 | if (ret > flt_ctx->cur_len[idx]) |
| 10334 | ret = flt_ctx->cur_len[idx]; |
| 10335 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10336 | } |
| 10337 | } |
| 10338 | return ret; |
| 10339 | } |
| 10340 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10341 | static int hlua_filter_parse_fct(char **args, int *cur_arg, struct proxy *px, |
| 10342 | struct flt_conf *fconf, char **err, void *private) |
| 10343 | { |
| 10344 | struct hlua_reg_filter *reg_flt = private; |
| 10345 | lua_State *L; |
| 10346 | struct hlua_flt_config *conf = NULL; |
| 10347 | const char *flt_id = NULL; |
| 10348 | int state_id, pos, flt_flags = 0; |
| 10349 | struct flt_ops *hlua_flt_ops = NULL; |
| 10350 | |
| 10351 | state_id = reg_flt_to_stack_id(reg_flt); |
| 10352 | L = hlua_states[state_id]; |
| 10353 | |
| 10354 | /* Initialize the filter ops with default callbacks */ |
| 10355 | hlua_flt_ops = calloc(1, sizeof(*hlua_flt_ops)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10356 | if (!hlua_flt_ops) |
| 10357 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10358 | hlua_flt_ops->init = hlua_filter_init; |
| 10359 | hlua_flt_ops->deinit = hlua_filter_deinit; |
| 10360 | if (state_id) { |
| 10361 | /* Set per-thread callback if script is loaded per-thread */ |
| 10362 | hlua_flt_ops->init_per_thread = hlua_filter_init_per_thread; |
| 10363 | hlua_flt_ops->deinit_per_thread = hlua_filter_deinit_per_thread; |
| 10364 | } |
| 10365 | hlua_flt_ops->attach = hlua_filter_new; |
| 10366 | hlua_flt_ops->detach = hlua_filter_delete; |
| 10367 | |
| 10368 | /* Push the filter class on the stack and resolve all callbacks */ |
| 10369 | lua_rawgeti(L, LUA_REGISTRYINDEX, reg_flt->flt_ref[state_id]); |
| 10370 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10371 | if (lua_getfield(L, -1, "start_analyze") == LUA_TFUNCTION) |
| 10372 | hlua_flt_ops->channel_start_analyze = hlua_filter_start_analyze; |
| 10373 | lua_pop(L, 1); |
| 10374 | if (lua_getfield(L, -1, "end_analyze") == LUA_TFUNCTION) |
| 10375 | hlua_flt_ops->channel_end_analyze = hlua_filter_end_analyze; |
| 10376 | lua_pop(L, 1); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10377 | if (lua_getfield(L, -1, "http_headers") == LUA_TFUNCTION) |
| 10378 | hlua_flt_ops->http_headers = hlua_filter_http_headers; |
| 10379 | lua_pop(L, 1); |
| 10380 | if (lua_getfield(L, -1, "http_payload") == LUA_TFUNCTION) |
| 10381 | hlua_flt_ops->http_payload = hlua_filter_http_payload; |
| 10382 | lua_pop(L, 1); |
| 10383 | if (lua_getfield(L, -1, "http_end") == LUA_TFUNCTION) |
| 10384 | hlua_flt_ops->http_end = hlua_filter_http_end; |
| 10385 | lua_pop(L, 1); |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10386 | if (lua_getfield(L, -1, "tcp_payload") == LUA_TFUNCTION) |
| 10387 | hlua_flt_ops->tcp_payload = hlua_filter_tcp_payload; |
| 10388 | lua_pop(L, 1); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10389 | |
| 10390 | /* Get id and flags of the filter class */ |
| 10391 | if (lua_getfield(L, -1, "id") == LUA_TSTRING) |
| 10392 | flt_id = lua_tostring(L, -1); |
| 10393 | lua_pop(L, 1); |
| 10394 | if (lua_getfield(L, -1, "flags") == LUA_TNUMBER) |
| 10395 | flt_flags = lua_tointeger(L, -1); |
| 10396 | lua_pop(L, 1); |
| 10397 | |
| 10398 | /* Create the filter config */ |
| 10399 | conf = calloc(1, sizeof(*conf)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10400 | if (!conf) |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10401 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10402 | conf->reg = reg_flt; |
| 10403 | |
| 10404 | /* duplicate args */ |
| 10405 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++); |
| 10406 | conf->args = calloc(pos + 1, sizeof(*conf->args)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10407 | if (!conf->args) |
| 10408 | goto error; |
| 10409 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++) { |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10410 | conf->args[pos] = strdup(args[*cur_arg + 1 + pos]); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10411 | if (!conf->args[pos]) |
| 10412 | goto error; |
| 10413 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10414 | conf->args[pos] = NULL; |
| 10415 | *cur_arg += pos + 1; |
| 10416 | |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10417 | if (flt_id) { |
| 10418 | fconf->id = strdup(flt_id); |
| 10419 | if (!fconf->id) |
| 10420 | goto error; |
| 10421 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10422 | fconf->flags = flt_flags; |
| 10423 | fconf->conf = conf; |
| 10424 | fconf->ops = hlua_flt_ops; |
| 10425 | |
| 10426 | lua_settop(L, 0); |
| 10427 | return 0; |
| 10428 | |
| 10429 | error: |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10430 | 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] | 10431 | free(hlua_flt_ops); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10432 | if (conf && conf->args) { |
| 10433 | for (pos = 0; conf->args[pos]; pos++) |
| 10434 | free(conf->args[pos]); |
| 10435 | free(conf->args); |
| 10436 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10437 | free(conf); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10438 | free((char *)fconf->id); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10439 | lua_settop(L, 0); |
| 10440 | return -1; |
| 10441 | } |
| 10442 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10443 | __LJMP static int hlua_register_data_filter(lua_State *L) |
| 10444 | { |
| 10445 | struct filter *filter; |
| 10446 | struct channel *chn; |
| 10447 | |
| 10448 | MAY_LJMP(check_args(L, 2, "register_data_filter")); |
| 10449 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 10450 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 10451 | |
| 10452 | lua_getfield(L, 1, "__filter"); |
| 10453 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 10454 | filter = lua_touserdata (L, -1); |
| 10455 | lua_pop(L, 1); |
| 10456 | |
| 10457 | register_data_filter(chn_strm(chn), chn, filter); |
| 10458 | return 1; |
| 10459 | } |
| 10460 | |
| 10461 | __LJMP static int hlua_unregister_data_filter(lua_State *L) |
| 10462 | { |
| 10463 | struct filter *filter; |
| 10464 | struct channel *chn; |
| 10465 | |
| 10466 | MAY_LJMP(check_args(L, 2, "unregister_data_filter")); |
| 10467 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 10468 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 10469 | |
| 10470 | lua_getfield(L, 1, "__filter"); |
| 10471 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 10472 | filter = lua_touserdata (L, -1); |
| 10473 | lua_pop(L, 1); |
| 10474 | |
| 10475 | unregister_data_filter(chn_strm(chn), chn, filter); |
| 10476 | return 1; |
| 10477 | } |
| 10478 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10479 | /* This function is an LUA binding used for registering a filter. It expects a |
| 10480 | * fileter name used in the haproxy configuration file and a LUA function to |
| 10481 | * parse configuration arguments. |
| 10482 | */ |
| 10483 | __LJMP static int hlua_register_filter(lua_State *L) |
| 10484 | { |
| 10485 | struct buffer *trash; |
| 10486 | struct flt_kw_list *fkl; |
| 10487 | struct flt_kw *fkw; |
| 10488 | const char *name; |
| 10489 | struct hlua_reg_filter *reg_flt= NULL; |
| 10490 | int flt_ref, fun_ref; |
| 10491 | int len; |
| 10492 | |
| 10493 | MAY_LJMP(check_args(L, 3, "register_filter")); |
| 10494 | |
| 10495 | /* First argument : filter name. */ |
| 10496 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 10497 | |
| 10498 | /* Second argument : The filter class */ |
| 10499 | flt_ref = MAY_LJMP(hlua_checktable(L, 2)); |
| 10500 | |
| 10501 | /* Third argument : lua function. */ |
| 10502 | fun_ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 10503 | |
| 10504 | trash = get_trash_chunk(); |
| 10505 | chunk_printf(trash, "lua.%s", name); |
| 10506 | fkw = flt_find_kw(trash->area); |
| 10507 | if (fkw != NULL) { |
| 10508 | reg_flt = fkw->private; |
| 10509 | if (reg_flt->flt_ref[hlua_state_id] != -1 || reg_flt->fun_ref[hlua_state_id] != -1) { |
| 10510 | ha_warning("Trying to register filter 'lua.%s' more than once. " |
| 10511 | "This will become a hard error in version 2.5.\n", name); |
| 10512 | } |
| 10513 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 10514 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 10515 | return 0; |
| 10516 | } |
| 10517 | |
| 10518 | fkl = calloc(1, sizeof(*fkl) + sizeof(struct flt_kw) * 2); |
| 10519 | if (!fkl) |
| 10520 | goto alloc_error; |
| 10521 | fkl->scope = "HLUA"; |
| 10522 | |
| 10523 | reg_flt = new_hlua_reg_filter(name); |
| 10524 | if (!reg_flt) |
| 10525 | goto alloc_error; |
| 10526 | |
| 10527 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 10528 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 10529 | |
| 10530 | /* The filter keyword */ |
| 10531 | len = strlen("lua.") + strlen(name) + 1; |
| 10532 | fkl->kw[0].kw = calloc(1, len); |
| 10533 | if (!fkl->kw[0].kw) |
| 10534 | goto alloc_error; |
| 10535 | |
| 10536 | snprintf((char *)fkl->kw[0].kw, len, "lua.%s", name); |
| 10537 | |
| 10538 | fkl->kw[0].parse = hlua_filter_parse_fct; |
| 10539 | fkl->kw[0].private = reg_flt; |
| 10540 | memset(&fkl->kw[1], 0, sizeof(*fkl->kw)); |
| 10541 | |
| 10542 | /* Register this new filter */ |
| 10543 | flt_register_keywords(fkl); |
| 10544 | |
| 10545 | return 0; |
| 10546 | |
| 10547 | alloc_error: |
| 10548 | release_hlua_reg_filter(reg_flt); |
| 10549 | ha_free(&fkl); |
| 10550 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 10551 | return 0; /* Never reached */ |
| 10552 | } |
| 10553 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 10554 | 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] | 10555 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 10556 | char **err, unsigned int *timeout) |
| 10557 | { |
| 10558 | const char *error; |
| 10559 | |
| 10560 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 10561 | if (error == PARSE_TIME_OVER) { |
| 10562 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 10563 | args[1], args[0]); |
| 10564 | return -1; |
| 10565 | } |
| 10566 | else if (error == PARSE_TIME_UNDER) { |
| 10567 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 10568 | args[1], args[0]); |
| 10569 | return -1; |
| 10570 | } |
| 10571 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 10572 | memprintf(err, "%s: invalid timeout", args[0]); |
| 10573 | return -1; |
| 10574 | } |
| 10575 | return 0; |
| 10576 | } |
| 10577 | |
| 10578 | 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] | 10579 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 10580 | char **err) |
| 10581 | { |
| 10582 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 10583 | file, line, err, &hlua_timeout_session); |
| 10584 | } |
| 10585 | |
| 10586 | 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] | 10587 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 10588 | char **err) |
| 10589 | { |
| 10590 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 10591 | file, line, err, &hlua_timeout_task); |
| 10592 | } |
| 10593 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10594 | 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] | 10595 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10596 | char **err) |
| 10597 | { |
| 10598 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 10599 | file, line, err, &hlua_timeout_applet); |
| 10600 | } |
| 10601 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 10602 | 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] | 10603 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 10604 | char **err) |
| 10605 | { |
| 10606 | char *error; |
| 10607 | |
| 10608 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 10609 | if (*error != '\0') { |
| 10610 | memprintf(err, "%s: invalid number", args[0]); |
| 10611 | return -1; |
| 10612 | } |
| 10613 | return 0; |
| 10614 | } |
| 10615 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 10616 | 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] | 10617 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 10618 | char **err) |
| 10619 | { |
| 10620 | char *error; |
| 10621 | |
| 10622 | if (*(args[1]) == 0) { |
| 10623 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 10624 | return -1; |
| 10625 | } |
| 10626 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 10627 | if (*error != '\0') { |
| 10628 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 10629 | return -1; |
| 10630 | } |
| 10631 | return 0; |
| 10632 | } |
| 10633 | |
| 10634 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10635 | /* This function is called by the main configuration key "lua-load". It loads and |
| 10636 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 10637 | * the main lua entry point. |
| 10638 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 10639 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 10640 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10641 | * |
| 10642 | * In some error case, LUA set an error message in top of the stack. This function |
| 10643 | * 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] | 10644 | * |
| 10645 | * This function can fail with an abort() due to an Lua critical error. |
| 10646 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 10647 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10648 | */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 10649 | static int hlua_load_state(char *filename, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10650 | { |
| 10651 | int error; |
| 10652 | |
| 10653 | /* Just load and compile the file. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 10654 | error = luaL_loadfile(L, filename); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10655 | if (error) { |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 10656 | memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1)); |
| 10657 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10658 | return -1; |
| 10659 | } |
| 10660 | |
| 10661 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 10662 | error = lua_pcall(L, 0, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10663 | switch (error) { |
| 10664 | case LUA_OK: |
| 10665 | break; |
| 10666 | case LUA_ERRRUN: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 10667 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1)); |
| 10668 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10669 | return -1; |
| 10670 | case LUA_ERRMEM: |
Thierry Fournier | de6145f | 2020-11-29 00:55:53 +0100 | [diff] [blame] | 10671 | memprintf(err, "Lua out of memory error\n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10672 | return -1; |
| 10673 | case LUA_ERRERR: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 10674 | memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1)); |
| 10675 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10676 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 10677 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10678 | case LUA_ERRGCMM: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 10679 | memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1)); |
| 10680 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10681 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 10682 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10683 | default: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 10684 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1)); |
| 10685 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10686 | return -1; |
| 10687 | } |
| 10688 | |
| 10689 | return 0; |
| 10690 | } |
| 10691 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 10692 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 10693 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 10694 | char **err) |
| 10695 | { |
| 10696 | if (*(args[1]) == 0) { |
| 10697 | memprintf(err, "'%s' expects a file name as parameter.\n", args[0]); |
| 10698 | return -1; |
| 10699 | } |
| 10700 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10701 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10702 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10703 | ha_set_tid(0); |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 10704 | return hlua_load_state(args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 10705 | } |
| 10706 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10707 | 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] | 10708 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10709 | char **err) |
| 10710 | { |
| 10711 | int len; |
| 10712 | |
| 10713 | if (*(args[1]) == 0) { |
| 10714 | memprintf(err, "'%s' expects a file as parameter.\n", args[0]); |
| 10715 | return -1; |
| 10716 | } |
| 10717 | |
| 10718 | if (per_thread_load == NULL) { |
| 10719 | /* allocate the first entry large enough to store the final NULL */ |
| 10720 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 10721 | if (per_thread_load == NULL) { |
| 10722 | memprintf(err, "out of memory error"); |
| 10723 | return -1; |
| 10724 | } |
| 10725 | } |
| 10726 | |
| 10727 | /* count used entries */ |
| 10728 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 10729 | ; |
| 10730 | |
| 10731 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 10732 | if (per_thread_load == NULL) { |
| 10733 | memprintf(err, "out of memory error"); |
| 10734 | return -1; |
| 10735 | } |
| 10736 | |
| 10737 | per_thread_load[len] = strdup(args[1]); |
| 10738 | per_thread_load[len + 1] = NULL; |
| 10739 | |
| 10740 | if (per_thread_load[len] == NULL) { |
| 10741 | memprintf(err, "out of memory error"); |
| 10742 | return -1; |
| 10743 | } |
| 10744 | |
| 10745 | /* loading for thread 1 only */ |
| 10746 | hlua_state_id = 1; |
| 10747 | ha_set_tid(0); |
| 10748 | return hlua_load_state(args[1], hlua_states[1], err); |
| 10749 | } |
| 10750 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 10751 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 10752 | * in the given <ctx>. |
| 10753 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 10754 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 10755 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 10756 | lua_getglobal(L, "package"); /* push package variable */ |
| 10757 | lua_pushstring(L, path); /* push given path */ |
| 10758 | lua_pushstring(L, ";"); /* push semicolon */ |
| 10759 | lua_getfield(L, -3, type); /* push old path */ |
| 10760 | lua_concat(L, 3); /* concatenate to new path */ |
| 10761 | lua_setfield(L, -2, type); /* store new path */ |
| 10762 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 10763 | |
| 10764 | return 0; |
| 10765 | } |
| 10766 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 10767 | 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] | 10768 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 10769 | char **err) |
| 10770 | { |
| 10771 | char *path; |
| 10772 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 10773 | struct prepend_path *p = NULL; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10774 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 10775 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 10776 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 10777 | } |
| 10778 | |
| 10779 | if (!(*args[1])) { |
| 10780 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 10781 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 10782 | } |
| 10783 | path = args[1]; |
| 10784 | |
| 10785 | if (*args[2]) { |
| 10786 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 10787 | 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] | 10788 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 10789 | } |
| 10790 | type = args[2]; |
| 10791 | } |
| 10792 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10793 | p = calloc(1, sizeof(*p)); |
| 10794 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 10795 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 10796 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10797 | } |
| 10798 | p->path = strdup(path); |
| 10799 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 10800 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 10801 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10802 | } |
| 10803 | p->type = strdup(type); |
| 10804 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 10805 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 10806 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10807 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 10808 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10809 | |
| 10810 | hlua_prepend_path(hlua_states[0], type, path); |
| 10811 | hlua_prepend_path(hlua_states[1], type, path); |
| 10812 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 10813 | |
| 10814 | err2: |
| 10815 | free(p->type); |
| 10816 | free(p->path); |
| 10817 | err: |
| 10818 | free(p); |
| 10819 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 10820 | } |
| 10821 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10822 | /* configuration keywords declaration */ |
| 10823 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 10824 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 10825 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10826 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 10827 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 10828 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 10829 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 10830 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 10831 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10832 | { 0, NULL, NULL }, |
| 10833 | }}; |
| 10834 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 10835 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 10836 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 10837 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 10838 | /* This function can fail with an abort() due to an Lua critical error. |
| 10839 | * We are in the initialisation process of HAProxy, this abort() is |
| 10840 | * tolerated. |
| 10841 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 10842 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 10843 | { |
| 10844 | struct hlua_init_function *init; |
| 10845 | const char *msg; |
| 10846 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 10847 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 10848 | const char *kind; |
| 10849 | const char *trace; |
| 10850 | int return_status = 1; |
| 10851 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 10852 | int nres; |
| 10853 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 10854 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 10855 | /* disable memory limit checks if limit is not set */ |
| 10856 | if (!hlua_global_allocator.limit) |
| 10857 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 10858 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 10859 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 10860 | if (setjmp(safe_ljmp_env) != 0) { |
| 10861 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 10862 | if (lua_type(L, -1) == LUA_TSTRING) |
| 10863 | error = lua_tostring(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 10864 | else |
| 10865 | error = "critical error"; |
| 10866 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 10867 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 10868 | } else { |
| 10869 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 10870 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 10871 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 10872 | hlua_fcn_post_init(L); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 10873 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10874 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 10875 | lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 10876 | |
| 10877 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 10878 | ret = lua_resume(L, L, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 10879 | #else |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 10880 | ret = lua_resume(L, L, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 10881 | #endif |
| 10882 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 10883 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 10884 | |
| 10885 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 10886 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 10887 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 10888 | |
| 10889 | case LUA_ERRERR: |
| 10890 | kind = "message handler error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 10891 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 10892 | case LUA_ERRRUN: |
| 10893 | if (!kind) |
| 10894 | kind = "runtime error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 10895 | msg = lua_tostring(L, -1); |
| 10896 | lua_settop(L, 0); /* Empty the stack. */ |
| 10897 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 10898 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 10899 | if (msg) |
| 10900 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 10901 | else |
| 10902 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
| 10903 | return_status = 0; |
| 10904 | break; |
| 10905 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 10906 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 10907 | /* Unknown error */ |
| 10908 | kind = "Unknown error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 10909 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 10910 | case LUA_YIELD: |
| 10911 | /* yield is not configured at this step, this state doesn't happen */ |
| 10912 | if (!kind) |
| 10913 | kind = "yield not allowed"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 10914 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 10915 | case LUA_ERRMEM: |
| 10916 | if (!kind) |
| 10917 | kind = "out of memory error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 10918 | lua_settop(L, 0); |
| 10919 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 10920 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 10921 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 10922 | return_status = 0; |
| 10923 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 10924 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 10925 | if (!return_status) |
| 10926 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 10927 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 10928 | |
| 10929 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 10930 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 10931 | } |
| 10932 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 10933 | int hlua_post_init() |
| 10934 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10935 | int ret; |
| 10936 | int i; |
| 10937 | int errors; |
| 10938 | char *err = NULL; |
| 10939 | struct hlua_function *fcn; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10940 | struct hlua_reg_filter *reg_flt; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10941 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 10942 | #if USE_OPENSSL |
| 10943 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 10944 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 10945 | int saved_used_backed = global.ssl_used_backend; |
| 10946 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 10947 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 10948 | global.ssl_used_backend = saved_used_backed; |
| 10949 | } |
| 10950 | #endif |
| 10951 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10952 | /* Perform post init of common thread */ |
| 10953 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10954 | ha_set_tid(0); |
| 10955 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 10956 | if (ret == 0) |
| 10957 | return 0; |
| 10958 | |
| 10959 | /* init remaining lua states and load files */ |
| 10960 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 10961 | |
| 10962 | /* set thread context */ |
| 10963 | ha_set_tid(hlua_state_id - 1); |
| 10964 | |
| 10965 | /* Init lua state */ |
| 10966 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 10967 | |
| 10968 | /* Load lua files */ |
| 10969 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 10970 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 10971 | if (ret != 0) { |
| 10972 | ha_alert("Lua init: %s\n", err); |
| 10973 | return 0; |
| 10974 | } |
| 10975 | } |
| 10976 | } |
| 10977 | |
| 10978 | /* Reset thread context */ |
| 10979 | ha_set_tid(0); |
| 10980 | |
| 10981 | /* Execute post init for all states */ |
| 10982 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 10983 | |
| 10984 | /* set thread context */ |
| 10985 | ha_set_tid(hlua_state_id - 1); |
| 10986 | |
| 10987 | /* run post init */ |
| 10988 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 10989 | if (ret == 0) |
| 10990 | return 0; |
| 10991 | } |
| 10992 | |
| 10993 | /* Reset thread context */ |
| 10994 | ha_set_tid(0); |
| 10995 | |
| 10996 | /* control functions registering. Each function must have: |
| 10997 | * - only the function_ref[0] set positive and all other to -1 |
| 10998 | * - only the function_ref[0] set to -1 and all other positive |
| 10999 | * This ensure a same reference is not used both in shared |
| 11000 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11001 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11002 | * complicated to found for the end user. |
| 11003 | */ |
| 11004 | errors = 0; |
| 11005 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 11006 | ret = 0; |
| 11007 | for (i = 1; i < global.nbthread + 1; i++) { |
| 11008 | if (fcn->function_ref[i] == -1) |
| 11009 | ret--; |
| 11010 | else |
| 11011 | ret++; |
| 11012 | } |
| 11013 | if (abs(ret) != global.nbthread) { |
| 11014 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 11015 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 11016 | errors++; |
| 11017 | continue; |
| 11018 | } |
| 11019 | |
| 11020 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11021 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 11022 | "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] | 11023 | "exclusive.\n", fcn->name); |
| 11024 | errors++; |
| 11025 | } |
| 11026 | } |
| 11027 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11028 | /* Do the same with registered filters */ |
| 11029 | list_for_each_entry(reg_flt, &referenced_filters, l) { |
| 11030 | ret = 0; |
| 11031 | for (i = 1; i < global.nbthread + 1; i++) { |
| 11032 | if (reg_flt->flt_ref[i] == -1) |
| 11033 | ret--; |
| 11034 | else |
| 11035 | ret++; |
| 11036 | } |
| 11037 | if (abs(ret) != global.nbthread) { |
| 11038 | ha_alert("Lua filter '%s' is not referenced in all thread. " |
| 11039 | "Expect function in all thread or in none thread.\n", reg_flt->name); |
| 11040 | errors++; |
| 11041 | continue; |
| 11042 | } |
| 11043 | |
| 11044 | if ((reg_flt->flt_ref[0] == -1) == (ret < 0)) { |
| 11045 | ha_alert("Lua filter '%s' is referenced both ins shared Lua context (through lua-load) " |
| 11046 | "and per-thread Lua context (through lua-load-per-thread). these two context " |
| 11047 | "exclusive.\n", fcn->name); |
| 11048 | errors++; |
| 11049 | } |
| 11050 | } |
| 11051 | |
| 11052 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11053 | if (errors > 0) |
| 11054 | return 0; |
| 11055 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11056 | /* 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] | 11057 | * -1 in order to have probably a segfault if someone use it |
| 11058 | */ |
| 11059 | hlua_state_id = -1; |
| 11060 | |
| 11061 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11062 | } |
| 11063 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11064 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 11065 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 11066 | * 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] | 11067 | * allocation. <nsize> is the requested new size. A new allocation is |
| 11068 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11069 | * zero. This one verifies that the limits are respected but is optimized |
| 11070 | * for the fast case where limits are not used, hence stats are not updated. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11071 | */ |
| 11072 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 11073 | { |
| 11074 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11075 | size_t limit, old, new; |
| 11076 | |
Tim Duesterhus | 2258652 | 2021-01-08 10:35:33 +0100 | [diff] [blame] | 11077 | if (unlikely(!ptr && !nsize)) |
| 11078 | return NULL; |
| 11079 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11080 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 11081 | * for accounting anymore. |
| 11082 | */ |
Christopher Faulet | cc2c4f8 | 2021-03-24 14:52:24 +0100 | [diff] [blame] | 11083 | if (likely(~zone->limit == 0)) |
| 11084 | return realloc(ptr, nsize); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11085 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 11086 | if (!ptr) |
| 11087 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11088 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11089 | /* enforce strict limits across all threads */ |
| 11090 | limit = zone->limit; |
| 11091 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 11092 | do { |
| 11093 | new = old + nsize - osize; |
| 11094 | if (unlikely(nsize && limit && new > limit)) |
| 11095 | return NULL; |
| 11096 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11097 | |
| 11098 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11099 | |
| 11100 | if (unlikely(!ptr && nsize)) // failed |
| 11101 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 11102 | |
| 11103 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11104 | return ptr; |
| 11105 | } |
| 11106 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11107 | /* 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] | 11108 | * We are in the initialisation process of HAProxy, this abort() is |
| 11109 | * tolerated. |
| 11110 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11111 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 11112 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11113 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11114 | int idx; |
| 11115 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11116 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11117 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 11118 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11119 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11120 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11121 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11122 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11123 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11124 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11125 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11126 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11127 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11128 | *context = NULL; |
| 11129 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 11130 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 11131 | * the Lua function can fail with an abort. We are in the initialisation |
| 11132 | * process of HAProxy, this abort() is tolerated. |
| 11133 | */ |
| 11134 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11135 | /* Call post initialisation function in safe environment. */ |
| 11136 | if (setjmp(safe_ljmp_env) != 0) { |
| 11137 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11138 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11139 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 11140 | else |
| 11141 | error_msg = "critical error"; |
| 11142 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 11143 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11144 | } else { |
| 11145 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 11146 | } |
| 11147 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11148 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11149 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 11150 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 11151 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 11152 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11153 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 11154 | #endif |
| 11155 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11156 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 11157 | #endif |
| 11158 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 11159 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11160 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11161 | /* Apply configured prepend path */ |
| 11162 | list_for_each_entry(pp, &prepend_path_list, l) |
| 11163 | hlua_prepend_path(L, pp->type, pp->path); |
| 11164 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11165 | /* |
| 11166 | * |
| 11167 | * Create "core" object. |
| 11168 | * |
| 11169 | */ |
| 11170 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 11171 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11172 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11173 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11174 | /* set the thread id */ |
| 11175 | hlua_class_const_int(L, "thread", thread_num); |
| 11176 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11177 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 11178 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11179 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11180 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11181 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11182 | hlua_class_function(L, "register_init", hlua_register_init); |
| 11183 | hlua_class_function(L, "register_task", hlua_register_task); |
| 11184 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 11185 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 11186 | hlua_class_function(L, "register_action", hlua_register_action); |
| 11187 | hlua_class_function(L, "register_service", hlua_register_service); |
| 11188 | hlua_class_function(L, "register_cli", hlua_register_cli); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11189 | hlua_class_function(L, "register_filter", hlua_register_filter); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11190 | hlua_class_function(L, "yield", hlua_yield); |
| 11191 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 11192 | hlua_class_function(L, "sleep", hlua_sleep); |
| 11193 | hlua_class_function(L, "msleep", hlua_msleep); |
| 11194 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 11195 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 11196 | hlua_class_function(L, "set_map", hlua_set_map); |
| 11197 | hlua_class_function(L, "del_map", hlua_del_map); |
| 11198 | hlua_class_function(L, "tcp", hlua_socket_new); |
| 11199 | hlua_class_function(L, "log", hlua_log); |
| 11200 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 11201 | hlua_class_function(L, "Info", hlua_log_info); |
| 11202 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 11203 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 11204 | hlua_class_function(L, "done", hlua_done); |
| 11205 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11206 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11207 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 11208 | |
| 11209 | /* |
| 11210 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11211 | * Create "act" object. |
| 11212 | * |
| 11213 | */ |
| 11214 | |
| 11215 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11216 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11217 | |
| 11218 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11219 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 11220 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 11221 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 11222 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 11223 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 11224 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 11225 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 11226 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11227 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11228 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 11229 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11230 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11231 | |
| 11232 | /* |
| 11233 | * |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11234 | * Create "Filter" object. |
| 11235 | * |
| 11236 | */ |
| 11237 | |
| 11238 | /* This table entry is the object "filter" base. */ |
| 11239 | lua_newtable(L); |
| 11240 | |
| 11241 | /* push flags and constants */ |
| 11242 | hlua_class_const_int(L, "CONTINUE", 1); |
| 11243 | hlua_class_const_int(L, "WAIT", 0); |
| 11244 | hlua_class_const_int(L, "ERROR", -1); |
| 11245 | |
| 11246 | hlua_class_const_int(L, "FLT_CFG_FL_HTX", FLT_CFG_FL_HTX); |
| 11247 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11248 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
| 11249 | hlua_class_function(L, "register_data_filter", hlua_register_data_filter); |
| 11250 | hlua_class_function(L, "unregister_data_filter", hlua_unregister_data_filter); |
| 11251 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11252 | lua_setglobal(L, "filter"); |
| 11253 | |
| 11254 | /* |
| 11255 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11256 | * Register class Map |
| 11257 | * |
| 11258 | */ |
| 11259 | |
| 11260 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11261 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11262 | |
| 11263 | /* register pattern types. */ |
| 11264 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11265 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 11266 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11267 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11268 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 11269 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11270 | |
| 11271 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11272 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11273 | |
| 11274 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11275 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11276 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11277 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11278 | lua_pushstring(L, "__index"); |
| 11279 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11280 | |
| 11281 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11282 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 11283 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11284 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11285 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11286 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 11287 | /* Register previous table in the registry with reference and named entry. |
| 11288 | * The function hlua_register_metatable() pops the stack, so we |
| 11289 | * previously create a copy of the table. |
| 11290 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11291 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 11292 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11293 | |
| 11294 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11295 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11296 | |
| 11297 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11298 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11299 | |
| 11300 | /* |
| 11301 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11302 | * Register class Channel |
| 11303 | * |
| 11304 | */ |
| 11305 | |
| 11306 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11307 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11308 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11309 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11310 | lua_pushstring(L, "__index"); |
| 11311 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11312 | |
| 11313 | /* Register . */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 11314 | hlua_class_function(L, "data", hlua_channel_get_data); |
| 11315 | hlua_class_function(L, "line", hlua_channel_get_line); |
| 11316 | hlua_class_function(L, "set", hlua_channel_set_data); |
| 11317 | hlua_class_function(L, "remove", hlua_channel_del_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11318 | hlua_class_function(L, "append", hlua_channel_append); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 11319 | hlua_class_function(L, "prepend", hlua_channel_prepend); |
| 11320 | hlua_class_function(L, "insert", hlua_channel_insert_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11321 | hlua_class_function(L, "send", hlua_channel_send); |
| 11322 | hlua_class_function(L, "forward", hlua_channel_forward); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 11323 | hlua_class_function(L, "input", hlua_channel_get_in_len); |
| 11324 | hlua_class_function(L, "output", hlua_channel_get_out_len); |
| 11325 | hlua_class_function(L, "may_recv", hlua_channel_may_recv); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11326 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 11327 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11328 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 11329 | /* Deprecated API */ |
| 11330 | hlua_class_function(L, "get", hlua_channel_get); |
| 11331 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 11332 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 11333 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 11334 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 11335 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11336 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11337 | |
| 11338 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11339 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11340 | |
| 11341 | /* |
| 11342 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 11343 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 11344 | * |
| 11345 | */ |
| 11346 | |
| 11347 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11348 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 11349 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11350 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11351 | lua_pushstring(L, "__index"); |
| 11352 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 11353 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11354 | /* Browse existing fetches and create the associated |
| 11355 | * object method. |
| 11356 | */ |
| 11357 | sf = NULL; |
| 11358 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11359 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 11360 | * by an underscore. |
| 11361 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11362 | strncpy(trash.area, sf->kw, trash.size); |
| 11363 | trash.area[trash.size - 1] = '\0'; |
| 11364 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11365 | if (*p == '.' || *p == '-' || *p == '+') |
| 11366 | *p = '_'; |
| 11367 | |
| 11368 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11369 | lua_pushstring(L, trash.area); |
| 11370 | lua_pushlightuserdata(L, sf); |
| 11371 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 11372 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11373 | } |
| 11374 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11375 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 11376 | |
| 11377 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11378 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 11379 | |
| 11380 | /* |
| 11381 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11382 | * Register class Converters |
| 11383 | * |
| 11384 | */ |
| 11385 | |
| 11386 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11387 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11388 | |
| 11389 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11390 | lua_pushstring(L, "__index"); |
| 11391 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11392 | |
| 11393 | /* Browse existing converters and create the associated |
| 11394 | * object method. |
| 11395 | */ |
| 11396 | sc = NULL; |
| 11397 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11398 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 11399 | * by an underscore. |
| 11400 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11401 | strncpy(trash.area, sc->kw, trash.size); |
| 11402 | trash.area[trash.size - 1] = '\0'; |
| 11403 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11404 | if (*p == '.' || *p == '-' || *p == '+') |
| 11405 | *p = '_'; |
| 11406 | |
| 11407 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11408 | lua_pushstring(L, trash.area); |
| 11409 | lua_pushlightuserdata(L, sc); |
| 11410 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 11411 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11412 | } |
| 11413 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11414 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11415 | |
| 11416 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11417 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11418 | |
| 11419 | /* |
| 11420 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11421 | * Register class HTTP |
| 11422 | * |
| 11423 | */ |
| 11424 | |
| 11425 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11426 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11427 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11428 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11429 | lua_pushstring(L, "__index"); |
| 11430 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11431 | |
| 11432 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11433 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 11434 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 11435 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 11436 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 11437 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 11438 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 11439 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 11440 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 11441 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 11442 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11443 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11444 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 11445 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 11446 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 11447 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 11448 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 11449 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 11450 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11451 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11452 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11453 | |
| 11454 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11455 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11456 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 11457 | /* |
| 11458 | * |
| 11459 | * Register class HTTPMessage |
| 11460 | * |
| 11461 | */ |
| 11462 | |
| 11463 | /* Create and fill the metatable. */ |
| 11464 | lua_newtable(L); |
| 11465 | |
| 11466 | /* Create and fille the __index entry. */ |
| 11467 | lua_pushstring(L, "__index"); |
| 11468 | lua_newtable(L); |
| 11469 | |
| 11470 | /* Register Lua functions. */ |
| 11471 | hlua_class_function(L, "is_resp", hlua_http_msg_is_resp); |
| 11472 | hlua_class_function(L, "get_stline", hlua_http_msg_get_stline); |
| 11473 | hlua_class_function(L, "get_headers", hlua_http_msg_get_headers); |
| 11474 | hlua_class_function(L, "del_header", hlua_http_msg_del_hdr); |
| 11475 | hlua_class_function(L, "rep_header", hlua_http_msg_rep_hdr); |
| 11476 | hlua_class_function(L, "rep_value", hlua_http_msg_rep_val); |
| 11477 | hlua_class_function(L, "add_header", hlua_http_msg_add_hdr); |
| 11478 | hlua_class_function(L, "set_header", hlua_http_msg_set_hdr); |
| 11479 | hlua_class_function(L, "set_method", hlua_http_msg_set_meth); |
| 11480 | hlua_class_function(L, "set_path", hlua_http_msg_set_path); |
| 11481 | hlua_class_function(L, "set_query", hlua_http_msg_set_query); |
| 11482 | hlua_class_function(L, "set_uri", hlua_http_msg_set_uri); |
| 11483 | hlua_class_function(L, "set_status", hlua_http_msg_set_status); |
| 11484 | hlua_class_function(L, "is_full", hlua_http_msg_is_full); |
| 11485 | hlua_class_function(L, "may_recv", hlua_http_msg_may_recv); |
| 11486 | hlua_class_function(L, "eom", hlua_http_msg_is_eom); |
| 11487 | hlua_class_function(L, "input", hlua_http_msg_get_in_len); |
| 11488 | hlua_class_function(L, "output", hlua_http_msg_get_out_len); |
| 11489 | |
| 11490 | hlua_class_function(L, "body", hlua_http_msg_get_body); |
| 11491 | hlua_class_function(L, "set", hlua_http_msg_set_data); |
| 11492 | hlua_class_function(L, "remove", hlua_http_msg_del_data); |
| 11493 | hlua_class_function(L, "append", hlua_http_msg_append); |
| 11494 | hlua_class_function(L, "prepend", hlua_http_msg_prepend); |
| 11495 | hlua_class_function(L, "insert", hlua_http_msg_insert_data); |
| 11496 | hlua_class_function(L, "set_eom", hlua_http_msg_set_eom); |
| 11497 | hlua_class_function(L, "unset_eom", hlua_http_msg_unset_eom); |
| 11498 | |
| 11499 | hlua_class_function(L, "send", hlua_http_msg_send); |
| 11500 | hlua_class_function(L, "forward", hlua_http_msg_forward); |
| 11501 | |
| 11502 | lua_rawset(L, -3); |
| 11503 | |
| 11504 | /* Register previous table in the registry with reference and named entry. */ |
| 11505 | class_http_msg_ref = hlua_register_metatable(L, CLASS_HTTP_MSG); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11506 | /* |
| 11507 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11508 | * Register class AppletTCP |
| 11509 | * |
| 11510 | */ |
| 11511 | |
| 11512 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11513 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11514 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11515 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11516 | lua_pushstring(L, "__index"); |
| 11517 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11518 | |
| 11519 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11520 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 11521 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 11522 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 11523 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 11524 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 11525 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 11526 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 11527 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11528 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11529 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11530 | |
| 11531 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11532 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11533 | |
| 11534 | /* |
| 11535 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 11536 | * Register class AppletHTTP |
| 11537 | * |
| 11538 | */ |
| 11539 | |
| 11540 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11541 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 11542 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11543 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11544 | lua_pushstring(L, "__index"); |
| 11545 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 11546 | |
| 11547 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11548 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 11549 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 11550 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 11551 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 11552 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 11553 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 11554 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 11555 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 11556 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 11557 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 11558 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 11559 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11560 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 11561 | |
| 11562 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11563 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 11564 | |
| 11565 | /* |
| 11566 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 11567 | * Register class TXN |
| 11568 | * |
| 11569 | */ |
| 11570 | |
| 11571 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11572 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 11573 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11574 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11575 | lua_pushstring(L, "__index"); |
| 11576 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 11577 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 11578 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11579 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 11580 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 11581 | hlua_class_function(L, "set_var", hlua_set_var); |
| 11582 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 11583 | hlua_class_function(L, "get_var", hlua_get_var); |
| 11584 | hlua_class_function(L, "done", hlua_txn_done); |
| 11585 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 11586 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 11587 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 11588 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 11589 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 11590 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 11591 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 11592 | hlua_class_function(L, "log", hlua_txn_log); |
| 11593 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 11594 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 11595 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 11596 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 11597 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11598 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 11599 | |
| 11600 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11601 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11602 | |
| 11603 | /* |
| 11604 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 11605 | * Register class reply |
| 11606 | * |
| 11607 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11608 | lua_newtable(L); |
| 11609 | lua_pushstring(L, "__index"); |
| 11610 | lua_newtable(L); |
| 11611 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 11612 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 11613 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 11614 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 11615 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 11616 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 11617 | |
| 11618 | |
| 11619 | /* |
| 11620 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11621 | * Register class Socket |
| 11622 | * |
| 11623 | */ |
| 11624 | |
| 11625 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11626 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11627 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11628 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11629 | lua_pushstring(L, "__index"); |
| 11630 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11631 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 11632 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11633 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 11634 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11635 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 11636 | hlua_class_function(L, "send", hlua_socket_send); |
| 11637 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 11638 | hlua_class_function(L, "close", hlua_socket_close); |
| 11639 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 11640 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 11641 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 11642 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11643 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11644 | 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] | 11645 | |
| 11646 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11647 | lua_pushstring(L, "__gc"); |
| 11648 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 11649 | 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] | 11650 | |
| 11651 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11652 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11653 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 11654 | lua_atpanic(L, hlua_panic_safe); |
| 11655 | |
| 11656 | return L; |
| 11657 | } |
| 11658 | |
| 11659 | void hlua_init(void) { |
| 11660 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 11661 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 11662 | #ifdef USE_OPENSSL |
| 11663 | struct srv_kw *kw; |
| 11664 | int tmp_error; |
| 11665 | char *error; |
| 11666 | char *args[] = { /* SSL client configuration. */ |
| 11667 | "ssl", |
| 11668 | "verify", |
| 11669 | "none", |
| 11670 | NULL |
| 11671 | }; |
| 11672 | #endif |
| 11673 | |
| 11674 | /* Init post init function list head */ |
| 11675 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 11676 | LIST_INIT(&hlua_init_functions[i]); |
| 11677 | |
| 11678 | /* Init state for common/shared lua parts */ |
| 11679 | hlua_state_id = 0; |
| 11680 | ha_set_tid(0); |
| 11681 | hlua_states[0] = hlua_init_state(0); |
| 11682 | |
| 11683 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 11684 | hlua_state_id = 1; |
| 11685 | ha_set_tid(0); |
| 11686 | hlua_states[1] = hlua_init_state(1); |
| 11687 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11688 | /* Proxy and server configuration initialisation. */ |
William Lallemand | 6bb77b9 | 2021-07-28 15:48:16 +0200 | [diff] [blame] | 11689 | 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] | 11690 | if (!socket_proxy) { |
| 11691 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 11692 | exit(1); |
| 11693 | } |
| 11694 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11695 | |
| 11696 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11697 | socket_tcp = new_server(socket_proxy); |
| 11698 | if (!socket_tcp) { |
| 11699 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 11700 | exit(1); |
| 11701 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11702 | |
| 11703 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11704 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11705 | socket_ssl = new_server(socket_proxy); |
| 11706 | if (!socket_ssl) { |
| 11707 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 11708 | exit(1); |
| 11709 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11710 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11711 | socket_ssl->use_ssl = 1; |
| 11712 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11713 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 11714 | for (i = 0; args[i] != NULL; i++) { |
| 11715 | 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] | 11716 | /* |
| 11717 | * |
| 11718 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 11719 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11720 | * features like client certificates and ssl_verify. |
| 11721 | * |
| 11722 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11723 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11724 | if (tmp_error != 0) { |
| 11725 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 11726 | abort(); /* This must be never arrives because the command line |
| 11727 | not editable by the user. */ |
| 11728 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 11729 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11730 | } |
| 11731 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11732 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 11733 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 11734 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 11735 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 11736 | static void hlua_deinit() |
| 11737 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 11738 | int thr; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11739 | struct hlua_reg_filter *reg_flt, *reg_flt_bck; |
| 11740 | |
| 11741 | list_for_each_entry_safe(reg_flt, reg_flt_bck, &referenced_filters, l) |
| 11742 | release_hlua_reg_filter(reg_flt); |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 11743 | |
| 11744 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 11745 | if (hlua_states[thr]) |
| 11746 | lua_close(hlua_states[thr]); |
| 11747 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 11748 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11749 | free_server(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 11750 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 11751 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11752 | free_server(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 11753 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 11754 | |
| 11755 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 11756 | } |
| 11757 | |
| 11758 | REGISTER_POST_DEINIT(hlua_deinit); |
| 11759 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 11760 | static void hlua_register_build_options(void) |
| 11761 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 11762 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 11763 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 11764 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 11765 | hap_register_build_opts(ptr, 1); |
| 11766 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 11767 | |
| 11768 | INITCALL0(STG_REGISTER, hlua_register_build_options); |