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 | 4ad7310 | 2020-03-05 11:07:31 +0100 | [diff] [blame] | 7485 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7486 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7487 | return 0; |
| 7488 | } |
| 7489 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7490 | /* |
| 7491 | * |
| 7492 | * |
| 7493 | * Class REPLY |
| 7494 | * |
| 7495 | * |
| 7496 | */ |
| 7497 | |
| 7498 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 7499 | * free slots, the function fails and returns 0; |
| 7500 | */ |
| 7501 | static int hlua_txn_reply_new(lua_State *L) |
| 7502 | { |
| 7503 | struct hlua_txn *htxn; |
| 7504 | const char *reason, *body = NULL; |
| 7505 | int ret, status; |
| 7506 | |
| 7507 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 7508 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7509 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 7510 | WILL_LJMP(lua_error(L)); |
| 7511 | } |
| 7512 | |
| 7513 | /* Default value */ |
| 7514 | status = 200; |
| 7515 | reason = http_get_reason(status); |
| 7516 | |
| 7517 | if (lua_istable(L, 2)) { |
| 7518 | /* load status and reason from the table argument at index 2 */ |
| 7519 | ret = lua_getfield(L, 2, "status"); |
| 7520 | if (ret == LUA_TNIL) |
| 7521 | goto reason; |
| 7522 | else if (ret != LUA_TNUMBER) { |
| 7523 | /* invalid status: ignore the reason */ |
| 7524 | goto body; |
| 7525 | } |
| 7526 | status = lua_tointeger(L, -1); |
| 7527 | |
| 7528 | reason: |
| 7529 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 7530 | ret = lua_getfield(L, 2, "reason"); |
| 7531 | if (ret == LUA_TSTRING) |
| 7532 | reason = lua_tostring(L, -1); |
| 7533 | |
| 7534 | body: |
| 7535 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 7536 | ret = lua_getfield(L, 2, "body"); |
| 7537 | if (ret == LUA_TSTRING) |
| 7538 | body = lua_tostring(L, -1); |
| 7539 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 7540 | } |
| 7541 | |
| 7542 | /* Create the Reply table */ |
| 7543 | lua_newtable(L); |
| 7544 | |
| 7545 | /* Add status element */ |
| 7546 | lua_pushstring(L, "status"); |
| 7547 | lua_pushinteger(L, status); |
| 7548 | lua_settable(L, -3); |
| 7549 | |
| 7550 | /* Add reason element */ |
| 7551 | reason = http_get_reason(status); |
| 7552 | lua_pushstring(L, "reason"); |
| 7553 | lua_pushstring(L, reason); |
| 7554 | lua_settable(L, -3); |
| 7555 | |
| 7556 | /* Add body element, nil if undefined */ |
| 7557 | lua_pushstring(L, "body"); |
| 7558 | if (body) |
| 7559 | lua_pushstring(L, body); |
| 7560 | else |
| 7561 | lua_pushnil(L); |
| 7562 | lua_settable(L, -3); |
| 7563 | |
| 7564 | /* Add headers element */ |
| 7565 | lua_pushstring(L, "headers"); |
| 7566 | lua_newtable(L); |
| 7567 | |
| 7568 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 7569 | if (lua_istable(L, 2)) { |
| 7570 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 7571 | ret = lua_getfield(L, 2, "headers"); |
| 7572 | if (ret == LUA_TTABLE) { |
| 7573 | /* stack: [ ... <headers:table>, <table> ] */ |
| 7574 | lua_pushnil(L); |
| 7575 | while (lua_next(L, -2) != 0) { |
| 7576 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 7577 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 7578 | /* invalid value type, skip it */ |
| 7579 | lua_pop(L, 1); |
| 7580 | continue; |
| 7581 | } |
| 7582 | |
| 7583 | |
| 7584 | /* Duplicate the key and swap it with the value. */ |
| 7585 | lua_pushvalue(L, -2); |
| 7586 | lua_insert(L, -2); |
| 7587 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 7588 | |
| 7589 | lua_newtable(L); |
| 7590 | lua_insert(L, -2); |
| 7591 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 7592 | |
| 7593 | if (lua_isstring(L, -1)) { |
| 7594 | /* push the value in the inner table */ |
| 7595 | lua_rawseti(L, -2, 1); |
| 7596 | } |
| 7597 | else { /* table */ |
| 7598 | lua_pushnil(L); |
| 7599 | while (lua_next(L, -2) != 0) { |
| 7600 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 7601 | if (!lua_isstring(L, -1)) { |
| 7602 | /* invalid value type, skip it*/ |
| 7603 | lua_pop(L, 1); |
| 7604 | continue; |
| 7605 | } |
| 7606 | /* push the value in the inner table */ |
| 7607 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 7608 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 7609 | } |
| 7610 | lua_pop(L, 1); |
| 7611 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 7612 | } |
| 7613 | |
| 7614 | /* push (k,v) on the stack in the headers table: |
| 7615 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 7616 | */ |
| 7617 | lua_settable(L, -5); |
| 7618 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 7619 | } |
| 7620 | } |
| 7621 | lua_pop(L, 1); |
| 7622 | } |
| 7623 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 7624 | lua_settable(L, -3); |
| 7625 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 7626 | |
| 7627 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 7628 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 7629 | lua_setmetatable(L, -2); |
| 7630 | return 1; |
| 7631 | } |
| 7632 | |
| 7633 | /* Set the reply status code, and optionally the reason. If no reason is |
| 7634 | * provided, the default one corresponding to the status code is used. |
| 7635 | */ |
| 7636 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 7637 | { |
| 7638 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7639 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 7640 | |
| 7641 | /* First argument (self) must be a table */ |
| 7642 | luaL_checktype(L, 1, LUA_TTABLE); |
| 7643 | |
| 7644 | if (status < 100 || status > 599) { |
| 7645 | lua_pushboolean(L, 0); |
| 7646 | return 1; |
| 7647 | } |
| 7648 | if (!reason) |
| 7649 | reason = http_get_reason(status); |
| 7650 | |
| 7651 | lua_pushinteger(L, status); |
| 7652 | lua_setfield(L, 1, "status"); |
| 7653 | |
| 7654 | lua_pushstring(L, reason); |
| 7655 | lua_setfield(L, 1, "reason"); |
| 7656 | |
| 7657 | lua_pushboolean(L, 1); |
| 7658 | return 1; |
| 7659 | } |
| 7660 | |
| 7661 | /* Add a header into the reply object. Each header name is associated to an |
| 7662 | * array of values in the "headers" table. If the header name is not found, a |
| 7663 | * new entry is created. |
| 7664 | */ |
| 7665 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 7666 | { |
| 7667 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7668 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 7669 | int ret; |
| 7670 | |
| 7671 | /* First argument (self) must be a table */ |
| 7672 | luaL_checktype(L, 1, LUA_TTABLE); |
| 7673 | |
| 7674 | /* Push in the stack the "headers" entry. */ |
| 7675 | ret = lua_getfield(L, 1, "headers"); |
| 7676 | if (ret != LUA_TTABLE) { |
| 7677 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 7678 | WILL_LJMP(lua_error(L)); |
| 7679 | } |
| 7680 | |
| 7681 | /* check if the header is already registered. If not, register it. */ |
| 7682 | ret = lua_getfield(L, -1, name); |
| 7683 | if (ret == LUA_TNIL) { |
| 7684 | /* Entry not found. */ |
| 7685 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 7686 | |
| 7687 | /* Insert the new header name in the array in the top of the stack. |
| 7688 | * It left the new array in the top of the stack. |
| 7689 | */ |
| 7690 | lua_newtable(L); |
| 7691 | lua_pushstring(L, name); |
| 7692 | lua_pushvalue(L, -2); |
| 7693 | lua_settable(L, -4); |
| 7694 | } |
| 7695 | else if (ret != LUA_TTABLE) { |
| 7696 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 7697 | WILL_LJMP(lua_error(L)); |
| 7698 | } |
| 7699 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 7700 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7701 | * the header value as new entry. |
| 7702 | */ |
| 7703 | lua_pushstring(L, value); |
| 7704 | ret = lua_rawlen(L, -2); |
| 7705 | lua_rawseti(L, -2, ret + 1); |
| 7706 | |
| 7707 | lua_pushboolean(L, 1); |
| 7708 | return 1; |
| 7709 | } |
| 7710 | |
| 7711 | /* Remove all occurrences of a given header name. */ |
| 7712 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 7713 | { |
| 7714 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7715 | int ret; |
| 7716 | |
| 7717 | /* First argument (self) must be a table */ |
| 7718 | luaL_checktype(L, 1, LUA_TTABLE); |
| 7719 | |
| 7720 | /* Push in the stack the "headers" entry. */ |
| 7721 | ret = lua_getfield(L, 1, "headers"); |
| 7722 | if (ret != LUA_TTABLE) { |
| 7723 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 7724 | WILL_LJMP(lua_error(L)); |
| 7725 | } |
| 7726 | |
| 7727 | lua_pushstring(L, name); |
| 7728 | lua_pushnil(L); |
| 7729 | lua_settable(L, -3); |
| 7730 | |
| 7731 | lua_pushboolean(L, 1); |
| 7732 | return 1; |
| 7733 | } |
| 7734 | |
| 7735 | /* Set the reply's body. Overwrite any existing entry. */ |
| 7736 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 7737 | { |
| 7738 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7739 | |
| 7740 | /* First argument (self) must be a table */ |
| 7741 | luaL_checktype(L, 1, LUA_TTABLE); |
| 7742 | |
| 7743 | lua_pushstring(L, payload); |
| 7744 | lua_setfield(L, 1, "body"); |
| 7745 | |
| 7746 | lua_pushboolean(L, 1); |
| 7747 | return 1; |
| 7748 | } |
| 7749 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7750 | __LJMP static int hlua_log(lua_State *L) |
| 7751 | { |
| 7752 | int level; |
| 7753 | const char *msg; |
| 7754 | |
| 7755 | MAY_LJMP(check_args(L, 2, "log")); |
| 7756 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 7757 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7758 | |
| 7759 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 7760 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 7761 | |
| 7762 | hlua_sendlog(NULL, level, msg); |
| 7763 | return 0; |
| 7764 | } |
| 7765 | |
| 7766 | __LJMP static int hlua_log_debug(lua_State *L) |
| 7767 | { |
| 7768 | const char *msg; |
| 7769 | |
| 7770 | MAY_LJMP(check_args(L, 1, "debug")); |
| 7771 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7772 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 7773 | return 0; |
| 7774 | } |
| 7775 | |
| 7776 | __LJMP static int hlua_log_info(lua_State *L) |
| 7777 | { |
| 7778 | const char *msg; |
| 7779 | |
| 7780 | MAY_LJMP(check_args(L, 1, "info")); |
| 7781 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7782 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 7783 | return 0; |
| 7784 | } |
| 7785 | |
| 7786 | __LJMP static int hlua_log_warning(lua_State *L) |
| 7787 | { |
| 7788 | const char *msg; |
| 7789 | |
| 7790 | MAY_LJMP(check_args(L, 1, "warning")); |
| 7791 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7792 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 7793 | return 0; |
| 7794 | } |
| 7795 | |
| 7796 | __LJMP static int hlua_log_alert(lua_State *L) |
| 7797 | { |
| 7798 | const char *msg; |
| 7799 | |
| 7800 | MAY_LJMP(check_args(L, 1, "alert")); |
| 7801 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7802 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7803 | return 0; |
| 7804 | } |
| 7805 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 7806 | __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] | 7807 | { |
| 7808 | int wakeup_ms = lua_tointeger(L, -1); |
| 7809 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 7810 | 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] | 7811 | return 0; |
| 7812 | } |
| 7813 | |
| 7814 | __LJMP static int hlua_sleep(lua_State *L) |
| 7815 | { |
| 7816 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 7817 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7818 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 7819 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7820 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 7821 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 7822 | wakeup_ms = tick_add(now_ms, delay); |
| 7823 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7824 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 7825 | 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] | 7826 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7827 | } |
| 7828 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 7829 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7830 | { |
| 7831 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 7832 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7833 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 7834 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7835 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 7836 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 7837 | wakeup_ms = tick_add(now_ms, delay); |
| 7838 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7839 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 7840 | 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] | 7841 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7842 | } |
| 7843 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 7844 | /* This functionis an LUA binding. it permits to give back |
| 7845 | * the hand at the HAProxy scheduler. It is used when the |
| 7846 | * LUA processing consumes a lot of time. |
| 7847 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 7848 | __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] | 7849 | { |
| 7850 | return 0; |
| 7851 | } |
| 7852 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 7853 | __LJMP static int hlua_yield(lua_State *L) |
| 7854 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 7855 | 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] | 7856 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 7857 | } |
| 7858 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 7859 | /* This function change the nice of the currently executed |
| 7860 | * task. It is used set low or high priority at the current |
| 7861 | * task. |
| 7862 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7863 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 7864 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7865 | struct hlua *hlua; |
| 7866 | int nice; |
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 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7869 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 7870 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7871 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 7872 | hlua = hlua_gethlua(L); |
| 7873 | |
| 7874 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 7875 | if (!hlua || !hlua->task) |
| 7876 | return 0; |
| 7877 | |
| 7878 | if (nice < -1024) |
| 7879 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7880 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 7881 | nice = 1024; |
| 7882 | |
| 7883 | hlua->task->nice = nice; |
| 7884 | return 0; |
| 7885 | } |
| 7886 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7887 | /* 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] | 7888 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 7889 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 7890 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7891 | * |
| 7892 | * Task wrapper are longjmp safe because the only one Lua code |
| 7893 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7894 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 7895 | 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] | 7896 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 7897 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7898 | enum hlua_exec status; |
| 7899 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 7900 | if (task->thread_mask == MAX_THREADS_MASK) |
| 7901 | task_set_affinity(task, tid_bit); |
| 7902 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7903 | /* If it is the first call to the task, we must initialize the |
| 7904 | * execution timeouts. |
| 7905 | */ |
| 7906 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7907 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7908 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7909 | /* Execute the Lua code. */ |
| 7910 | status = hlua_ctx_resume(hlua, 1); |
| 7911 | |
| 7912 | switch (status) { |
| 7913 | /* finished or yield */ |
| 7914 | case HLUA_E_OK: |
| 7915 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7916 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 7917 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7918 | break; |
| 7919 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 7920 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 7921 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 7922 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7923 | break; |
| 7924 | |
| 7925 | /* finished with error. */ |
| 7926 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7927 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7928 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7929 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 7930 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7931 | break; |
| 7932 | |
| 7933 | case HLUA_E_ERR: |
| 7934 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7935 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7936 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7937 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 7938 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7939 | break; |
| 7940 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 7941 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7942 | } |
| 7943 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7944 | /* This function is an LUA binding that register LUA function to be |
| 7945 | * executed after the HAProxy configuration parsing and before the |
| 7946 | * HAProxy scheduler starts. This function expect only one LUA |
| 7947 | * argument that is a function. This function returns nothing, but |
| 7948 | * throws if an error is encountered. |
| 7949 | */ |
| 7950 | __LJMP static int hlua_register_init(lua_State *L) |
| 7951 | { |
| 7952 | struct hlua_init_function *init; |
| 7953 | int ref; |
| 7954 | |
| 7955 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 7956 | |
| 7957 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 7958 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7959 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7960 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7961 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7962 | |
| 7963 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 7964 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7965 | return 0; |
| 7966 | } |
| 7967 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7968 | /* This functio is an LUA binding. It permits to register a task |
| 7969 | * executed in parallel of the main HAroxy activity. The task is |
| 7970 | * created and it is set in the HAProxy scheduler. It can be called |
| 7971 | * from the "init" section, "post init" or during the runtime. |
| 7972 | * |
| 7973 | * Lua prototype: |
| 7974 | * |
| 7975 | * <none> core.register_task(<function>) |
| 7976 | */ |
| 7977 | static int hlua_register_task(lua_State *L) |
| 7978 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 7979 | struct hlua *hlua = NULL; |
| 7980 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7981 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 7982 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7983 | |
| 7984 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 7985 | |
| 7986 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 7987 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 7988 | /* Get the reference state. If the reference is NULL, L is the master |
| 7989 | * state, otherwise hlua->T is. |
| 7990 | */ |
| 7991 | hlua = hlua_gethlua(L); |
| 7992 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 7993 | /* we are in runtime processing */ |
| 7994 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 7995 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 7996 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7997 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 7998 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7999 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8000 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8001 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8002 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8003 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8004 | /* We are in the common lua state, execute the task anywhere, |
| 8005 | * otherwise, inherit the current thread identifier |
| 8006 | */ |
| 8007 | if (state_id == 0) |
| 8008 | task = task_new(MAX_THREADS_MASK); |
| 8009 | else |
| 8010 | task = task_new(tid_bit); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8011 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8012 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8013 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8014 | task->context = hlua; |
| 8015 | task->process = hlua_process_task; |
| 8016 | |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8017 | if (!hlua_ctx_init(hlua, state_id, task, 1)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8018 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8019 | |
| 8020 | /* Restore the function in the stack. */ |
| 8021 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 8022 | hlua->nargs = 0; |
| 8023 | |
| 8024 | /* Schedule task. */ |
| 8025 | task_schedule(task, now_ms); |
| 8026 | |
| 8027 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8028 | |
| 8029 | alloc_error: |
| 8030 | task_destroy(task); |
| 8031 | hlua_ctx_destroy(hlua); |
| 8032 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8033 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8034 | } |
| 8035 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8036 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 8037 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 8038 | * resume converters. |
| 8039 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8040 | 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] | 8041 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8042 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8043 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8044 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8045 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8046 | if (!stream) |
| 8047 | return 0; |
| 8048 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8049 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8050 | * Lua context can be not initialized. This behavior |
| 8051 | * permits to save performances because a systematic |
| 8052 | * Lua initialization cause 5% performances loss. |
| 8053 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8054 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8055 | struct hlua *hlua; |
| 8056 | |
| 8057 | hlua = pool_alloc(pool_head_hlua); |
| 8058 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8059 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8060 | return 0; |
| 8061 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8062 | HLUA_INIT(hlua); |
| 8063 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8064 | 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] | 8065 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8066 | return 0; |
| 8067 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8068 | } |
| 8069 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8070 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8071 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8072 | |
| 8073 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8074 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8075 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8076 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8077 | else |
| 8078 | error = "critical error"; |
| 8079 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8080 | return 0; |
| 8081 | } |
| 8082 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8083 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8084 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8085 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8086 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8087 | return 0; |
| 8088 | } |
| 8089 | |
| 8090 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8091 | 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] | 8092 | |
| 8093 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8094 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8095 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8096 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8097 | return 0; |
| 8098 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8099 | hlua_smp2lua(stream->hlua->T, smp); |
| 8100 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8101 | |
| 8102 | /* push keywords in the stack. */ |
| 8103 | if (arg_p) { |
| 8104 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8105 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8106 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8107 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8108 | return 0; |
| 8109 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8110 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8111 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8112 | } |
| 8113 | } |
| 8114 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8115 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8116 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8117 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8118 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8119 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8120 | } |
| 8121 | |
| 8122 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8123 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8124 | /* finished. */ |
| 8125 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8126 | /* If the stack is empty, the function fails. */ |
| 8127 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8128 | return 0; |
| 8129 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8130 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8131 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8132 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8133 | return 1; |
| 8134 | |
| 8135 | /* yield. */ |
| 8136 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8137 | 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] | 8138 | return 0; |
| 8139 | |
| 8140 | /* finished with error. */ |
| 8141 | case HLUA_E_ERRMSG: |
| 8142 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8143 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8144 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8145 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8146 | return 0; |
| 8147 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8148 | case HLUA_E_ETMOUT: |
| 8149 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 8150 | return 0; |
| 8151 | |
| 8152 | case HLUA_E_NOMEM: |
| 8153 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 8154 | return 0; |
| 8155 | |
| 8156 | case HLUA_E_YIELD: |
| 8157 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 8158 | return 0; |
| 8159 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8160 | case HLUA_E_ERR: |
| 8161 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8162 | 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] | 8163 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8164 | |
| 8165 | default: |
| 8166 | return 0; |
| 8167 | } |
| 8168 | } |
| 8169 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8170 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 8171 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8172 | * resume sample-fetches. This function will be called by the sample |
| 8173 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8174 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8175 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 8176 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8177 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8178 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8179 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8180 | const char *error; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 8181 | unsigned int hflags = HLUA_TXN_NOTERM | HLUA_TXN_SMP_CTX; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8182 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8183 | if (!stream) |
| 8184 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8185 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8186 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8187 | * Lua context can be not initialized. This behavior |
| 8188 | * permits to save performances because a systematic |
| 8189 | * Lua initialization cause 5% performances loss. |
| 8190 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8191 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8192 | struct hlua *hlua; |
| 8193 | |
| 8194 | hlua = pool_alloc(pool_head_hlua); |
| 8195 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8196 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8197 | return 0; |
| 8198 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8199 | hlua->T = NULL; |
| 8200 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8201 | 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] | 8202 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8203 | return 0; |
| 8204 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8205 | } |
| 8206 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8207 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8208 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8209 | |
| 8210 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8211 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8212 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8213 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8214 | else |
| 8215 | error = "critical error"; |
| 8216 | 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] | 8217 | return 0; |
| 8218 | } |
| 8219 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8220 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8221 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8222 | 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] | 8223 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8224 | return 0; |
| 8225 | } |
| 8226 | |
| 8227 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8228 | 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] | 8229 | |
| 8230 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 8231 | 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] | 8232 | 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] | 8233 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8234 | return 0; |
| 8235 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8236 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8237 | |
| 8238 | /* push keywords in the stack. */ |
| 8239 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 8240 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8241 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8242 | 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] | 8243 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8244 | return 0; |
| 8245 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8246 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8247 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8248 | } |
| 8249 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8250 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8251 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8252 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8253 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8254 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8255 | } |
| 8256 | |
| 8257 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8258 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8259 | /* finished. */ |
| 8260 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8261 | /* If the stack is empty, the function fails. */ |
| 8262 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8263 | return 0; |
| 8264 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8265 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8266 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8267 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8268 | |
| 8269 | /* Set the end of execution flag. */ |
| 8270 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 8271 | return 1; |
| 8272 | |
| 8273 | /* yield. */ |
| 8274 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8275 | 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] | 8276 | return 0; |
| 8277 | |
| 8278 | /* finished with error. */ |
| 8279 | case HLUA_E_ERRMSG: |
| 8280 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8281 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8282 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8283 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8284 | return 0; |
| 8285 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8286 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8287 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 8288 | return 0; |
| 8289 | |
| 8290 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8291 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 8292 | return 0; |
| 8293 | |
| 8294 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8295 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 8296 | return 0; |
| 8297 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8298 | case HLUA_E_ERR: |
| 8299 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8300 | 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] | 8301 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8302 | |
| 8303 | default: |
| 8304 | return 0; |
| 8305 | } |
| 8306 | } |
| 8307 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8308 | /* This function is an LUA binding used for registering |
| 8309 | * "sample-conv" functions. It expects a converter name used |
| 8310 | * in the haproxy configuration file, and an LUA function. |
| 8311 | */ |
| 8312 | __LJMP static int hlua_register_converters(lua_State *L) |
| 8313 | { |
| 8314 | struct sample_conv_kw_list *sck; |
| 8315 | const char *name; |
| 8316 | int ref; |
| 8317 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8318 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8319 | struct sample_conv *sc; |
| 8320 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8321 | |
| 8322 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 8323 | |
| 8324 | /* First argument : converter name. */ |
| 8325 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8326 | |
| 8327 | /* Second argument : lua function. */ |
| 8328 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 8329 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8330 | /* Check if the converter is already registered */ |
| 8331 | trash = get_trash_chunk(); |
| 8332 | chunk_printf(trash, "lua.%s", name); |
| 8333 | sc = find_sample_conv(trash->area, trash->data); |
| 8334 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8335 | fcn = sc->private; |
| 8336 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8337 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 8338 | "This will become a hard error in version 2.5.\n", name); |
| 8339 | } |
| 8340 | fcn->function_ref[hlua_state_id] = ref; |
| 8341 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8342 | } |
| 8343 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8344 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8345 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8346 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8347 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8348 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8349 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8350 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8351 | |
| 8352 | /* Fill fcn. */ |
| 8353 | fcn->name = strdup(name); |
| 8354 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8355 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8356 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8357 | |
| 8358 | /* List head */ |
| 8359 | sck->list.n = sck->list.p = NULL; |
| 8360 | |
| 8361 | /* converter keyword. */ |
| 8362 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8363 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8364 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8365 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8366 | |
| 8367 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 8368 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 8369 | 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] | 8370 | sck->kw[0].val_args = NULL; |
| 8371 | sck->kw[0].in_type = SMP_T_STR; |
| 8372 | sck->kw[0].out_type = SMP_T_STR; |
| 8373 | sck->kw[0].private = fcn; |
| 8374 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8375 | /* Register this new converter */ |
| 8376 | sample_register_convs(sck); |
| 8377 | |
| 8378 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8379 | |
| 8380 | alloc_error: |
| 8381 | release_hlua_function(fcn); |
| 8382 | ha_free(&sck); |
| 8383 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8384 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8385 | } |
| 8386 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8387 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8388 | * "sample-fetch" functions. It expects a converter name used |
| 8389 | * in the haproxy configuration file, and an LUA function. |
| 8390 | */ |
| 8391 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 8392 | { |
| 8393 | const char *name; |
| 8394 | int ref; |
| 8395 | int len; |
| 8396 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8397 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8398 | struct sample_fetch *sf; |
| 8399 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8400 | |
| 8401 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 8402 | |
| 8403 | /* First argument : sample-fetch name. */ |
| 8404 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8405 | |
| 8406 | /* Second argument : lua function. */ |
| 8407 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 8408 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8409 | /* Check if the sample-fetch is already registered */ |
| 8410 | trash = get_trash_chunk(); |
| 8411 | chunk_printf(trash, "lua.%s", name); |
| 8412 | sf = find_sample_fetch(trash->area, trash->data); |
| 8413 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8414 | fcn = sf->private; |
| 8415 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8416 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 8417 | "This will become a hard error in version 2.5.\n", name); |
| 8418 | } |
| 8419 | fcn->function_ref[hlua_state_id] = ref; |
| 8420 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8421 | } |
| 8422 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8423 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8424 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8425 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8426 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8427 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8428 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8429 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8430 | |
| 8431 | /* Fill fcn. */ |
| 8432 | fcn->name = strdup(name); |
| 8433 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8434 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8435 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8436 | |
| 8437 | /* List head */ |
| 8438 | sfk->list.n = sfk->list.p = NULL; |
| 8439 | |
| 8440 | /* sample-fetch keyword. */ |
| 8441 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8442 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8443 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8444 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8445 | |
| 8446 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 8447 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 8448 | 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] | 8449 | sfk->kw[0].val_args = NULL; |
| 8450 | sfk->kw[0].out_type = SMP_T_STR; |
| 8451 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 8452 | sfk->kw[0].val = 0; |
| 8453 | sfk->kw[0].private = fcn; |
| 8454 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8455 | /* Register this new fetch. */ |
| 8456 | sample_register_fetches(sfk); |
| 8457 | |
| 8458 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8459 | |
| 8460 | alloc_error: |
| 8461 | release_hlua_function(fcn); |
| 8462 | ha_free(&sfk); |
| 8463 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8464 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8465 | } |
| 8466 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 8467 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8468 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 8469 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8470 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8471 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8472 | unsigned int delay; |
| 8473 | unsigned int wakeup_ms; |
| 8474 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8475 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 8476 | hlua = hlua_gethlua(L); |
| 8477 | if (!hlua) { |
| 8478 | return 0; |
| 8479 | } |
| 8480 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8481 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 8482 | |
| 8483 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 8484 | wakeup_ms = tick_add(now_ms, delay); |
| 8485 | hlua->wake_time = wakeup_ms; |
| 8486 | return 0; |
| 8487 | } |
| 8488 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8489 | /* This function is a wrapper to execute each LUA function declared as an action |
| 8490 | * wrapper during the initialisation period. This function may return any |
| 8491 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 8492 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 8493 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8494 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 8495 | 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] | 8496 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8497 | { |
| 8498 | char **arg; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 8499 | unsigned int hflags = HLUA_TXN_ACT_CTX; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8500 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8501 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 8502 | |
| 8503 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8504 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 8505 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 8506 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 8507 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 8508 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8509 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8510 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 8511 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8512 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8513 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8514 | * Lua context can be not initialized. This behavior |
| 8515 | * permits to save performances because a systematic |
| 8516 | * Lua initialization cause 5% performances loss. |
| 8517 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8518 | if (!s->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8519 | struct hlua *hlua; |
| 8520 | |
| 8521 | hlua = pool_alloc(pool_head_hlua); |
| 8522 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8523 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8524 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8525 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8526 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8527 | HLUA_INIT(hlua); |
| 8528 | s->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8529 | 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] | 8530 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8531 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8532 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8533 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8534 | } |
| 8535 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8536 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8537 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8538 | |
| 8539 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8540 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8541 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 8542 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8543 | else |
| 8544 | error = "critical error"; |
| 8545 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8546 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8547 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8548 | } |
| 8549 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8550 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8551 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8552 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8553 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8554 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8555 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8556 | } |
| 8557 | |
| 8558 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8559 | 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] | 8560 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8561 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 8562 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8563 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8564 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8565 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8566 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8567 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8568 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8569 | |
| 8570 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 8571 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8572 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8573 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8574 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8575 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8576 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8577 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8578 | lua_pushstring(s->hlua->T, *arg); |
| 8579 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8580 | } |
| 8581 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8582 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8583 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8584 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8585 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8586 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8587 | } |
| 8588 | |
| 8589 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 8590 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8591 | /* finished. */ |
| 8592 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8593 | /* Catch the return value */ |
| 8594 | if (lua_gettop(s->hlua->T) > 0) |
| 8595 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8596 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8597 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 8598 | if (act_ret == ACT_RET_YIELD) { |
| 8599 | if (flags & ACT_OPT_FINAL) |
| 8600 | goto err_yield; |
| 8601 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 8602 | if (dir == SMP_OPT_DIR_REQ) |
| 8603 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 8604 | s->hlua->wake_time); |
| 8605 | else |
| 8606 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 8607 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8608 | } |
| 8609 | goto end; |
| 8610 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8611 | /* yield. */ |
| 8612 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 8613 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 8614 | if (dir == SMP_OPT_DIR_REQ) |
| 8615 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 8616 | s->hlua->wake_time); |
| 8617 | else |
| 8618 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 8619 | s->hlua->wake_time); |
| 8620 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8621 | /* Some actions can be wake up when a "write" event |
| 8622 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8623 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8624 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 8625 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8626 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8627 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8628 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8629 | act_ret = ACT_RET_YIELD; |
| 8630 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8631 | |
| 8632 | /* finished with error. */ |
| 8633 | case HLUA_E_ERRMSG: |
| 8634 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8635 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8636 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8637 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8638 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8639 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8640 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8641 | 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] | 8642 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8643 | |
| 8644 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8645 | 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] | 8646 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8647 | |
| 8648 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 8649 | err_yield: |
| 8650 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8651 | 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] | 8652 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8653 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8654 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8655 | case HLUA_E_ERR: |
| 8656 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8657 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8658 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8659 | |
| 8660 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8661 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8662 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8663 | |
| 8664 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 8665 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 8666 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8667 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8668 | } |
| 8669 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 8670 | 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] | 8671 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 8672 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8673 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8674 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 8675 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 8676 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8677 | } |
| 8678 | |
| 8679 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 8680 | { |
| 8681 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8682 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8683 | struct task *task; |
| 8684 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8685 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8686 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8687 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8688 | if (!hlua) { |
| 8689 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8690 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8691 | return 0; |
| 8692 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8693 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8694 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8695 | ctx->ctx.hlua_apptcp.flags = 0; |
| 8696 | |
| 8697 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 8698 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8699 | if (!task) { |
| 8700 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8701 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8702 | return 0; |
| 8703 | } |
| 8704 | task->nice = 0; |
| 8705 | task->context = ctx; |
| 8706 | task->process = hlua_applet_wakeup; |
| 8707 | ctx->ctx.hlua_apptcp.task = task; |
| 8708 | |
| 8709 | /* In the execution wrappers linked with a stream, the |
| 8710 | * Lua context can be not initialized. This behavior |
| 8711 | * permits to save performances because a systematic |
| 8712 | * Lua initialization cause 5% performances loss. |
| 8713 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8714 | 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] | 8715 | 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] | 8716 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8717 | return 0; |
| 8718 | } |
| 8719 | |
| 8720 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 8721 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8722 | |
| 8723 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8724 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8725 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 8726 | error = lua_tostring(hlua->T, -1); |
| 8727 | else |
| 8728 | error = "critical error"; |
| 8729 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8730 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8731 | return 0; |
| 8732 | } |
| 8733 | |
| 8734 | /* Check stack available size. */ |
| 8735 | if (!lua_checkstack(hlua->T, 1)) { |
| 8736 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8737 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8738 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8739 | return 0; |
| 8740 | } |
| 8741 | |
| 8742 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8743 | 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] | 8744 | |
| 8745 | /* Create and and push object stream in the stack. */ |
| 8746 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 8747 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8748 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8749 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8750 | return 0; |
| 8751 | } |
| 8752 | hlua->nargs = 1; |
| 8753 | |
| 8754 | /* push keywords in the stack. */ |
| 8755 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 8756 | if (!lua_checkstack(hlua->T, 1)) { |
| 8757 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8758 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8759 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8760 | return 0; |
| 8761 | } |
| 8762 | lua_pushstring(hlua->T, *arg); |
| 8763 | hlua->nargs++; |
| 8764 | } |
| 8765 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8766 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8767 | |
| 8768 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 8769 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 8770 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8771 | |
| 8772 | return 1; |
| 8773 | } |
| 8774 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 8775 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8776 | { |
| 8777 | struct stream_interface *si = ctx->owner; |
| 8778 | struct stream *strm = si_strm(si); |
| 8779 | struct channel *res = si_ic(si); |
| 8780 | struct act_rule *rule = ctx->rule; |
| 8781 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8782 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8783 | |
| 8784 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 8785 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 8786 | /* eat the whole request */ |
| 8787 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8788 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 8789 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8790 | |
| 8791 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 8792 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 8793 | return; |
| 8794 | |
| 8795 | /* Execute the function. */ |
| 8796 | switch (hlua_ctx_resume(hlua, 1)) { |
| 8797 | /* finished. */ |
| 8798 | case HLUA_E_OK: |
| 8799 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 8800 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8801 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 8802 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8803 | res->flags |= CF_READ_NULL; |
| 8804 | si_shutr(si); |
| 8805 | return; |
| 8806 | |
| 8807 | /* yield. */ |
| 8808 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 8809 | if (hlua->wake_time != TICK_ETERNITY) |
| 8810 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8811 | return; |
| 8812 | |
| 8813 | /* finished with error. */ |
| 8814 | case HLUA_E_ERRMSG: |
| 8815 | /* Display log. */ |
| 8816 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8817 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8818 | lua_pop(hlua->T, 1); |
| 8819 | goto error; |
| 8820 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8821 | case HLUA_E_ETMOUT: |
| 8822 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8823 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8824 | goto error; |
| 8825 | |
| 8826 | case HLUA_E_NOMEM: |
| 8827 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8828 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8829 | goto error; |
| 8830 | |
| 8831 | case HLUA_E_YIELD: /* unexpected */ |
| 8832 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8833 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8834 | goto error; |
| 8835 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8836 | case HLUA_E_ERR: |
| 8837 | /* Display log. */ |
| 8838 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8839 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8840 | goto error; |
| 8841 | |
| 8842 | default: |
| 8843 | goto error; |
| 8844 | } |
| 8845 | |
| 8846 | error: |
| 8847 | |
| 8848 | /* For all other cases, just close the stream. */ |
| 8849 | si_shutw(si); |
| 8850 | si_shutr(si); |
| 8851 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 8852 | } |
| 8853 | |
| 8854 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 8855 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8856 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8857 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8858 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8859 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8860 | } |
| 8861 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8862 | /* The function returns 1 if the initialisation is complete, 0 if |
| 8863 | * an errors occurs and -1 if more data are required for initializing |
| 8864 | * the applet. |
| 8865 | */ |
| 8866 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 8867 | { |
| 8868 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8869 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8870 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8871 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8872 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8873 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8874 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8875 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8876 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8877 | if (!hlua) { |
| 8878 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8879 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8880 | return 0; |
| 8881 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8882 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8883 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8884 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 8885 | ctx->ctx.hlua_apphttp.flags = 0; |
| 8886 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 8887 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 8888 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 8889 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8890 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 8891 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8892 | if (!task) { |
| 8893 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8894 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8895 | return 0; |
| 8896 | } |
| 8897 | task->nice = 0; |
| 8898 | task->context = ctx; |
| 8899 | task->process = hlua_applet_wakeup; |
| 8900 | ctx->ctx.hlua_apphttp.task = task; |
| 8901 | |
| 8902 | /* In the execution wrappers linked with a stream, the |
| 8903 | * Lua context can be not initialized. This behavior |
| 8904 | * permits to save performances because a systematic |
| 8905 | * Lua initialization cause 5% performances loss. |
| 8906 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8907 | 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] | 8908 | 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] | 8909 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8910 | return 0; |
| 8911 | } |
| 8912 | |
| 8913 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 8914 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8915 | |
| 8916 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8917 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8918 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 8919 | error = lua_tostring(hlua->T, -1); |
| 8920 | else |
| 8921 | error = "critical error"; |
| 8922 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8923 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8924 | return 0; |
| 8925 | } |
| 8926 | |
| 8927 | /* Check stack available size. */ |
| 8928 | if (!lua_checkstack(hlua->T, 1)) { |
| 8929 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8930 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8931 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8932 | return 0; |
| 8933 | } |
| 8934 | |
| 8935 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8936 | 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] | 8937 | |
| 8938 | /* Create and and push object stream in the stack. */ |
| 8939 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 8940 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8941 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8942 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8943 | return 0; |
| 8944 | } |
| 8945 | hlua->nargs = 1; |
| 8946 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8947 | /* push keywords in the stack. */ |
| 8948 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 8949 | if (!lua_checkstack(hlua->T, 1)) { |
| 8950 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8951 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8952 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8953 | return 0; |
| 8954 | } |
| 8955 | lua_pushstring(hlua->T, *arg); |
| 8956 | hlua->nargs++; |
| 8957 | } |
| 8958 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8959 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8960 | |
| 8961 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 8962 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8963 | |
| 8964 | return 1; |
| 8965 | } |
| 8966 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 8967 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 8968 | { |
| 8969 | struct stream_interface *si = ctx->owner; |
| 8970 | struct stream *strm = si_strm(si); |
| 8971 | struct channel *req = si_oc(si); |
| 8972 | struct channel *res = si_ic(si); |
| 8973 | struct act_rule *rule = ctx->rule; |
| 8974 | struct proxy *px = strm->be; |
| 8975 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 8976 | struct htx *req_htx, *res_htx; |
| 8977 | |
| 8978 | res_htx = htx_from_buf(&res->buf); |
| 8979 | |
| 8980 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 8981 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 8982 | goto out; |
| 8983 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 8984 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 8985 | if (!b_size(&res->buf)) { |
| 8986 | si_rx_room_blk(si); |
| 8987 | goto out; |
| 8988 | } |
| 8989 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 8990 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 8991 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 8992 | |
| 8993 | /* Set the currently running flag. */ |
| 8994 | if (!HLUA_IS_RUNNING(hlua) && |
| 8995 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 8996 | if (!co_data(req)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 8997 | si_cant_get(si); |
| 8998 | goto out; |
| 8999 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9000 | } |
| 9001 | |
| 9002 | /* Executes The applet if it is not done. */ |
| 9003 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 9004 | |
| 9005 | /* Execute the function. */ |
| 9006 | switch (hlua_ctx_resume(hlua, 1)) { |
| 9007 | /* finished. */ |
| 9008 | case HLUA_E_OK: |
| 9009 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 9010 | break; |
| 9011 | |
| 9012 | /* yield. */ |
| 9013 | case HLUA_E_AGAIN: |
| 9014 | if (hlua->wake_time != TICK_ETERNITY) |
| 9015 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9016 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9017 | |
| 9018 | /* finished with error. */ |
| 9019 | case HLUA_E_ERRMSG: |
| 9020 | /* Display log. */ |
| 9021 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9022 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9023 | lua_pop(hlua->T, 1); |
| 9024 | goto error; |
| 9025 | |
| 9026 | case HLUA_E_ETMOUT: |
| 9027 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9028 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9029 | goto error; |
| 9030 | |
| 9031 | case HLUA_E_NOMEM: |
| 9032 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9033 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9034 | goto error; |
| 9035 | |
| 9036 | case HLUA_E_YIELD: /* unexpected */ |
| 9037 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9038 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9039 | goto error; |
| 9040 | |
| 9041 | case HLUA_E_ERR: |
| 9042 | /* Display log. */ |
| 9043 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9044 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9045 | goto error; |
| 9046 | |
| 9047 | default: |
| 9048 | goto error; |
| 9049 | } |
| 9050 | } |
| 9051 | |
| 9052 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9053 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 9054 | goto done; |
| 9055 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9056 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 9057 | goto error; |
| 9058 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 9059 | /* no more data are expected. Don't add TLR because mux-h1 will take care of it */ |
| 9060 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9061 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 9062 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9063 | } |
| 9064 | |
| 9065 | done: |
| 9066 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9067 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9068 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9069 | si_shutr(si); |
| 9070 | } |
| 9071 | |
| 9072 | /* eat the whole request */ |
| 9073 | if (co_data(req)) { |
| 9074 | req_htx = htx_from_buf(&req->buf); |
| 9075 | co_htx_skip(req, req_htx, co_data(req)); |
| 9076 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9077 | } |
| 9078 | } |
| 9079 | |
| 9080 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9081 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9082 | return; |
| 9083 | |
| 9084 | error: |
| 9085 | |
| 9086 | /* If we are in HTTP mode, and we are not send any |
| 9087 | * data, return a 500 server error in best effort: |
| 9088 | * if there is no room available in the buffer, |
| 9089 | * just close the connection. |
| 9090 | */ |
| 9091 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 9092 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9093 | |
| 9094 | channel_erase(res); |
| 9095 | res->buf.data = b_data(err); |
| 9096 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 9097 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 9098 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9099 | } |
| 9100 | if (!(strm->flags & SF_ERR_MASK)) |
| 9101 | strm->flags |= SF_ERR_RESOURCE; |
| 9102 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 9103 | goto done; |
| 9104 | } |
| 9105 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9106 | static void hlua_applet_http_release(struct appctx *ctx) |
| 9107 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 9108 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9109 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9110 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9111 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9112 | } |
| 9113 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9114 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9115 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9116 | * |
| 9117 | * This function can fail with an abort() due to an Lua critical error. |
| 9118 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 9119 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9120 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9121 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 9122 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9123 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9124 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9125 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9126 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9127 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9128 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9129 | if (!rule->arg.hlua_rule) { |
| 9130 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9131 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9132 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9133 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9134 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 9135 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 9136 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9137 | if (!rule->arg.hlua_rule->args) { |
| 9138 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9139 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9140 | } |
| 9141 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9142 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9143 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9144 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9145 | /* Expect some arguments */ |
| 9146 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9147 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9148 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9149 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9150 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9151 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9152 | if (!rule->arg.hlua_rule->args[i]) { |
| 9153 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9154 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9155 | } |
| 9156 | (*cur_arg)++; |
| 9157 | } |
| 9158 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9159 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 9160 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9161 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9162 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9163 | |
| 9164 | error: |
| 9165 | if (rule->arg.hlua_rule) { |
| 9166 | if (rule->arg.hlua_rule->args) { |
| 9167 | for (i = 0; i < fcn->nargs; i++) |
| 9168 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 9169 | ha_free(&rule->arg.hlua_rule->args); |
| 9170 | } |
| 9171 | ha_free(&rule->arg.hlua_rule); |
| 9172 | } |
| 9173 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9174 | } |
| 9175 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9176 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 9177 | struct act_rule *rule, char **err) |
| 9178 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9179 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9180 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9181 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9182 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9183 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9184 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9185 | * the call of this analyzer. |
| 9186 | */ |
| 9187 | if (rule->from != ACT_F_HTTP_REQ) { |
| 9188 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 9189 | return ACT_RET_PRS_ERR; |
| 9190 | } |
| 9191 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9192 | /* Memory for the rule. */ |
| 9193 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 9194 | if (!rule->arg.hlua_rule) { |
| 9195 | memprintf(err, "out of memory error"); |
| 9196 | return ACT_RET_PRS_ERR; |
| 9197 | } |
| 9198 | |
| 9199 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9200 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9201 | |
| 9202 | /* TODO: later accept arguments. */ |
| 9203 | rule->arg.hlua_rule->args = NULL; |
| 9204 | |
| 9205 | /* Add applet pointer in the rule. */ |
| 9206 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 9207 | rule->applet.name = fcn->name; |
| 9208 | rule->applet.init = hlua_applet_http_init; |
| 9209 | rule->applet.fct = hlua_applet_http_fct; |
| 9210 | rule->applet.release = hlua_applet_http_release; |
| 9211 | rule->applet.timeout = hlua_timeout_applet; |
| 9212 | |
| 9213 | return ACT_RET_PRS_OK; |
| 9214 | } |
| 9215 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9216 | /* This function is an LUA binding used for registering |
| 9217 | * "sample-conv" functions. It expects a converter name used |
| 9218 | * in the haproxy configuration file, and an LUA function. |
| 9219 | */ |
| 9220 | __LJMP static int hlua_register_action(lua_State *L) |
| 9221 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9222 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9223 | const char *name; |
| 9224 | int ref; |
| 9225 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9226 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9227 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9228 | struct buffer *trash; |
| 9229 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9230 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9231 | /* Initialise the number of expected arguments at 0. */ |
| 9232 | nargs = 0; |
| 9233 | |
| 9234 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 9235 | 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] | 9236 | |
| 9237 | /* First argument : converter name. */ |
| 9238 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9239 | |
| 9240 | /* Second argument : environment. */ |
| 9241 | if (lua_type(L, 2) != LUA_TTABLE) |
| 9242 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9243 | |
| 9244 | /* Third argument : lua function. */ |
| 9245 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 9246 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9247 | /* 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] | 9248 | if (lua_gettop(L) >= 4) |
| 9249 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 9250 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9251 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9252 | lua_pushnil(L); |
| 9253 | while (lua_next(L, 2) != 0) { |
| 9254 | if (lua_type(L, -1) != LUA_TSTRING) |
| 9255 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9256 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9257 | /* Check if action exists */ |
| 9258 | trash = get_trash_chunk(); |
| 9259 | chunk_printf(trash, "lua.%s", name); |
| 9260 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 9261 | akw = tcp_req_cont_action(trash->area); |
| 9262 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 9263 | akw = tcp_res_cont_action(trash->area); |
| 9264 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 9265 | akw = action_http_req_custom(trash->area); |
| 9266 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 9267 | akw = action_http_res_custom(trash->area); |
| 9268 | } else { |
| 9269 | akw = NULL; |
| 9270 | } |
| 9271 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9272 | fcn = akw->private; |
| 9273 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9274 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 9275 | "This will become a hard error in version 2.5.\n", name); |
| 9276 | } |
| 9277 | fcn->function_ref[hlua_state_id] = ref; |
| 9278 | |
| 9279 | /* pop the environment string. */ |
| 9280 | lua_pop(L, 1); |
| 9281 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9282 | } |
| 9283 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9284 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 9285 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9286 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9287 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9288 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9289 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9290 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9291 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9292 | |
| 9293 | /* Fill fcn. */ |
| 9294 | fcn->name = strdup(name); |
| 9295 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9296 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9297 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9298 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 9299 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9300 | fcn->nargs = nargs; |
| 9301 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9302 | /* List head */ |
| 9303 | akl->list.n = akl->list.p = NULL; |
| 9304 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9305 | /* action keyword. */ |
| 9306 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9307 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9308 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9309 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9310 | |
| 9311 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 9312 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 9313 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9314 | akl->kw[0].private = fcn; |
| 9315 | akl->kw[0].parse = action_register_lua; |
| 9316 | |
| 9317 | /* select the action registering point. */ |
| 9318 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 9319 | tcp_req_cont_keywords_register(akl); |
| 9320 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 9321 | tcp_res_cont_keywords_register(akl); |
| 9322 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 9323 | http_req_keywords_register(akl); |
| 9324 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 9325 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9326 | else { |
| 9327 | release_hlua_function(fcn); |
| 9328 | if (akl) |
| 9329 | ha_free((char **)&(akl->kw[0].kw)); |
| 9330 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 9331 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9332 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 9333 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9334 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9335 | |
| 9336 | /* pop the environment string. */ |
| 9337 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9338 | |
| 9339 | /* reset for next loop */ |
| 9340 | akl = NULL; |
| 9341 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9342 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9343 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9344 | |
| 9345 | alloc_error: |
| 9346 | release_hlua_function(fcn); |
| 9347 | ha_free(&akl); |
| 9348 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9349 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9350 | } |
| 9351 | |
| 9352 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 9353 | struct act_rule *rule, char **err) |
| 9354 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9355 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9356 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 9357 | if (px->mode == PR_MODE_HTTP) { |
| 9358 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 9359 | return ACT_RET_PRS_ERR; |
| 9360 | } |
| 9361 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9362 | /* Memory for the rule. */ |
| 9363 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 9364 | if (!rule->arg.hlua_rule) { |
| 9365 | memprintf(err, "out of memory error"); |
| 9366 | return ACT_RET_PRS_ERR; |
| 9367 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9368 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9369 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9370 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9371 | |
| 9372 | /* TODO: later accept arguments. */ |
| 9373 | rule->arg.hlua_rule->args = NULL; |
| 9374 | |
| 9375 | /* Add applet pointer in the rule. */ |
| 9376 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 9377 | rule->applet.name = fcn->name; |
| 9378 | rule->applet.init = hlua_applet_tcp_init; |
| 9379 | rule->applet.fct = hlua_applet_tcp_fct; |
| 9380 | rule->applet.release = hlua_applet_tcp_release; |
| 9381 | rule->applet.timeout = hlua_timeout_applet; |
| 9382 | |
| 9383 | return 0; |
| 9384 | } |
| 9385 | |
| 9386 | /* This function is an LUA binding used for registering |
| 9387 | * "sample-conv" functions. It expects a converter name used |
| 9388 | * in the haproxy configuration file, and an LUA function. |
| 9389 | */ |
| 9390 | __LJMP static int hlua_register_service(lua_State *L) |
| 9391 | { |
| 9392 | struct action_kw_list *akl; |
| 9393 | const char *name; |
| 9394 | const char *env; |
| 9395 | int ref; |
| 9396 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9397 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9398 | struct buffer *trash; |
| 9399 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9400 | |
| 9401 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 9402 | |
| 9403 | /* First argument : converter name. */ |
| 9404 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9405 | |
| 9406 | /* Second argument : environment. */ |
| 9407 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 9408 | |
| 9409 | /* Third argument : lua function. */ |
| 9410 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 9411 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9412 | /* Check for service already registered */ |
| 9413 | trash = get_trash_chunk(); |
| 9414 | chunk_printf(trash, "lua.%s", name); |
| 9415 | akw = service_find(trash->area); |
| 9416 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9417 | fcn = akw->private; |
| 9418 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9419 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 9420 | "This will become a hard error in version 2.5.\n", name); |
| 9421 | } |
| 9422 | fcn->function_ref[hlua_state_id] = ref; |
| 9423 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9424 | } |
| 9425 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9426 | /* Allocate and fill the sample fetch keyword struct. */ |
| 9427 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 9428 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9429 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9430 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9431 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9432 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9433 | |
| 9434 | /* Fill fcn. */ |
| 9435 | len = strlen("<lua.>") + strlen(name) + 1; |
| 9436 | fcn->name = calloc(1, len); |
| 9437 | if (!fcn->name) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9438 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9439 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9440 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9441 | |
| 9442 | /* List head */ |
| 9443 | akl->list.n = akl->list.p = NULL; |
| 9444 | |
| 9445 | /* converter keyword. */ |
| 9446 | len = strlen("lua.") + strlen(name) + 1; |
| 9447 | akl->kw[0].kw = calloc(1, len); |
| 9448 | if (!akl->kw[0].kw) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9449 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9450 | |
| 9451 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 9452 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 9453 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9454 | if (strcmp(env, "tcp") == 0) |
| 9455 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9456 | else if (strcmp(env, "http") == 0) |
| 9457 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9458 | else { |
| 9459 | release_hlua_function(fcn); |
| 9460 | if (akl) |
| 9461 | ha_free((char **)&(akl->kw[0].kw)); |
| 9462 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 9463 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 9464 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9465 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9466 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 9467 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9468 | akl->kw[0].private = fcn; |
| 9469 | |
| 9470 | /* End of array. */ |
| 9471 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 9472 | |
| 9473 | /* Register this new converter */ |
| 9474 | service_keywords_register(akl); |
| 9475 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9476 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9477 | |
| 9478 | alloc_error: |
| 9479 | release_hlua_function(fcn); |
| 9480 | ha_free(&akl); |
| 9481 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9482 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9483 | } |
| 9484 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9485 | /* This function initialises Lua cli handler. It copies the |
| 9486 | * arguments in the Lua stack and create channel IO objects. |
| 9487 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 9488 | 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] | 9489 | { |
| 9490 | struct hlua *hlua; |
| 9491 | struct hlua_function *fcn; |
| 9492 | int i; |
| 9493 | const char *error; |
| 9494 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9495 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9496 | appctx->ctx.hlua_cli.fcn = private; |
| 9497 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9498 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9499 | if (!hlua) { |
| 9500 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 9501 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9502 | } |
| 9503 | HLUA_INIT(hlua); |
| 9504 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9505 | |
| 9506 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9507 | * 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] | 9508 | * applet_http. It is absolutely compatible. |
| 9509 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 9510 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9511 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 9512 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 9513 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9514 | } |
| 9515 | appctx->ctx.hlua_cli.task->nice = 0; |
| 9516 | appctx->ctx.hlua_cli.task->context = appctx; |
| 9517 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 9518 | |
| 9519 | /* Initialises the Lua context */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9520 | 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] | 9521 | 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] | 9522 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9523 | } |
| 9524 | |
| 9525 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9526 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9527 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9528 | error = lua_tostring(hlua->T, -1); |
| 9529 | else |
| 9530 | error = "critical error"; |
| 9531 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 9532 | goto error; |
| 9533 | } |
| 9534 | |
| 9535 | /* Check stack available size. */ |
| 9536 | if (!lua_checkstack(hlua->T, 2)) { |
| 9537 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 9538 | goto error; |
| 9539 | } |
| 9540 | |
| 9541 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9542 | 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] | 9543 | |
| 9544 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 9545 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9546 | */ |
| 9547 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 9548 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 9549 | goto error; |
| 9550 | } |
| 9551 | hlua->nargs = 1; |
| 9552 | |
| 9553 | /* push keywords in the stack. */ |
| 9554 | for (i = 0; *args[i]; i++) { |
| 9555 | /* Check stack available size. */ |
| 9556 | if (!lua_checkstack(hlua->T, 1)) { |
| 9557 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 9558 | goto error; |
| 9559 | } |
| 9560 | lua_pushstring(hlua->T, args[i]); |
| 9561 | hlua->nargs++; |
| 9562 | } |
| 9563 | |
| 9564 | /* We must initialize the execution timeouts. */ |
| 9565 | hlua->max_time = hlua_timeout_session; |
| 9566 | |
| 9567 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9568 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9569 | |
| 9570 | /* It's ok */ |
| 9571 | return 0; |
| 9572 | |
| 9573 | /* It's not ok. */ |
| 9574 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9575 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9576 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 9577 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9578 | return 1; |
| 9579 | } |
| 9580 | |
| 9581 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 9582 | { |
| 9583 | struct hlua *hlua; |
| 9584 | struct stream_interface *si; |
| 9585 | struct hlua_function *fcn; |
| 9586 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9587 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9588 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 9589 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9590 | |
| 9591 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 9592 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 9593 | return 1; |
| 9594 | |
| 9595 | /* Execute the function. */ |
| 9596 | switch (hlua_ctx_resume(hlua, 1)) { |
| 9597 | |
| 9598 | /* finished. */ |
| 9599 | case HLUA_E_OK: |
| 9600 | return 1; |
| 9601 | |
| 9602 | /* yield. */ |
| 9603 | case HLUA_E_AGAIN: |
| 9604 | /* We want write. */ |
| 9605 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 9606 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9607 | /* Set the timeout. */ |
| 9608 | if (hlua->wake_time != TICK_ETERNITY) |
| 9609 | task_schedule(hlua->task, hlua->wake_time); |
| 9610 | return 0; |
| 9611 | |
| 9612 | /* finished with error. */ |
| 9613 | case HLUA_E_ERRMSG: |
| 9614 | /* Display log. */ |
| 9615 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 9616 | fcn->name, lua_tostring(hlua->T, -1)); |
| 9617 | lua_pop(hlua->T, 1); |
| 9618 | return 1; |
| 9619 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9620 | case HLUA_E_ETMOUT: |
| 9621 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 9622 | fcn->name); |
| 9623 | return 1; |
| 9624 | |
| 9625 | case HLUA_E_NOMEM: |
| 9626 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 9627 | fcn->name); |
| 9628 | return 1; |
| 9629 | |
| 9630 | case HLUA_E_YIELD: /* unexpected */ |
| 9631 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 9632 | fcn->name); |
| 9633 | return 1; |
| 9634 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9635 | case HLUA_E_ERR: |
| 9636 | /* Display log. */ |
| 9637 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 9638 | fcn->name); |
| 9639 | return 1; |
| 9640 | |
| 9641 | default: |
| 9642 | return 1; |
| 9643 | } |
| 9644 | |
| 9645 | return 1; |
| 9646 | } |
| 9647 | |
| 9648 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 9649 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9650 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9651 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9652 | } |
| 9653 | |
| 9654 | /* This function is an LUA binding used for registering |
| 9655 | * new keywords in the cli. It expects a list of keywords |
| 9656 | * which are the "path". It is limited to 5 keywords. A |
| 9657 | * description of the command, a function to be executed |
| 9658 | * for the parsing and a function for io handlers. |
| 9659 | */ |
| 9660 | __LJMP static int hlua_register_cli(lua_State *L) |
| 9661 | { |
| 9662 | struct cli_kw_list *cli_kws; |
| 9663 | const char *message; |
| 9664 | int ref_io; |
| 9665 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 9666 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9667 | int index; |
| 9668 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9669 | struct buffer *trash; |
| 9670 | const char *kw[5]; |
| 9671 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 9672 | const char *errmsg; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9673 | |
| 9674 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 9675 | |
| 9676 | /* First argument : an array of maximum 5 keywords. */ |
| 9677 | if (!lua_istable(L, 1)) |
| 9678 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 9679 | |
| 9680 | /* Second argument : string with contextual message. */ |
| 9681 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 9682 | |
| 9683 | /* Third and fourth argument : lua function. */ |
| 9684 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 9685 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9686 | /* Check for CLI service already registered */ |
| 9687 | trash = get_trash_chunk(); |
| 9688 | index = 0; |
| 9689 | lua_pushnil(L); |
| 9690 | memset(kw, 0, sizeof(kw)); |
| 9691 | while (lua_next(L, 1) != 0) { |
| 9692 | if (index >= CLI_PREFIX_KW_NB) |
| 9693 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 9694 | if (lua_type(L, -1) != LUA_TSTRING) |
| 9695 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 9696 | kw[index] = lua_tostring(L, -1); |
| 9697 | if (index == 0) |
| 9698 | chunk_printf(trash, "%s", kw[index]); |
| 9699 | else |
| 9700 | chunk_appendf(trash, " %s", kw[index]); |
| 9701 | index++; |
| 9702 | lua_pop(L, 1); |
| 9703 | } |
| 9704 | cli_kw = cli_find_kw_exact((char **)kw); |
| 9705 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9706 | fcn = cli_kw->private; |
| 9707 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9708 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 9709 | "This will become a hard error in version 2.5.\n", trash->area); |
| 9710 | } |
| 9711 | fcn->function_ref[hlua_state_id] = ref_io; |
| 9712 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9713 | } |
| 9714 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9715 | /* Allocate and fill the sample fetch keyword struct. */ |
| 9716 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 9717 | if (!cli_kws) { |
| 9718 | errmsg = "Lua out of memory error."; |
| 9719 | goto error; |
| 9720 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9721 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 9722 | if (!fcn) { |
| 9723 | errmsg = "Lua out of memory error."; |
| 9724 | goto error; |
| 9725 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9726 | |
| 9727 | /* Fill path. */ |
| 9728 | index = 0; |
| 9729 | lua_pushnil(L); |
| 9730 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 9731 | if (index >= 5) { |
| 9732 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 9733 | goto error; |
| 9734 | } |
| 9735 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 9736 | errmsg = "1st argument must be a table filled with strings"; |
| 9737 | goto error; |
| 9738 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9739 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 9740 | if (!cli_kws->kw[0].str_kw[index]) { |
| 9741 | errmsg = "Lua out of memory error."; |
| 9742 | goto error; |
| 9743 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9744 | index++; |
| 9745 | lua_pop(L, 1); |
| 9746 | } |
| 9747 | |
| 9748 | /* Copy help message. */ |
| 9749 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 9750 | if (!cli_kws->kw[0].usage) { |
| 9751 | errmsg = "Lua out of memory error."; |
| 9752 | goto error; |
| 9753 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9754 | |
| 9755 | /* Fill fcn io handler. */ |
| 9756 | len = strlen("<lua.cli>") + 1; |
| 9757 | for (i = 0; i < index; i++) |
| 9758 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 9759 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 9760 | if (!fcn->name) { |
| 9761 | errmsg = "Lua out of memory error."; |
| 9762 | goto error; |
| 9763 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9764 | strncat((char *)fcn->name, "<lua.cli", len); |
| 9765 | for (i = 0; i < index; i++) { |
| 9766 | strncat((char *)fcn->name, ".", len); |
| 9767 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 9768 | } |
| 9769 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9770 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9771 | |
| 9772 | /* Fill last entries. */ |
| 9773 | cli_kws->kw[0].private = fcn; |
| 9774 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 9775 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 9776 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 9777 | |
| 9778 | /* Register this new converter */ |
| 9779 | cli_register_kw(cli_kws); |
| 9780 | |
| 9781 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 9782 | |
| 9783 | error: |
| 9784 | release_hlua_function(fcn); |
| 9785 | if (cli_kws) { |
| 9786 | for (i = 0; i < index; i++) |
| 9787 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 9788 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 9789 | } |
| 9790 | ha_free(&cli_kws); |
| 9791 | WILL_LJMP(luaL_error(L, errmsg)); |
| 9792 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9793 | } |
| 9794 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 9795 | static int hlua_filter_init_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 9796 | { |
| 9797 | struct hlua_flt_config *conf = fconf->conf; |
| 9798 | lua_State *L; |
| 9799 | int error, pos, state_id, flt_ref; |
| 9800 | |
| 9801 | state_id = reg_flt_to_stack_id(conf->reg); |
| 9802 | L = hlua_states[state_id]; |
| 9803 | pos = lua_gettop(L); |
| 9804 | |
| 9805 | /* The filter parsing function */ |
| 9806 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->fun_ref[state_id]); |
| 9807 | |
| 9808 | /* Push the filter class on the stack and resolve all callbacks */ |
| 9809 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->flt_ref[state_id]); |
| 9810 | |
| 9811 | /* Duplicate the filter class so each filter will have its own copy */ |
| 9812 | lua_newtable(L); |
| 9813 | lua_pushnil(L); |
| 9814 | |
| 9815 | while (lua_next(L, pos+2)) { |
| 9816 | lua_pushvalue(L, -2); |
| 9817 | lua_insert(L, -2); |
| 9818 | lua_settable(L, -4); |
| 9819 | } |
| 9820 | flt_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
| 9821 | |
| 9822 | /* Remove the original lua filter class from the stack */ |
| 9823 | lua_pop(L, 1); |
| 9824 | |
| 9825 | /* Push the copy on the stack */ |
| 9826 | lua_rawgeti(L, LUA_REGISTRYINDEX, flt_ref); |
| 9827 | |
| 9828 | /* extra args are pushed in a table */ |
| 9829 | lua_newtable(L); |
| 9830 | for (pos = 0; conf->args[pos]; pos++) { |
| 9831 | /* Check stack available size. */ |
| 9832 | if (!lua_checkstack(L, 1)) { |
| 9833 | ha_alert("Lua filter '%s' : Lua error : full stack.", conf->reg->name); |
| 9834 | goto error; |
| 9835 | } |
| 9836 | lua_pushstring(L, conf->args[pos]); |
| 9837 | lua_rawseti(L, -2, lua_rawlen(L, -2) + 1); |
| 9838 | } |
| 9839 | |
| 9840 | error = lua_pcall(L, 2, LUA_MULTRET, 0); |
| 9841 | switch (error) { |
| 9842 | case LUA_OK: |
| 9843 | /* replace the filter ref */ |
| 9844 | conf->ref[state_id] = flt_ref; |
| 9845 | break; |
| 9846 | case LUA_ERRRUN: |
| 9847 | ha_alert("Lua filter '%s' : runtime error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 9848 | goto error; |
| 9849 | case LUA_ERRMEM: |
| 9850 | ha_alert("Lua filter '%s' : out of memory error", conf->reg->name); |
| 9851 | goto error; |
| 9852 | case LUA_ERRERR: |
| 9853 | ha_alert("Lua filter '%s' : message handler error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 9854 | goto error; |
| 9855 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
| 9856 | case LUA_ERRGCMM: |
| 9857 | ha_alert("Lua filter '%s' : garbage collector error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 9858 | goto error; |
| 9859 | #endif |
| 9860 | default: |
| 9861 | ha_alert("Lua filter '%s' : unknonwn error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 9862 | goto error; |
| 9863 | } |
| 9864 | |
| 9865 | lua_settop(L, 0); |
| 9866 | return 0; |
| 9867 | |
| 9868 | error: |
| 9869 | lua_settop(L, 0); |
| 9870 | return -1; |
| 9871 | } |
| 9872 | |
| 9873 | static void hlua_filter_deinit_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 9874 | { |
| 9875 | struct hlua_flt_config *conf = fconf->conf; |
| 9876 | lua_State *L; |
| 9877 | int state_id; |
| 9878 | |
| 9879 | if (!conf) |
| 9880 | return; |
| 9881 | |
| 9882 | state_id = reg_flt_to_stack_id(conf->reg); |
| 9883 | L = hlua_states[state_id]; |
| 9884 | luaL_unref(L, LUA_REGISTRYINDEX, conf->ref[state_id]); |
| 9885 | } |
| 9886 | |
| 9887 | static int hlua_filter_init(struct proxy *px, struct flt_conf *fconf) |
| 9888 | { |
| 9889 | struct hlua_flt_config *conf = fconf->conf; |
| 9890 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 9891 | |
| 9892 | /* Rely on per-thread init for global scripts */ |
| 9893 | if (!state_id) |
| 9894 | return hlua_filter_init_per_thread(px, fconf); |
| 9895 | return 0; |
| 9896 | } |
| 9897 | |
| 9898 | static void hlua_filter_deinit(struct proxy *px, struct flt_conf *fconf) |
| 9899 | { |
| 9900 | |
| 9901 | if (fconf->conf) { |
| 9902 | struct hlua_flt_config *conf = fconf->conf; |
| 9903 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 9904 | int pos; |
| 9905 | |
| 9906 | /* Rely on per-thread deinit for global scripts */ |
| 9907 | if (!state_id) |
| 9908 | hlua_filter_deinit_per_thread(px, fconf); |
| 9909 | |
| 9910 | for (pos = 0; conf->args[pos]; pos++) |
| 9911 | free(conf->args[pos]); |
| 9912 | free(conf->args); |
| 9913 | } |
| 9914 | ha_free(&fconf->conf); |
| 9915 | ha_free((char **)&fconf->id); |
| 9916 | ha_free(&fconf->ops); |
| 9917 | } |
| 9918 | |
| 9919 | static int hlua_filter_new(struct stream *s, struct filter *filter) |
| 9920 | { |
| 9921 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 9922 | struct hlua_flt_ctx *flt_ctx = NULL; |
| 9923 | int ret = 1; |
| 9924 | |
| 9925 | /* In the execution wrappers linked with a stream, the |
| 9926 | * Lua context can be not initialized. This behavior |
| 9927 | * permits to save performances because a systematic |
| 9928 | * Lua initialization cause 5% performances loss. |
| 9929 | */ |
| 9930 | if (!s->hlua) { |
| 9931 | struct hlua *hlua; |
| 9932 | |
| 9933 | hlua = pool_alloc(pool_head_hlua); |
| 9934 | if (!hlua) { |
| 9935 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 9936 | conf->reg->name); |
| 9937 | ret = 0; |
| 9938 | goto end; |
| 9939 | } |
| 9940 | HLUA_INIT(hlua); |
| 9941 | s->hlua = hlua; |
| 9942 | if (!hlua_ctx_init(s->hlua, reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 9943 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 9944 | conf->reg->name); |
| 9945 | ret = 0; |
| 9946 | goto end; |
| 9947 | } |
| 9948 | } |
| 9949 | |
| 9950 | flt_ctx = pool_zalloc(pool_head_hlua_flt_ctx); |
| 9951 | if (!flt_ctx) { |
| 9952 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 9953 | conf->reg->name); |
| 9954 | ret = 0; |
| 9955 | goto end; |
| 9956 | } |
| 9957 | flt_ctx->hlua[0] = pool_alloc(pool_head_hlua); |
| 9958 | flt_ctx->hlua[1] = pool_alloc(pool_head_hlua); |
| 9959 | if (!flt_ctx->hlua[0] || !flt_ctx->hlua[1]) { |
| 9960 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 9961 | conf->reg->name); |
| 9962 | ret = 0; |
| 9963 | goto end; |
| 9964 | } |
| 9965 | HLUA_INIT(flt_ctx->hlua[0]); |
| 9966 | HLUA_INIT(flt_ctx->hlua[1]); |
| 9967 | if (!hlua_ctx_init(flt_ctx->hlua[0], reg_flt_to_stack_id(conf->reg), s->task, 0) || |
| 9968 | !hlua_ctx_init(flt_ctx->hlua[1], reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 9969 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 9970 | conf->reg->name); |
| 9971 | ret = 0; |
| 9972 | goto end; |
| 9973 | } |
| 9974 | |
| 9975 | if (!HLUA_IS_RUNNING(s->hlua)) { |
| 9976 | /* The following Lua calls can fail. */ |
| 9977 | if (!SET_SAFE_LJMP(s->hlua)) { |
| 9978 | const char *error; |
| 9979 | |
| 9980 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 9981 | error = lua_tostring(s->hlua->T, -1); |
| 9982 | else |
| 9983 | error = "critical error"; |
| 9984 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 9985 | ret = 0; |
| 9986 | goto end; |
| 9987 | } |
| 9988 | |
| 9989 | /* Check stack size. */ |
| 9990 | if (!lua_checkstack(s->hlua->T, 1)) { |
| 9991 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 9992 | ret = 0; |
| 9993 | goto end; |
| 9994 | } |
| 9995 | |
| 9996 | lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, conf->ref[s->hlua->state_id]); |
| 9997 | if (lua_getfield(s->hlua->T, -1, "new") != LUA_TFUNCTION) { |
| 9998 | SEND_ERR(s->be, "Lua filter '%s': 'new' field is not a function.\n", |
| 9999 | conf->reg->name); |
| 10000 | RESET_SAFE_LJMP(s->hlua); |
| 10001 | ret = 0; |
| 10002 | goto end; |
| 10003 | } |
| 10004 | lua_insert(s->hlua->T, -2); |
| 10005 | |
| 10006 | /* Push the copy on the stack */ |
| 10007 | s->hlua->nargs = 1; |
| 10008 | |
| 10009 | /* We must initialize the execution timeouts. */ |
| 10010 | s->hlua->max_time = hlua_timeout_session; |
| 10011 | |
| 10012 | /* At this point the execution is safe. */ |
| 10013 | RESET_SAFE_LJMP(s->hlua); |
| 10014 | } |
| 10015 | |
| 10016 | switch (hlua_ctx_resume(s->hlua, 0)) { |
| 10017 | case HLUA_E_OK: |
| 10018 | /* Nothing returned or not a table, ignore the filter for current stream */ |
| 10019 | if (!lua_gettop(s->hlua->T) || !lua_istable(s->hlua->T, 1)) { |
| 10020 | ret = 0; |
| 10021 | goto end; |
| 10022 | } |
| 10023 | |
| 10024 | /* Attached the filter pointer to the ctx */ |
| 10025 | lua_pushstring(s->hlua->T, "__filter"); |
| 10026 | lua_pushlightuserdata(s->hlua->T, filter); |
| 10027 | lua_settable(s->hlua->T, -3); |
| 10028 | |
| 10029 | /* Save a ref on the filter ctx */ |
| 10030 | lua_pushvalue(s->hlua->T, 1); |
| 10031 | flt_ctx->ref = luaL_ref(s->hlua->T, LUA_REGISTRYINDEX); |
| 10032 | filter->ctx = flt_ctx; |
| 10033 | break; |
| 10034 | case HLUA_E_ERRMSG: |
| 10035 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(s->hlua->T, -1)); |
| 10036 | ret = -1; |
| 10037 | goto end; |
| 10038 | case HLUA_E_ETMOUT: |
| 10039 | SEND_ERR(s->be, "Lua filter '%s' : 'new' execution timeout.\n", conf->reg->name); |
| 10040 | ret = 0; |
| 10041 | goto end; |
| 10042 | case HLUA_E_NOMEM: |
| 10043 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10044 | ret = 0; |
| 10045 | goto end; |
| 10046 | case HLUA_E_AGAIN: |
| 10047 | case HLUA_E_YIELD: |
| 10048 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10049 | " are not allowed from 'new' function.\n", conf->reg->name); |
| 10050 | ret = 0; |
| 10051 | goto end; |
| 10052 | case HLUA_E_ERR: |
| 10053 | SEND_ERR(s->be, "Lua filter '%s': 'new' returns an unknown error.\n", conf->reg->name); |
| 10054 | ret = 0; |
| 10055 | goto end; |
| 10056 | default: |
| 10057 | ret = 0; |
| 10058 | goto end; |
| 10059 | } |
| 10060 | |
| 10061 | end: |
| 10062 | if (s->hlua) |
| 10063 | lua_settop(s->hlua->T, 0); |
| 10064 | if (ret <= 0) { |
| 10065 | if (flt_ctx) { |
| 10066 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10067 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10068 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10069 | } |
| 10070 | } |
| 10071 | return ret; |
| 10072 | } |
| 10073 | |
| 10074 | static void hlua_filter_delete(struct stream *s, struct filter *filter) |
| 10075 | { |
| 10076 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10077 | |
| 10078 | luaL_unref(s->hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 10079 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10080 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10081 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10082 | filter->ctx = NULL; |
| 10083 | } |
| 10084 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 10085 | static int hlua_filter_from_payload(struct filter *filter) |
| 10086 | { |
| 10087 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10088 | |
| 10089 | return (flt_ctx && !!(flt_ctx->flags & HLUA_FLT_CTX_FL_PAYLOAD)); |
| 10090 | } |
| 10091 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10092 | static int hlua_filter_callback(struct stream *s, struct filter *filter, const char *fun, |
| 10093 | int dir, unsigned int flags) |
| 10094 | { |
| 10095 | struct hlua *flt_hlua; |
| 10096 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 10097 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10098 | unsigned int hflags = HLUA_TXN_FLT_CTX; |
| 10099 | int ret = 1; |
| 10100 | |
| 10101 | flt_hlua = flt_ctx->hlua[(dir == SMP_OPT_DIR_REQ ? 0 : 1)]; |
| 10102 | if (!flt_hlua) |
| 10103 | goto end; |
| 10104 | |
| 10105 | if (!HLUA_IS_RUNNING(flt_hlua)) { |
| 10106 | int extra_idx = lua_gettop(flt_hlua->T); |
| 10107 | |
| 10108 | /* The following Lua calls can fail. */ |
| 10109 | if (!SET_SAFE_LJMP(flt_hlua)) { |
| 10110 | const char *error; |
| 10111 | |
| 10112 | if (lua_type(flt_hlua->T, -1) == LUA_TSTRING) |
| 10113 | error = lua_tostring(flt_hlua->T, -1); |
| 10114 | else |
| 10115 | error = "critical error"; |
| 10116 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 10117 | goto end; |
| 10118 | } |
| 10119 | |
| 10120 | /* Check stack size. */ |
| 10121 | if (!lua_checkstack(flt_hlua->T, 3)) { |
| 10122 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10123 | RESET_SAFE_LJMP(flt_hlua); |
| 10124 | goto end; |
| 10125 | } |
| 10126 | |
| 10127 | lua_rawgeti(flt_hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 10128 | if (lua_getfield(flt_hlua->T, -1, fun) != LUA_TFUNCTION) { |
| 10129 | RESET_SAFE_LJMP(flt_hlua); |
| 10130 | goto end; |
| 10131 | } |
| 10132 | lua_insert(flt_hlua->T, -2); |
| 10133 | |
| 10134 | if (!hlua_txn_new(flt_hlua->T, s, s->be, dir, hflags)) { |
| 10135 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10136 | RESET_SAFE_LJMP(flt_hlua); |
| 10137 | goto end; |
| 10138 | } |
| 10139 | flt_hlua->nargs = 2; |
| 10140 | |
| 10141 | if (flags & HLUA_FLT_CB_ARG_CHN) { |
| 10142 | if (dir == SMP_OPT_DIR_REQ) |
| 10143 | lua_getfield(flt_hlua->T, -1, "req"); |
| 10144 | else |
| 10145 | lua_getfield(flt_hlua->T, -1, "res"); |
| 10146 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10147 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10148 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10149 | lua_settable(flt_hlua->T, -3); |
| 10150 | } |
| 10151 | flt_hlua->nargs++; |
| 10152 | } |
| 10153 | else if (flags & HLUA_FLT_CB_ARG_HTTP_MSG) { |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10154 | if (dir == SMP_OPT_DIR_REQ) |
| 10155 | lua_getfield(flt_hlua->T, -1, "http_req"); |
| 10156 | else |
| 10157 | lua_getfield(flt_hlua->T, -1, "http_res"); |
| 10158 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10159 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10160 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10161 | lua_settable(flt_hlua->T, -3); |
| 10162 | } |
| 10163 | flt_hlua->nargs++; |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10164 | } |
| 10165 | |
| 10166 | /* Check stack size. */ |
| 10167 | if (!lua_checkstack(flt_hlua->T, 1)) { |
| 10168 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10169 | RESET_SAFE_LJMP(flt_hlua); |
| 10170 | goto end; |
| 10171 | } |
| 10172 | |
| 10173 | while (extra_idx--) { |
| 10174 | lua_pushvalue(flt_hlua->T, 1); |
| 10175 | lua_remove(flt_hlua->T, 1); |
| 10176 | flt_hlua->nargs++; |
| 10177 | } |
| 10178 | |
| 10179 | /* We must initialize the execution timeouts. */ |
| 10180 | flt_hlua->max_time = hlua_timeout_session; |
| 10181 | |
| 10182 | /* At this point the execution is safe. */ |
| 10183 | RESET_SAFE_LJMP(flt_hlua); |
| 10184 | } |
| 10185 | |
| 10186 | switch (hlua_ctx_resume(flt_hlua, !(flags & HLUA_FLT_CB_FINAL))) { |
| 10187 | case HLUA_E_OK: |
| 10188 | /* Catch the return value if it required */ |
| 10189 | if ((flags & HLUA_FLT_CB_RETVAL) && lua_gettop(flt_hlua->T) > 0) { |
| 10190 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10191 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10192 | } |
| 10193 | |
| 10194 | /* Set timeout in the required channel. */ |
| 10195 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10196 | if (dir == SMP_OPT_DIR_REQ) |
| 10197 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10198 | else |
| 10199 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10200 | } |
| 10201 | break; |
| 10202 | case HLUA_E_AGAIN: |
| 10203 | /* Set timeout in the required channel. */ |
| 10204 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10205 | if (dir == SMP_OPT_DIR_REQ) |
| 10206 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10207 | else |
| 10208 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10209 | } |
| 10210 | /* Some actions can be wake up when a "write" event |
| 10211 | * is detected on a response channel. This is useful |
| 10212 | * only for actions targeted on the requests. |
| 10213 | */ |
| 10214 | if (HLUA_IS_WAKERESWR(flt_hlua)) |
| 10215 | s->res.flags |= CF_WAKE_WRITE; |
| 10216 | if (HLUA_IS_WAKEREQWR(flt_hlua)) |
| 10217 | s->req.flags |= CF_WAKE_WRITE; |
| 10218 | ret = 0; |
| 10219 | goto end; |
| 10220 | case HLUA_E_ERRMSG: |
| 10221 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(flt_hlua->T, -1)); |
| 10222 | ret = -1; |
| 10223 | goto end; |
| 10224 | case HLUA_E_ETMOUT: |
| 10225 | SEND_ERR(s->be, "Lua filter '%s' : '%s' callback execution timeout.\n", conf->reg->name, fun); |
| 10226 | goto end; |
| 10227 | case HLUA_E_NOMEM: |
| 10228 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10229 | goto end; |
| 10230 | case HLUA_E_YIELD: |
| 10231 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10232 | " are not allowed from '%s' callback.\n", conf->reg->name, fun); |
| 10233 | goto end; |
| 10234 | case HLUA_E_ERR: |
| 10235 | SEND_ERR(s->be, "Lua filter '%s': '%s' returns an unknown error.\n", conf->reg->name, fun); |
| 10236 | goto end; |
| 10237 | default: |
| 10238 | goto end; |
| 10239 | } |
| 10240 | |
| 10241 | |
| 10242 | end: |
| 10243 | return ret; |
| 10244 | } |
| 10245 | |
| 10246 | static int hlua_filter_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 10247 | { |
| 10248 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10249 | |
| 10250 | flt_ctx->flags = 0; |
| 10251 | return hlua_filter_callback(s, filter, "start_analyze", |
| 10252 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10253 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 10254 | } |
| 10255 | |
| 10256 | static int hlua_filter_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 10257 | { |
| 10258 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10259 | |
| 10260 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10261 | return hlua_filter_callback(s, filter, "end_analyze", |
| 10262 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10263 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 10264 | } |
| 10265 | |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10266 | static int hlua_filter_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 10267 | { |
| 10268 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10269 | |
| 10270 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10271 | return hlua_filter_callback(s, filter, "http_headers", |
| 10272 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10273 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10274 | } |
| 10275 | |
| 10276 | static int hlua_filter_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg, |
| 10277 | unsigned int offset, unsigned int len) |
| 10278 | { |
| 10279 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10280 | struct hlua *flt_hlua; |
| 10281 | int dir = (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 10282 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 10283 | int ret; |
| 10284 | |
| 10285 | flt_hlua = flt_ctx->hlua[idx]; |
| 10286 | flt_ctx->cur_off[idx] = offset; |
| 10287 | flt_ctx->cur_len[idx] = len; |
| 10288 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 10289 | ret = hlua_filter_callback(s, filter, "http_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10290 | if (ret != -1) { |
| 10291 | ret = flt_ctx->cur_len[idx]; |
| 10292 | if (lua_gettop(flt_hlua->T) > 0) { |
| 10293 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10294 | if (ret > flt_ctx->cur_len[idx]) |
| 10295 | ret = flt_ctx->cur_len[idx]; |
| 10296 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10297 | } |
| 10298 | } |
| 10299 | return ret; |
| 10300 | } |
| 10301 | |
| 10302 | static int hlua_filter_http_end(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 10303 | { |
| 10304 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10305 | |
| 10306 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10307 | return hlua_filter_callback(s, filter, "http_end", |
| 10308 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10309 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10310 | } |
| 10311 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10312 | static int hlua_filter_tcp_payload(struct stream *s, struct filter *filter, struct channel *chn, |
| 10313 | unsigned int offset, unsigned int len) |
| 10314 | { |
| 10315 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10316 | struct hlua *flt_hlua; |
| 10317 | int dir = (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 10318 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 10319 | int ret; |
| 10320 | |
| 10321 | flt_hlua = flt_ctx->hlua[idx]; |
| 10322 | flt_ctx->cur_off[idx] = offset; |
| 10323 | flt_ctx->cur_len[idx] = len; |
| 10324 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 10325 | ret = hlua_filter_callback(s, filter, "tcp_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_CHN)); |
| 10326 | if (ret != -1) { |
| 10327 | ret = flt_ctx->cur_len[idx]; |
| 10328 | if (lua_gettop(flt_hlua->T) > 0) { |
| 10329 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10330 | if (ret > flt_ctx->cur_len[idx]) |
| 10331 | ret = flt_ctx->cur_len[idx]; |
| 10332 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10333 | } |
| 10334 | } |
| 10335 | return ret; |
| 10336 | } |
| 10337 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10338 | static int hlua_filter_parse_fct(char **args, int *cur_arg, struct proxy *px, |
| 10339 | struct flt_conf *fconf, char **err, void *private) |
| 10340 | { |
| 10341 | struct hlua_reg_filter *reg_flt = private; |
| 10342 | lua_State *L; |
| 10343 | struct hlua_flt_config *conf = NULL; |
| 10344 | const char *flt_id = NULL; |
| 10345 | int state_id, pos, flt_flags = 0; |
| 10346 | struct flt_ops *hlua_flt_ops = NULL; |
| 10347 | |
| 10348 | state_id = reg_flt_to_stack_id(reg_flt); |
| 10349 | L = hlua_states[state_id]; |
| 10350 | |
| 10351 | /* Initialize the filter ops with default callbacks */ |
| 10352 | hlua_flt_ops = calloc(1, sizeof(*hlua_flt_ops)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame^] | 10353 | if (!hlua_flt_ops) |
| 10354 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10355 | hlua_flt_ops->init = hlua_filter_init; |
| 10356 | hlua_flt_ops->deinit = hlua_filter_deinit; |
| 10357 | if (state_id) { |
| 10358 | /* Set per-thread callback if script is loaded per-thread */ |
| 10359 | hlua_flt_ops->init_per_thread = hlua_filter_init_per_thread; |
| 10360 | hlua_flt_ops->deinit_per_thread = hlua_filter_deinit_per_thread; |
| 10361 | } |
| 10362 | hlua_flt_ops->attach = hlua_filter_new; |
| 10363 | hlua_flt_ops->detach = hlua_filter_delete; |
| 10364 | |
| 10365 | /* Push the filter class on the stack and resolve all callbacks */ |
| 10366 | lua_rawgeti(L, LUA_REGISTRYINDEX, reg_flt->flt_ref[state_id]); |
| 10367 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10368 | if (lua_getfield(L, -1, "start_analyze") == LUA_TFUNCTION) |
| 10369 | hlua_flt_ops->channel_start_analyze = hlua_filter_start_analyze; |
| 10370 | lua_pop(L, 1); |
| 10371 | if (lua_getfield(L, -1, "end_analyze") == LUA_TFUNCTION) |
| 10372 | hlua_flt_ops->channel_end_analyze = hlua_filter_end_analyze; |
| 10373 | lua_pop(L, 1); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10374 | if (lua_getfield(L, -1, "http_headers") == LUA_TFUNCTION) |
| 10375 | hlua_flt_ops->http_headers = hlua_filter_http_headers; |
| 10376 | lua_pop(L, 1); |
| 10377 | if (lua_getfield(L, -1, "http_payload") == LUA_TFUNCTION) |
| 10378 | hlua_flt_ops->http_payload = hlua_filter_http_payload; |
| 10379 | lua_pop(L, 1); |
| 10380 | if (lua_getfield(L, -1, "http_end") == LUA_TFUNCTION) |
| 10381 | hlua_flt_ops->http_end = hlua_filter_http_end; |
| 10382 | lua_pop(L, 1); |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10383 | if (lua_getfield(L, -1, "tcp_payload") == LUA_TFUNCTION) |
| 10384 | hlua_flt_ops->tcp_payload = hlua_filter_tcp_payload; |
| 10385 | lua_pop(L, 1); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10386 | |
| 10387 | /* Get id and flags of the filter class */ |
| 10388 | if (lua_getfield(L, -1, "id") == LUA_TSTRING) |
| 10389 | flt_id = lua_tostring(L, -1); |
| 10390 | lua_pop(L, 1); |
| 10391 | if (lua_getfield(L, -1, "flags") == LUA_TNUMBER) |
| 10392 | flt_flags = lua_tointeger(L, -1); |
| 10393 | lua_pop(L, 1); |
| 10394 | |
| 10395 | /* Create the filter config */ |
| 10396 | conf = calloc(1, sizeof(*conf)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame^] | 10397 | if (!conf) |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10398 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10399 | conf->reg = reg_flt; |
| 10400 | |
| 10401 | /* duplicate args */ |
| 10402 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++); |
| 10403 | conf->args = calloc(pos + 1, sizeof(*conf->args)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame^] | 10404 | if (!conf->args) |
| 10405 | goto error; |
| 10406 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++) { |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10407 | conf->args[pos] = strdup(args[*cur_arg + 1 + pos]); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame^] | 10408 | if (!conf->args[pos]) |
| 10409 | goto error; |
| 10410 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10411 | conf->args[pos] = NULL; |
| 10412 | *cur_arg += pos + 1; |
| 10413 | |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame^] | 10414 | if (flt_id) { |
| 10415 | fconf->id = strdup(flt_id); |
| 10416 | if (!fconf->id) |
| 10417 | goto error; |
| 10418 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10419 | fconf->flags = flt_flags; |
| 10420 | fconf->conf = conf; |
| 10421 | fconf->ops = hlua_flt_ops; |
| 10422 | |
| 10423 | lua_settop(L, 0); |
| 10424 | return 0; |
| 10425 | |
| 10426 | error: |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame^] | 10427 | 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] | 10428 | free(hlua_flt_ops); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame^] | 10429 | if (conf && conf->args) { |
| 10430 | for (pos = 0; conf->args[pos]; pos++) |
| 10431 | free(conf->args[pos]); |
| 10432 | free(conf->args); |
| 10433 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10434 | free(conf); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame^] | 10435 | free((char *)fconf->id); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10436 | lua_settop(L, 0); |
| 10437 | return -1; |
| 10438 | } |
| 10439 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10440 | __LJMP static int hlua_register_data_filter(lua_State *L) |
| 10441 | { |
| 10442 | struct filter *filter; |
| 10443 | struct channel *chn; |
| 10444 | |
| 10445 | MAY_LJMP(check_args(L, 2, "register_data_filter")); |
| 10446 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 10447 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 10448 | |
| 10449 | lua_getfield(L, 1, "__filter"); |
| 10450 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 10451 | filter = lua_touserdata (L, -1); |
| 10452 | lua_pop(L, 1); |
| 10453 | |
| 10454 | register_data_filter(chn_strm(chn), chn, filter); |
| 10455 | return 1; |
| 10456 | } |
| 10457 | |
| 10458 | __LJMP static int hlua_unregister_data_filter(lua_State *L) |
| 10459 | { |
| 10460 | struct filter *filter; |
| 10461 | struct channel *chn; |
| 10462 | |
| 10463 | MAY_LJMP(check_args(L, 2, "unregister_data_filter")); |
| 10464 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 10465 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 10466 | |
| 10467 | lua_getfield(L, 1, "__filter"); |
| 10468 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 10469 | filter = lua_touserdata (L, -1); |
| 10470 | lua_pop(L, 1); |
| 10471 | |
| 10472 | unregister_data_filter(chn_strm(chn), chn, filter); |
| 10473 | return 1; |
| 10474 | } |
| 10475 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10476 | /* This function is an LUA binding used for registering a filter. It expects a |
| 10477 | * fileter name used in the haproxy configuration file and a LUA function to |
| 10478 | * parse configuration arguments. |
| 10479 | */ |
| 10480 | __LJMP static int hlua_register_filter(lua_State *L) |
| 10481 | { |
| 10482 | struct buffer *trash; |
| 10483 | struct flt_kw_list *fkl; |
| 10484 | struct flt_kw *fkw; |
| 10485 | const char *name; |
| 10486 | struct hlua_reg_filter *reg_flt= NULL; |
| 10487 | int flt_ref, fun_ref; |
| 10488 | int len; |
| 10489 | |
| 10490 | MAY_LJMP(check_args(L, 3, "register_filter")); |
| 10491 | |
| 10492 | /* First argument : filter name. */ |
| 10493 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 10494 | |
| 10495 | /* Second argument : The filter class */ |
| 10496 | flt_ref = MAY_LJMP(hlua_checktable(L, 2)); |
| 10497 | |
| 10498 | /* Third argument : lua function. */ |
| 10499 | fun_ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 10500 | |
| 10501 | trash = get_trash_chunk(); |
| 10502 | chunk_printf(trash, "lua.%s", name); |
| 10503 | fkw = flt_find_kw(trash->area); |
| 10504 | if (fkw != NULL) { |
| 10505 | reg_flt = fkw->private; |
| 10506 | if (reg_flt->flt_ref[hlua_state_id] != -1 || reg_flt->fun_ref[hlua_state_id] != -1) { |
| 10507 | ha_warning("Trying to register filter 'lua.%s' more than once. " |
| 10508 | "This will become a hard error in version 2.5.\n", name); |
| 10509 | } |
| 10510 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 10511 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 10512 | return 0; |
| 10513 | } |
| 10514 | |
| 10515 | fkl = calloc(1, sizeof(*fkl) + sizeof(struct flt_kw) * 2); |
| 10516 | if (!fkl) |
| 10517 | goto alloc_error; |
| 10518 | fkl->scope = "HLUA"; |
| 10519 | |
| 10520 | reg_flt = new_hlua_reg_filter(name); |
| 10521 | if (!reg_flt) |
| 10522 | goto alloc_error; |
| 10523 | |
| 10524 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 10525 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 10526 | |
| 10527 | /* The filter keyword */ |
| 10528 | len = strlen("lua.") + strlen(name) + 1; |
| 10529 | fkl->kw[0].kw = calloc(1, len); |
| 10530 | if (!fkl->kw[0].kw) |
| 10531 | goto alloc_error; |
| 10532 | |
| 10533 | snprintf((char *)fkl->kw[0].kw, len, "lua.%s", name); |
| 10534 | |
| 10535 | fkl->kw[0].parse = hlua_filter_parse_fct; |
| 10536 | fkl->kw[0].private = reg_flt; |
| 10537 | memset(&fkl->kw[1], 0, sizeof(*fkl->kw)); |
| 10538 | |
| 10539 | /* Register this new filter */ |
| 10540 | flt_register_keywords(fkl); |
| 10541 | |
| 10542 | return 0; |
| 10543 | |
| 10544 | alloc_error: |
| 10545 | release_hlua_reg_filter(reg_flt); |
| 10546 | ha_free(&fkl); |
| 10547 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 10548 | return 0; /* Never reached */ |
| 10549 | } |
| 10550 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 10551 | 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] | 10552 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 10553 | char **err, unsigned int *timeout) |
| 10554 | { |
| 10555 | const char *error; |
| 10556 | |
| 10557 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 10558 | if (error == PARSE_TIME_OVER) { |
| 10559 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 10560 | args[1], args[0]); |
| 10561 | return -1; |
| 10562 | } |
| 10563 | else if (error == PARSE_TIME_UNDER) { |
| 10564 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 10565 | args[1], args[0]); |
| 10566 | return -1; |
| 10567 | } |
| 10568 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 10569 | memprintf(err, "%s: invalid timeout", args[0]); |
| 10570 | return -1; |
| 10571 | } |
| 10572 | return 0; |
| 10573 | } |
| 10574 | |
| 10575 | 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] | 10576 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 10577 | char **err) |
| 10578 | { |
| 10579 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 10580 | file, line, err, &hlua_timeout_session); |
| 10581 | } |
| 10582 | |
| 10583 | 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] | 10584 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 10585 | char **err) |
| 10586 | { |
| 10587 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 10588 | file, line, err, &hlua_timeout_task); |
| 10589 | } |
| 10590 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10591 | 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] | 10592 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10593 | char **err) |
| 10594 | { |
| 10595 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 10596 | file, line, err, &hlua_timeout_applet); |
| 10597 | } |
| 10598 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 10599 | 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] | 10600 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 10601 | char **err) |
| 10602 | { |
| 10603 | char *error; |
| 10604 | |
| 10605 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 10606 | if (*error != '\0') { |
| 10607 | memprintf(err, "%s: invalid number", args[0]); |
| 10608 | return -1; |
| 10609 | } |
| 10610 | return 0; |
| 10611 | } |
| 10612 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 10613 | 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] | 10614 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 10615 | char **err) |
| 10616 | { |
| 10617 | char *error; |
| 10618 | |
| 10619 | if (*(args[1]) == 0) { |
| 10620 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 10621 | return -1; |
| 10622 | } |
| 10623 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 10624 | if (*error != '\0') { |
| 10625 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 10626 | return -1; |
| 10627 | } |
| 10628 | return 0; |
| 10629 | } |
| 10630 | |
| 10631 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10632 | /* This function is called by the main configuration key "lua-load". It loads and |
| 10633 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 10634 | * the main lua entry point. |
| 10635 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 10636 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 10637 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10638 | * |
| 10639 | * In some error case, LUA set an error message in top of the stack. This function |
| 10640 | * 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] | 10641 | * |
| 10642 | * This function can fail with an abort() due to an Lua critical error. |
| 10643 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 10644 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10645 | */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 10646 | static int hlua_load_state(char *filename, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10647 | { |
| 10648 | int error; |
| 10649 | |
| 10650 | /* Just load and compile the file. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 10651 | error = luaL_loadfile(L, filename); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10652 | if (error) { |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 10653 | memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1)); |
| 10654 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10655 | return -1; |
| 10656 | } |
| 10657 | |
| 10658 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 10659 | error = lua_pcall(L, 0, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10660 | switch (error) { |
| 10661 | case LUA_OK: |
| 10662 | break; |
| 10663 | case LUA_ERRRUN: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 10664 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1)); |
| 10665 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10666 | return -1; |
| 10667 | case LUA_ERRMEM: |
Thierry Fournier | de6145f | 2020-11-29 00:55:53 +0100 | [diff] [blame] | 10668 | memprintf(err, "Lua out of memory error\n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10669 | return -1; |
| 10670 | case LUA_ERRERR: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 10671 | memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1)); |
| 10672 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10673 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 10674 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10675 | case LUA_ERRGCMM: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 10676 | memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1)); |
| 10677 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10678 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 10679 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10680 | default: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 10681 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1)); |
| 10682 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10683 | return -1; |
| 10684 | } |
| 10685 | |
| 10686 | return 0; |
| 10687 | } |
| 10688 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 10689 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 10690 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 10691 | char **err) |
| 10692 | { |
| 10693 | if (*(args[1]) == 0) { |
| 10694 | memprintf(err, "'%s' expects a file name as parameter.\n", args[0]); |
| 10695 | return -1; |
| 10696 | } |
| 10697 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10698 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10699 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10700 | ha_set_tid(0); |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 10701 | return hlua_load_state(args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 10702 | } |
| 10703 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10704 | 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] | 10705 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10706 | char **err) |
| 10707 | { |
| 10708 | int len; |
| 10709 | |
| 10710 | if (*(args[1]) == 0) { |
| 10711 | memprintf(err, "'%s' expects a file as parameter.\n", args[0]); |
| 10712 | return -1; |
| 10713 | } |
| 10714 | |
| 10715 | if (per_thread_load == NULL) { |
| 10716 | /* allocate the first entry large enough to store the final NULL */ |
| 10717 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 10718 | if (per_thread_load == NULL) { |
| 10719 | memprintf(err, "out of memory error"); |
| 10720 | return -1; |
| 10721 | } |
| 10722 | } |
| 10723 | |
| 10724 | /* count used entries */ |
| 10725 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 10726 | ; |
| 10727 | |
| 10728 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 10729 | if (per_thread_load == NULL) { |
| 10730 | memprintf(err, "out of memory error"); |
| 10731 | return -1; |
| 10732 | } |
| 10733 | |
| 10734 | per_thread_load[len] = strdup(args[1]); |
| 10735 | per_thread_load[len + 1] = NULL; |
| 10736 | |
| 10737 | if (per_thread_load[len] == NULL) { |
| 10738 | memprintf(err, "out of memory error"); |
| 10739 | return -1; |
| 10740 | } |
| 10741 | |
| 10742 | /* loading for thread 1 only */ |
| 10743 | hlua_state_id = 1; |
| 10744 | ha_set_tid(0); |
| 10745 | return hlua_load_state(args[1], hlua_states[1], err); |
| 10746 | } |
| 10747 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 10748 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 10749 | * in the given <ctx>. |
| 10750 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 10751 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 10752 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 10753 | lua_getglobal(L, "package"); /* push package variable */ |
| 10754 | lua_pushstring(L, path); /* push given path */ |
| 10755 | lua_pushstring(L, ";"); /* push semicolon */ |
| 10756 | lua_getfield(L, -3, type); /* push old path */ |
| 10757 | lua_concat(L, 3); /* concatenate to new path */ |
| 10758 | lua_setfield(L, -2, type); /* store new path */ |
| 10759 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 10760 | |
| 10761 | return 0; |
| 10762 | } |
| 10763 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 10764 | 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] | 10765 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 10766 | char **err) |
| 10767 | { |
| 10768 | char *path; |
| 10769 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 10770 | struct prepend_path *p = NULL; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10771 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 10772 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 10773 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 10774 | } |
| 10775 | |
| 10776 | if (!(*args[1])) { |
| 10777 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 10778 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 10779 | } |
| 10780 | path = args[1]; |
| 10781 | |
| 10782 | if (*args[2]) { |
| 10783 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 10784 | 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] | 10785 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 10786 | } |
| 10787 | type = args[2]; |
| 10788 | } |
| 10789 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10790 | p = calloc(1, sizeof(*p)); |
| 10791 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 10792 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 10793 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10794 | } |
| 10795 | p->path = strdup(path); |
| 10796 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 10797 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 10798 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10799 | } |
| 10800 | p->type = strdup(type); |
| 10801 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 10802 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 10803 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10804 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 10805 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10806 | |
| 10807 | hlua_prepend_path(hlua_states[0], type, path); |
| 10808 | hlua_prepend_path(hlua_states[1], type, path); |
| 10809 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 10810 | |
| 10811 | err2: |
| 10812 | free(p->type); |
| 10813 | free(p->path); |
| 10814 | err: |
| 10815 | free(p); |
| 10816 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 10817 | } |
| 10818 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10819 | /* configuration keywords declaration */ |
| 10820 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 10821 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 10822 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10823 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 10824 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 10825 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 10826 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 10827 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 10828 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10829 | { 0, NULL, NULL }, |
| 10830 | }}; |
| 10831 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 10832 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 10833 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 10834 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 10835 | /* This function can fail with an abort() due to an Lua critical error. |
| 10836 | * We are in the initialisation process of HAProxy, this abort() is |
| 10837 | * tolerated. |
| 10838 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 10839 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 10840 | { |
| 10841 | struct hlua_init_function *init; |
| 10842 | const char *msg; |
| 10843 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 10844 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 10845 | const char *kind; |
| 10846 | const char *trace; |
| 10847 | int return_status = 1; |
| 10848 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 10849 | int nres; |
| 10850 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 10851 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 10852 | /* disable memory limit checks if limit is not set */ |
| 10853 | if (!hlua_global_allocator.limit) |
| 10854 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 10855 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 10856 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 10857 | if (setjmp(safe_ljmp_env) != 0) { |
| 10858 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 10859 | if (lua_type(L, -1) == LUA_TSTRING) |
| 10860 | error = lua_tostring(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 10861 | else |
| 10862 | error = "critical error"; |
| 10863 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 10864 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 10865 | } else { |
| 10866 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 10867 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 10868 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 10869 | hlua_fcn_post_init(L); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 10870 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10871 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 10872 | lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 10873 | |
| 10874 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 10875 | ret = lua_resume(L, L, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 10876 | #else |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 10877 | ret = lua_resume(L, L, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 10878 | #endif |
| 10879 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 10880 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 10881 | |
| 10882 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 10883 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 10884 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 10885 | |
| 10886 | case LUA_ERRERR: |
| 10887 | kind = "message handler error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 10888 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 10889 | case LUA_ERRRUN: |
| 10890 | if (!kind) |
| 10891 | kind = "runtime error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 10892 | msg = lua_tostring(L, -1); |
| 10893 | lua_settop(L, 0); /* Empty the stack. */ |
| 10894 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 10895 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 10896 | if (msg) |
| 10897 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 10898 | else |
| 10899 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
| 10900 | return_status = 0; |
| 10901 | break; |
| 10902 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 10903 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 10904 | /* Unknown error */ |
| 10905 | kind = "Unknown error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 10906 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 10907 | case LUA_YIELD: |
| 10908 | /* yield is not configured at this step, this state doesn't happen */ |
| 10909 | if (!kind) |
| 10910 | kind = "yield not allowed"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 10911 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 10912 | case LUA_ERRMEM: |
| 10913 | if (!kind) |
| 10914 | kind = "out of memory error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 10915 | lua_settop(L, 0); |
| 10916 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 10917 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 10918 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 10919 | return_status = 0; |
| 10920 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 10921 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 10922 | if (!return_status) |
| 10923 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 10924 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 10925 | |
| 10926 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 10927 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 10928 | } |
| 10929 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 10930 | int hlua_post_init() |
| 10931 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10932 | int ret; |
| 10933 | int i; |
| 10934 | int errors; |
| 10935 | char *err = NULL; |
| 10936 | struct hlua_function *fcn; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10937 | struct hlua_reg_filter *reg_flt; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10938 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 10939 | #if USE_OPENSSL |
| 10940 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 10941 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 10942 | int saved_used_backed = global.ssl_used_backend; |
| 10943 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 10944 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 10945 | global.ssl_used_backend = saved_used_backed; |
| 10946 | } |
| 10947 | #endif |
| 10948 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10949 | /* Perform post init of common thread */ |
| 10950 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10951 | ha_set_tid(0); |
| 10952 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 10953 | if (ret == 0) |
| 10954 | return 0; |
| 10955 | |
| 10956 | /* init remaining lua states and load files */ |
| 10957 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 10958 | |
| 10959 | /* set thread context */ |
| 10960 | ha_set_tid(hlua_state_id - 1); |
| 10961 | |
| 10962 | /* Init lua state */ |
| 10963 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 10964 | |
| 10965 | /* Load lua files */ |
| 10966 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 10967 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 10968 | if (ret != 0) { |
| 10969 | ha_alert("Lua init: %s\n", err); |
| 10970 | return 0; |
| 10971 | } |
| 10972 | } |
| 10973 | } |
| 10974 | |
| 10975 | /* Reset thread context */ |
| 10976 | ha_set_tid(0); |
| 10977 | |
| 10978 | /* Execute post init for all states */ |
| 10979 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 10980 | |
| 10981 | /* set thread context */ |
| 10982 | ha_set_tid(hlua_state_id - 1); |
| 10983 | |
| 10984 | /* run post init */ |
| 10985 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 10986 | if (ret == 0) |
| 10987 | return 0; |
| 10988 | } |
| 10989 | |
| 10990 | /* Reset thread context */ |
| 10991 | ha_set_tid(0); |
| 10992 | |
| 10993 | /* control functions registering. Each function must have: |
| 10994 | * - only the function_ref[0] set positive and all other to -1 |
| 10995 | * - only the function_ref[0] set to -1 and all other positive |
| 10996 | * This ensure a same reference is not used both in shared |
| 10997 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 10998 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10999 | * complicated to found for the end user. |
| 11000 | */ |
| 11001 | errors = 0; |
| 11002 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 11003 | ret = 0; |
| 11004 | for (i = 1; i < global.nbthread + 1; i++) { |
| 11005 | if (fcn->function_ref[i] == -1) |
| 11006 | ret--; |
| 11007 | else |
| 11008 | ret++; |
| 11009 | } |
| 11010 | if (abs(ret) != global.nbthread) { |
| 11011 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 11012 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 11013 | errors++; |
| 11014 | continue; |
| 11015 | } |
| 11016 | |
| 11017 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11018 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 11019 | "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] | 11020 | "exclusive.\n", fcn->name); |
| 11021 | errors++; |
| 11022 | } |
| 11023 | } |
| 11024 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11025 | /* Do the same with registered filters */ |
| 11026 | list_for_each_entry(reg_flt, &referenced_filters, l) { |
| 11027 | ret = 0; |
| 11028 | for (i = 1; i < global.nbthread + 1; i++) { |
| 11029 | if (reg_flt->flt_ref[i] == -1) |
| 11030 | ret--; |
| 11031 | else |
| 11032 | ret++; |
| 11033 | } |
| 11034 | if (abs(ret) != global.nbthread) { |
| 11035 | ha_alert("Lua filter '%s' is not referenced in all thread. " |
| 11036 | "Expect function in all thread or in none thread.\n", reg_flt->name); |
| 11037 | errors++; |
| 11038 | continue; |
| 11039 | } |
| 11040 | |
| 11041 | if ((reg_flt->flt_ref[0] == -1) == (ret < 0)) { |
| 11042 | ha_alert("Lua filter '%s' is referenced both ins shared Lua context (through lua-load) " |
| 11043 | "and per-thread Lua context (through lua-load-per-thread). these two context " |
| 11044 | "exclusive.\n", fcn->name); |
| 11045 | errors++; |
| 11046 | } |
| 11047 | } |
| 11048 | |
| 11049 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11050 | if (errors > 0) |
| 11051 | return 0; |
| 11052 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11053 | /* 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] | 11054 | * -1 in order to have probably a segfault if someone use it |
| 11055 | */ |
| 11056 | hlua_state_id = -1; |
| 11057 | |
| 11058 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11059 | } |
| 11060 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11061 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 11062 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 11063 | * 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] | 11064 | * allocation. <nsize> is the requested new size. A new allocation is |
| 11065 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11066 | * zero. This one verifies that the limits are respected but is optimized |
| 11067 | * 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] | 11068 | */ |
| 11069 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 11070 | { |
| 11071 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11072 | size_t limit, old, new; |
| 11073 | |
Tim Duesterhus | 2258652 | 2021-01-08 10:35:33 +0100 | [diff] [blame] | 11074 | if (unlikely(!ptr && !nsize)) |
| 11075 | return NULL; |
| 11076 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11077 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 11078 | * for accounting anymore. |
| 11079 | */ |
Christopher Faulet | cc2c4f8 | 2021-03-24 14:52:24 +0100 | [diff] [blame] | 11080 | if (likely(~zone->limit == 0)) |
| 11081 | return realloc(ptr, nsize); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11082 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 11083 | if (!ptr) |
| 11084 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11085 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11086 | /* enforce strict limits across all threads */ |
| 11087 | limit = zone->limit; |
| 11088 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 11089 | do { |
| 11090 | new = old + nsize - osize; |
| 11091 | if (unlikely(nsize && limit && new > limit)) |
| 11092 | return NULL; |
| 11093 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11094 | |
| 11095 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11096 | |
| 11097 | if (unlikely(!ptr && nsize)) // failed |
| 11098 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 11099 | |
| 11100 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11101 | return ptr; |
| 11102 | } |
| 11103 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11104 | /* 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] | 11105 | * We are in the initialisation process of HAProxy, this abort() is |
| 11106 | * tolerated. |
| 11107 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11108 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 11109 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11110 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11111 | int idx; |
| 11112 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11113 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11114 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 11115 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11116 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11117 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11118 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11119 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11120 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11121 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11122 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11123 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11124 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11125 | *context = NULL; |
| 11126 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 11127 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 11128 | * the Lua function can fail with an abort. We are in the initialisation |
| 11129 | * process of HAProxy, this abort() is tolerated. |
| 11130 | */ |
| 11131 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11132 | /* Call post initialisation function in safe environment. */ |
| 11133 | if (setjmp(safe_ljmp_env) != 0) { |
| 11134 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11135 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11136 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 11137 | else |
| 11138 | error_msg = "critical error"; |
| 11139 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 11140 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11141 | } else { |
| 11142 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 11143 | } |
| 11144 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11145 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11146 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 11147 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 11148 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 11149 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11150 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 11151 | #endif |
| 11152 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11153 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 11154 | #endif |
| 11155 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 11156 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11157 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11158 | /* Apply configured prepend path */ |
| 11159 | list_for_each_entry(pp, &prepend_path_list, l) |
| 11160 | hlua_prepend_path(L, pp->type, pp->path); |
| 11161 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11162 | /* |
| 11163 | * |
| 11164 | * Create "core" object. |
| 11165 | * |
| 11166 | */ |
| 11167 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 11168 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11169 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11170 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11171 | /* set the thread id */ |
| 11172 | hlua_class_const_int(L, "thread", thread_num); |
| 11173 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11174 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 11175 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11176 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11177 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11178 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11179 | hlua_class_function(L, "register_init", hlua_register_init); |
| 11180 | hlua_class_function(L, "register_task", hlua_register_task); |
| 11181 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 11182 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 11183 | hlua_class_function(L, "register_action", hlua_register_action); |
| 11184 | hlua_class_function(L, "register_service", hlua_register_service); |
| 11185 | hlua_class_function(L, "register_cli", hlua_register_cli); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11186 | hlua_class_function(L, "register_filter", hlua_register_filter); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11187 | hlua_class_function(L, "yield", hlua_yield); |
| 11188 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 11189 | hlua_class_function(L, "sleep", hlua_sleep); |
| 11190 | hlua_class_function(L, "msleep", hlua_msleep); |
| 11191 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 11192 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 11193 | hlua_class_function(L, "set_map", hlua_set_map); |
| 11194 | hlua_class_function(L, "del_map", hlua_del_map); |
| 11195 | hlua_class_function(L, "tcp", hlua_socket_new); |
| 11196 | hlua_class_function(L, "log", hlua_log); |
| 11197 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 11198 | hlua_class_function(L, "Info", hlua_log_info); |
| 11199 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 11200 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 11201 | hlua_class_function(L, "done", hlua_done); |
| 11202 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11203 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11204 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 11205 | |
| 11206 | /* |
| 11207 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11208 | * Create "act" object. |
| 11209 | * |
| 11210 | */ |
| 11211 | |
| 11212 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11213 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11214 | |
| 11215 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11216 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 11217 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 11218 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 11219 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 11220 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 11221 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 11222 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 11223 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11224 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11225 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 11226 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11227 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11228 | |
| 11229 | /* |
| 11230 | * |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11231 | * Create "Filter" object. |
| 11232 | * |
| 11233 | */ |
| 11234 | |
| 11235 | /* This table entry is the object "filter" base. */ |
| 11236 | lua_newtable(L); |
| 11237 | |
| 11238 | /* push flags and constants */ |
| 11239 | hlua_class_const_int(L, "CONTINUE", 1); |
| 11240 | hlua_class_const_int(L, "WAIT", 0); |
| 11241 | hlua_class_const_int(L, "ERROR", -1); |
| 11242 | |
| 11243 | hlua_class_const_int(L, "FLT_CFG_FL_HTX", FLT_CFG_FL_HTX); |
| 11244 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11245 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
| 11246 | hlua_class_function(L, "register_data_filter", hlua_register_data_filter); |
| 11247 | hlua_class_function(L, "unregister_data_filter", hlua_unregister_data_filter); |
| 11248 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11249 | lua_setglobal(L, "filter"); |
| 11250 | |
| 11251 | /* |
| 11252 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11253 | * Register class Map |
| 11254 | * |
| 11255 | */ |
| 11256 | |
| 11257 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11258 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11259 | |
| 11260 | /* register pattern types. */ |
| 11261 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11262 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 11263 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11264 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11265 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 11266 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11267 | |
| 11268 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11269 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11270 | |
| 11271 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11272 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11273 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11274 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11275 | lua_pushstring(L, "__index"); |
| 11276 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11277 | |
| 11278 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11279 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 11280 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11281 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11282 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11283 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 11284 | /* Register previous table in the registry with reference and named entry. |
| 11285 | * The function hlua_register_metatable() pops the stack, so we |
| 11286 | * previously create a copy of the table. |
| 11287 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11288 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 11289 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11290 | |
| 11291 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11292 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11293 | |
| 11294 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11295 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11296 | |
| 11297 | /* |
| 11298 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11299 | * Register class Channel |
| 11300 | * |
| 11301 | */ |
| 11302 | |
| 11303 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11304 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11305 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11306 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11307 | lua_pushstring(L, "__index"); |
| 11308 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11309 | |
| 11310 | /* Register . */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 11311 | hlua_class_function(L, "data", hlua_channel_get_data); |
| 11312 | hlua_class_function(L, "line", hlua_channel_get_line); |
| 11313 | hlua_class_function(L, "set", hlua_channel_set_data); |
| 11314 | hlua_class_function(L, "remove", hlua_channel_del_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11315 | hlua_class_function(L, "append", hlua_channel_append); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 11316 | hlua_class_function(L, "prepend", hlua_channel_prepend); |
| 11317 | hlua_class_function(L, "insert", hlua_channel_insert_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11318 | hlua_class_function(L, "send", hlua_channel_send); |
| 11319 | hlua_class_function(L, "forward", hlua_channel_forward); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 11320 | hlua_class_function(L, "input", hlua_channel_get_in_len); |
| 11321 | hlua_class_function(L, "output", hlua_channel_get_out_len); |
| 11322 | hlua_class_function(L, "may_recv", hlua_channel_may_recv); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11323 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 11324 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11325 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 11326 | /* Deprecated API */ |
| 11327 | hlua_class_function(L, "get", hlua_channel_get); |
| 11328 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 11329 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 11330 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 11331 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 11332 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11333 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11334 | |
| 11335 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11336 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11337 | |
| 11338 | /* |
| 11339 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 11340 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 11341 | * |
| 11342 | */ |
| 11343 | |
| 11344 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11345 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 11346 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11347 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11348 | lua_pushstring(L, "__index"); |
| 11349 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 11350 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11351 | /* Browse existing fetches and create the associated |
| 11352 | * object method. |
| 11353 | */ |
| 11354 | sf = NULL; |
| 11355 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11356 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 11357 | * by an underscore. |
| 11358 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11359 | strncpy(trash.area, sf->kw, trash.size); |
| 11360 | trash.area[trash.size - 1] = '\0'; |
| 11361 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11362 | if (*p == '.' || *p == '-' || *p == '+') |
| 11363 | *p = '_'; |
| 11364 | |
| 11365 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11366 | lua_pushstring(L, trash.area); |
| 11367 | lua_pushlightuserdata(L, sf); |
| 11368 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 11369 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11370 | } |
| 11371 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11372 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 11373 | |
| 11374 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11375 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 11376 | |
| 11377 | /* |
| 11378 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11379 | * Register class Converters |
| 11380 | * |
| 11381 | */ |
| 11382 | |
| 11383 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11384 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11385 | |
| 11386 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11387 | lua_pushstring(L, "__index"); |
| 11388 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11389 | |
| 11390 | /* Browse existing converters and create the associated |
| 11391 | * object method. |
| 11392 | */ |
| 11393 | sc = NULL; |
| 11394 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11395 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 11396 | * by an underscore. |
| 11397 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11398 | strncpy(trash.area, sc->kw, trash.size); |
| 11399 | trash.area[trash.size - 1] = '\0'; |
| 11400 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11401 | if (*p == '.' || *p == '-' || *p == '+') |
| 11402 | *p = '_'; |
| 11403 | |
| 11404 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11405 | lua_pushstring(L, trash.area); |
| 11406 | lua_pushlightuserdata(L, sc); |
| 11407 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 11408 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11409 | } |
| 11410 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11411 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11412 | |
| 11413 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11414 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11415 | |
| 11416 | /* |
| 11417 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11418 | * Register class HTTP |
| 11419 | * |
| 11420 | */ |
| 11421 | |
| 11422 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11423 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11424 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11425 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11426 | lua_pushstring(L, "__index"); |
| 11427 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11428 | |
| 11429 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11430 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 11431 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 11432 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 11433 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 11434 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 11435 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 11436 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 11437 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 11438 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 11439 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11440 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11441 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 11442 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 11443 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 11444 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 11445 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 11446 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 11447 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11448 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11449 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11450 | |
| 11451 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11452 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11453 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 11454 | /* |
| 11455 | * |
| 11456 | * Register class HTTPMessage |
| 11457 | * |
| 11458 | */ |
| 11459 | |
| 11460 | /* Create and fill the metatable. */ |
| 11461 | lua_newtable(L); |
| 11462 | |
| 11463 | /* Create and fille the __index entry. */ |
| 11464 | lua_pushstring(L, "__index"); |
| 11465 | lua_newtable(L); |
| 11466 | |
| 11467 | /* Register Lua functions. */ |
| 11468 | hlua_class_function(L, "is_resp", hlua_http_msg_is_resp); |
| 11469 | hlua_class_function(L, "get_stline", hlua_http_msg_get_stline); |
| 11470 | hlua_class_function(L, "get_headers", hlua_http_msg_get_headers); |
| 11471 | hlua_class_function(L, "del_header", hlua_http_msg_del_hdr); |
| 11472 | hlua_class_function(L, "rep_header", hlua_http_msg_rep_hdr); |
| 11473 | hlua_class_function(L, "rep_value", hlua_http_msg_rep_val); |
| 11474 | hlua_class_function(L, "add_header", hlua_http_msg_add_hdr); |
| 11475 | hlua_class_function(L, "set_header", hlua_http_msg_set_hdr); |
| 11476 | hlua_class_function(L, "set_method", hlua_http_msg_set_meth); |
| 11477 | hlua_class_function(L, "set_path", hlua_http_msg_set_path); |
| 11478 | hlua_class_function(L, "set_query", hlua_http_msg_set_query); |
| 11479 | hlua_class_function(L, "set_uri", hlua_http_msg_set_uri); |
| 11480 | hlua_class_function(L, "set_status", hlua_http_msg_set_status); |
| 11481 | hlua_class_function(L, "is_full", hlua_http_msg_is_full); |
| 11482 | hlua_class_function(L, "may_recv", hlua_http_msg_may_recv); |
| 11483 | hlua_class_function(L, "eom", hlua_http_msg_is_eom); |
| 11484 | hlua_class_function(L, "input", hlua_http_msg_get_in_len); |
| 11485 | hlua_class_function(L, "output", hlua_http_msg_get_out_len); |
| 11486 | |
| 11487 | hlua_class_function(L, "body", hlua_http_msg_get_body); |
| 11488 | hlua_class_function(L, "set", hlua_http_msg_set_data); |
| 11489 | hlua_class_function(L, "remove", hlua_http_msg_del_data); |
| 11490 | hlua_class_function(L, "append", hlua_http_msg_append); |
| 11491 | hlua_class_function(L, "prepend", hlua_http_msg_prepend); |
| 11492 | hlua_class_function(L, "insert", hlua_http_msg_insert_data); |
| 11493 | hlua_class_function(L, "set_eom", hlua_http_msg_set_eom); |
| 11494 | hlua_class_function(L, "unset_eom", hlua_http_msg_unset_eom); |
| 11495 | |
| 11496 | hlua_class_function(L, "send", hlua_http_msg_send); |
| 11497 | hlua_class_function(L, "forward", hlua_http_msg_forward); |
| 11498 | |
| 11499 | lua_rawset(L, -3); |
| 11500 | |
| 11501 | /* Register previous table in the registry with reference and named entry. */ |
| 11502 | class_http_msg_ref = hlua_register_metatable(L, CLASS_HTTP_MSG); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11503 | /* |
| 11504 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11505 | * Register class AppletTCP |
| 11506 | * |
| 11507 | */ |
| 11508 | |
| 11509 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11510 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11511 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11512 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11513 | lua_pushstring(L, "__index"); |
| 11514 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11515 | |
| 11516 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11517 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 11518 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 11519 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 11520 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 11521 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 11522 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 11523 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 11524 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11525 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11526 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11527 | |
| 11528 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11529 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11530 | |
| 11531 | /* |
| 11532 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 11533 | * Register class AppletHTTP |
| 11534 | * |
| 11535 | */ |
| 11536 | |
| 11537 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11538 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 11539 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11540 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11541 | lua_pushstring(L, "__index"); |
| 11542 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 11543 | |
| 11544 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11545 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 11546 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 11547 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 11548 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 11549 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 11550 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 11551 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 11552 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 11553 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 11554 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 11555 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 11556 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11557 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 11558 | |
| 11559 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11560 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 11561 | |
| 11562 | /* |
| 11563 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 11564 | * Register class TXN |
| 11565 | * |
| 11566 | */ |
| 11567 | |
| 11568 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11569 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 11570 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11571 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11572 | lua_pushstring(L, "__index"); |
| 11573 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 11574 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 11575 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11576 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 11577 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 11578 | hlua_class_function(L, "set_var", hlua_set_var); |
| 11579 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 11580 | hlua_class_function(L, "get_var", hlua_get_var); |
| 11581 | hlua_class_function(L, "done", hlua_txn_done); |
| 11582 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 11583 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 11584 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 11585 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 11586 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 11587 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 11588 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 11589 | hlua_class_function(L, "log", hlua_txn_log); |
| 11590 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 11591 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 11592 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 11593 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 11594 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11595 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 11596 | |
| 11597 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11598 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11599 | |
| 11600 | /* |
| 11601 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 11602 | * Register class reply |
| 11603 | * |
| 11604 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11605 | lua_newtable(L); |
| 11606 | lua_pushstring(L, "__index"); |
| 11607 | lua_newtable(L); |
| 11608 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 11609 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 11610 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 11611 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 11612 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 11613 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 11614 | |
| 11615 | |
| 11616 | /* |
| 11617 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11618 | * Register class Socket |
| 11619 | * |
| 11620 | */ |
| 11621 | |
| 11622 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11623 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11624 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11625 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11626 | lua_pushstring(L, "__index"); |
| 11627 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11628 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 11629 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11630 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 11631 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11632 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 11633 | hlua_class_function(L, "send", hlua_socket_send); |
| 11634 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 11635 | hlua_class_function(L, "close", hlua_socket_close); |
| 11636 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 11637 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 11638 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 11639 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11640 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11641 | 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] | 11642 | |
| 11643 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11644 | lua_pushstring(L, "__gc"); |
| 11645 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 11646 | 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] | 11647 | |
| 11648 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11649 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11650 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 11651 | lua_atpanic(L, hlua_panic_safe); |
| 11652 | |
| 11653 | return L; |
| 11654 | } |
| 11655 | |
| 11656 | void hlua_init(void) { |
| 11657 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 11658 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 11659 | #ifdef USE_OPENSSL |
| 11660 | struct srv_kw *kw; |
| 11661 | int tmp_error; |
| 11662 | char *error; |
| 11663 | char *args[] = { /* SSL client configuration. */ |
| 11664 | "ssl", |
| 11665 | "verify", |
| 11666 | "none", |
| 11667 | NULL |
| 11668 | }; |
| 11669 | #endif |
| 11670 | |
| 11671 | /* Init post init function list head */ |
| 11672 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 11673 | LIST_INIT(&hlua_init_functions[i]); |
| 11674 | |
| 11675 | /* Init state for common/shared lua parts */ |
| 11676 | hlua_state_id = 0; |
| 11677 | ha_set_tid(0); |
| 11678 | hlua_states[0] = hlua_init_state(0); |
| 11679 | |
| 11680 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 11681 | hlua_state_id = 1; |
| 11682 | ha_set_tid(0); |
| 11683 | hlua_states[1] = hlua_init_state(1); |
| 11684 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11685 | /* Proxy and server configuration initialisation. */ |
William Lallemand | 6bb77b9 | 2021-07-28 15:48:16 +0200 | [diff] [blame] | 11686 | 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] | 11687 | if (!socket_proxy) { |
| 11688 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 11689 | exit(1); |
| 11690 | } |
| 11691 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11692 | |
| 11693 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11694 | socket_tcp = new_server(socket_proxy); |
| 11695 | if (!socket_tcp) { |
| 11696 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 11697 | exit(1); |
| 11698 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11699 | |
| 11700 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11701 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11702 | socket_ssl = new_server(socket_proxy); |
| 11703 | if (!socket_ssl) { |
| 11704 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 11705 | exit(1); |
| 11706 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11707 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11708 | socket_ssl->use_ssl = 1; |
| 11709 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11710 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 11711 | for (i = 0; args[i] != NULL; i++) { |
| 11712 | 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] | 11713 | /* |
| 11714 | * |
| 11715 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 11716 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11717 | * features like client certificates and ssl_verify. |
| 11718 | * |
| 11719 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11720 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11721 | if (tmp_error != 0) { |
| 11722 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 11723 | abort(); /* This must be never arrives because the command line |
| 11724 | not editable by the user. */ |
| 11725 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 11726 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11727 | } |
| 11728 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11729 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 11730 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 11731 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 11732 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 11733 | static void hlua_deinit() |
| 11734 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 11735 | int thr; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11736 | struct hlua_reg_filter *reg_flt, *reg_flt_bck; |
| 11737 | |
| 11738 | list_for_each_entry_safe(reg_flt, reg_flt_bck, &referenced_filters, l) |
| 11739 | release_hlua_reg_filter(reg_flt); |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 11740 | |
| 11741 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 11742 | if (hlua_states[thr]) |
| 11743 | lua_close(hlua_states[thr]); |
| 11744 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 11745 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11746 | free_server(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 11747 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 11748 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11749 | free_server(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 11750 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 11751 | |
| 11752 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 11753 | } |
| 11754 | |
| 11755 | REGISTER_POST_DEINIT(hlua_deinit); |
| 11756 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 11757 | static void hlua_register_build_options(void) |
| 11758 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 11759 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 11760 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 11761 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 11762 | hap_register_build_opts(ptr, 1); |
| 11763 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 11764 | |
| 11765 | INITCALL0(STG_REGISTER, hlua_register_build_options); |