Thierry Fournier | e726b14 | 2016-02-11 17:57:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Lua unsafe core engine |
| 3 | * |
| 4 | * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Bertrand Jacquin | f4c12d4 | 2021-01-21 21:14:07 +0000 | [diff] [blame] | 13 | #define _GNU_SOURCE |
| 14 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 15 | #include <ctype.h> |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 16 | #include <setjmp.h> |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 17 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 18 | #include <lauxlib.h> |
| 19 | #include <lua.h> |
| 20 | #include <lualib.h> |
| 21 | |
Thierry FOURNIER | 463119c | 2015-03-10 00:35:36 +0100 | [diff] [blame] | 22 | #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503 |
| 23 | #error "Requires Lua 5.3 or later." |
Cyril Bonté | dc0306e | 2015-03-02 00:08:40 +0100 | [diff] [blame] | 24 | #endif |
| 25 | |
Willy Tarreau | 8d2b777 | 2020-05-27 10:58:19 +0200 | [diff] [blame] | 26 | #include <import/ebpttree.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 27 | |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 28 | #include <haproxy/api.h> |
| 29 | #include <haproxy/applet.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 30 | #include <haproxy/arg.h> |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 31 | #include <haproxy/auth.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 32 | #include <haproxy/cfgparse.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 33 | #include <haproxy/channel.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 34 | #include <haproxy/cli.h> |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 35 | #include <haproxy/clock.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 36 | #include <haproxy/connection.h> |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 37 | #include <haproxy/conn_stream.h> |
| 38 | #include <haproxy/cs_utils.h> |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 39 | #include <haproxy/filters.h> |
Willy Tarreau | 5413a87 | 2020-06-02 19:33:08 +0200 | [diff] [blame] | 40 | #include <haproxy/h1.h> |
Willy Tarreau | 8641605 | 2020-06-04 09:20:54 +0200 | [diff] [blame] | 41 | #include <haproxy/hlua.h> |
Willy Tarreau | 8c79400 | 2020-06-04 10:05:25 +0200 | [diff] [blame] | 42 | #include <haproxy/hlua_fcn.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 43 | #include <haproxy/http_ana.h> |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 44 | #include <haproxy/http_client.h> |
Willy Tarreau | 126ba3a | 2020-06-04 18:26:43 +0200 | [diff] [blame] | 45 | #include <haproxy/http_fetch.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 46 | #include <haproxy/http_htx.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 47 | #include <haproxy/http_rules.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 48 | #include <haproxy/log.h> |
Willy Tarreau | 2cd5809 | 2020-06-04 15:10:43 +0200 | [diff] [blame] | 49 | #include <haproxy/map.h> |
Willy Tarreau | 8efbdfb | 2020-06-04 11:29:21 +0200 | [diff] [blame] | 50 | #include <haproxy/obj_type.h> |
Willy Tarreau | 225a90a | 2020-06-04 15:06:28 +0200 | [diff] [blame] | 51 | #include <haproxy/pattern.h> |
Willy Tarreau | 469509b | 2020-06-04 15:13:30 +0200 | [diff] [blame] | 52 | #include <haproxy/payload.h> |
Willy Tarreau | 3d6ee40 | 2021-05-08 20:28:07 +0200 | [diff] [blame] | 53 | #include <haproxy/proxy.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 54 | #include <haproxy/regex.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 55 | #include <haproxy/sample.h> |
Willy Tarreau | 198e92a | 2021-03-05 10:23:32 +0100 | [diff] [blame] | 56 | #include <haproxy/server.h> |
Willy Tarreau | 48d25b3 | 2020-06-04 18:58:52 +0200 | [diff] [blame] | 57 | #include <haproxy/session.h> |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 58 | #include <haproxy/ssl_ckch.h> |
| 59 | #include <haproxy/ssl_sock.h> |
Willy Tarreau | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 60 | #include <haproxy/stats-t.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 61 | #include <haproxy/stream.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 62 | #include <haproxy/task.h> |
Willy Tarreau | 8b550af | 2020-06-04 17:42:48 +0200 | [diff] [blame] | 63 | #include <haproxy/tcp_rules.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 64 | #include <haproxy/thread.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 65 | #include <haproxy/tools.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 66 | #include <haproxy/vars.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 67 | #include <haproxy/xref.h> |
| 68 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 69 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 70 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 71 | * macro is used only for identifying the function that can |
| 72 | * not return because a longjmp is executed. |
| 73 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 74 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 75 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 76 | */ |
| 77 | #define __LJMP |
Willy Tarreau | 4e7cc33 | 2018-10-20 17:45:48 +0200 | [diff] [blame] | 78 | #define WILL_LJMP(func) do { func; my_unreachable(); } while(0) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 79 | #define MAY_LJMP(func) func |
| 80 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 81 | /* This couple of function executes securely some Lua calls outside of |
| 82 | * the lua runtime environment. Each Lua call can return a longjmp |
| 83 | * if it encounter a memory error. |
| 84 | * |
| 85 | * Lua documentation extract: |
| 86 | * |
| 87 | * If an error happens outside any protected environment, Lua calls |
| 88 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 89 | * exiting the host application. Your panic function can avoid this |
| 90 | * exit by never returning (e.g., doing a long jump to your own |
| 91 | * recovery point outside Lua). |
| 92 | * |
| 93 | * The panic function runs as if it were a message handler (see |
Willy Tarreau | 3dfb7da | 2022-03-02 22:33:39 +0100 | [diff] [blame] | 94 | * #2.3); in particular, the error message is at the top of the |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 95 | * stack. However, there is no guarantee about stack space. To push |
| 96 | * anything on the stack, the panic function must first check the |
Willy Tarreau | 3dfb7da | 2022-03-02 22:33:39 +0100 | [diff] [blame] | 97 | * available space (see #4.2). |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 98 | * |
| 99 | * We must check all the Lua entry point. This includes: |
| 100 | * - The include/proto/hlua.h exported functions |
| 101 | * - the task wrapper function |
| 102 | * - The action wrapper function |
| 103 | * - The converters wrapper function |
| 104 | * - The sample-fetch wrapper functions |
| 105 | * |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 106 | * It is tolerated that the initialisation function returns an abort. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 107 | * Before each Lua abort, an error message is written on stderr. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 108 | * |
| 109 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 110 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 111 | * because they must be exists in the program stack when the longjmp |
| 112 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 113 | * |
| 114 | * Note that the Lua processing is not really thread safe. It provides |
| 115 | * heavy system which consists to add our own lock function in the Lua |
| 116 | * code and recompile the library. This system will probably not accepted |
| 117 | * by maintainers of various distribs. |
| 118 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 119 | * 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] | 120 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 121 | * of function and a lua_unlock() at the end of the function. So I |
| 122 | * conclude that the Lua thread safe mode just perform a mutex around |
| 123 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 124 | * easier for distro maintainers. |
| 125 | * |
| 126 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 127 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 128 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 129 | */ |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 130 | __decl_spinlock(hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 131 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 132 | static int hlua_panic_safe(lua_State *L) { return 0; } |
Willy Tarreau | 6a51090 | 2021-07-14 19:41:25 +0200 | [diff] [blame] | 133 | 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] | 134 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 135 | /* This is the chained list of struct hlua_function referenced |
| 136 | * for haproxy action, sample-fetches, converters, cli and |
| 137 | * applet bindings. It is used for a post-initialisation control. |
| 138 | */ |
| 139 | static struct list referenced_functions = LIST_HEAD_INIT(referenced_functions); |
| 140 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 141 | /* This variable is used only during initialization to identify the Lua state |
| 142 | * currently being initialized. 0 is the common lua state, 1 to n are the Lua |
| 143 | * states dedicated to each thread (in this case hlua_state_id==tid+1). |
| 144 | */ |
| 145 | static int hlua_state_id; |
| 146 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 147 | /* This is a NULL-terminated list of lua file which are referenced to load per thread */ |
| 148 | static char **per_thread_load = NULL; |
| 149 | |
| 150 | lua_State *hlua_init_state(int thread_id); |
| 151 | |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 152 | /* This function takes the Lua global lock. Keep this function's visibility |
| 153 | * global so that it can appear in stack dumps and performance profiles! |
| 154 | */ |
| 155 | void lua_take_global_lock() |
| 156 | { |
| 157 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
| 158 | } |
| 159 | |
| 160 | static inline void lua_drop_global_lock() |
| 161 | { |
| 162 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
| 163 | } |
| 164 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 165 | #define SET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 166 | ({ \ |
| 167 | int ret; \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 168 | if ((__HLUA)->state_id == 0) \ |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 169 | lua_take_global_lock(); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 170 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 171 | lua_atpanic(__L, hlua_panic_safe); \ |
| 172 | ret = 0; \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 173 | if ((__HLUA)->state_id == 0) \ |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 174 | lua_drop_global_lock(); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 175 | } else { \ |
| 176 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 177 | ret = 1; \ |
| 178 | } \ |
| 179 | ret; \ |
| 180 | }) |
| 181 | |
| 182 | /* If we are the last function catching Lua errors, we |
| 183 | * must reset the panic function. |
| 184 | */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 185 | #define RESET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 186 | do { \ |
| 187 | lua_atpanic(__L, hlua_panic_safe); \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 188 | if ((__HLUA)->state_id == 0) \ |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 189 | lua_drop_global_lock(); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 190 | } while(0) |
| 191 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 192 | #define SET_SAFE_LJMP(__HLUA) \ |
| 193 | SET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 194 | |
| 195 | #define RESET_SAFE_LJMP(__HLUA) \ |
| 196 | RESET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 197 | |
| 198 | #define SET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 199 | SET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 200 | |
| 201 | #define RESET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 202 | RESET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 203 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 204 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 205 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 206 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 207 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 208 | /* unused: 0x08, 0x10 */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 209 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 210 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 211 | |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 212 | /* The main Lua execution context. The 0 index is the |
| 213 | * common state shared by all threads. |
| 214 | */ |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 215 | static lua_State *hlua_states[MAX_THREADS + 1]; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 216 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 217 | #define HLUA_FLT_CB_FINAL 0x00000001 |
| 218 | #define HLUA_FLT_CB_RETVAL 0x00000002 |
| 219 | #define HLUA_FLT_CB_ARG_CHN 0x00000004 |
| 220 | #define HLUA_FLT_CB_ARG_HTTP_MSG 0x00000008 |
| 221 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 222 | #define HLUA_FLT_CTX_FL_PAYLOAD 0x00000001 |
| 223 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 224 | struct hlua_reg_filter { |
| 225 | char *name; |
| 226 | int flt_ref[MAX_THREADS + 1]; |
| 227 | int fun_ref[MAX_THREADS + 1]; |
| 228 | struct list l; |
| 229 | }; |
| 230 | |
| 231 | struct hlua_flt_config { |
| 232 | struct hlua_reg_filter *reg; |
| 233 | int ref[MAX_THREADS + 1]; |
| 234 | char **args; |
| 235 | }; |
| 236 | |
| 237 | struct hlua_flt_ctx { |
| 238 | int ref; /* ref to the filter lua object */ |
| 239 | struct hlua *hlua[2]; /* lua runtime context (0: request, 1: response) */ |
| 240 | unsigned int cur_off[2]; /* current offset (0: request, 1: response) */ |
| 241 | unsigned int cur_len[2]; /* current forwardable length (0: request, 1: response) */ |
| 242 | unsigned int flags; /* HLUA_FLT_CTX_FL_* */ |
| 243 | }; |
| 244 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 245 | /* appctx context used by the cosockets */ |
| 246 | struct hlua_csk_ctx { |
| 247 | int connected; |
| 248 | struct xref xref; /* cross reference with the Lua object owner. */ |
| 249 | struct list wake_on_read; |
| 250 | struct list wake_on_write; |
| 251 | struct appctx *appctx; |
| 252 | int die; |
| 253 | }; |
| 254 | |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 255 | /* appctx context used by TCP services */ |
| 256 | struct hlua_tcp_ctx { |
| 257 | struct hlua *hlua; |
| 258 | int flags; |
| 259 | struct task *task; |
| 260 | }; |
| 261 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 262 | /* appctx context used by HTTP services */ |
| 263 | struct hlua_http_ctx { |
| 264 | struct hlua *hlua; |
| 265 | int left_bytes; /* The max amount of bytes that we can read. */ |
| 266 | int flags; |
| 267 | int status; |
| 268 | const char *reason; |
| 269 | struct task *task; |
| 270 | }; |
| 271 | |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 272 | /* used by registered CLI keywords */ |
| 273 | struct hlua_cli_ctx { |
| 274 | struct hlua *hlua; |
| 275 | struct task *task; |
| 276 | struct hlua_function *fcn; |
| 277 | }; |
| 278 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 279 | DECLARE_STATIC_POOL(pool_head_hlua_flt_ctx, "hlua_flt_ctx", sizeof(struct hlua_flt_ctx)); |
| 280 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 281 | static int hlua_filter_from_payload(struct filter *filter); |
| 282 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 283 | /* This is the chained list of struct hlua_flt referenced |
| 284 | * for haproxy filters. It is used for a post-initialisation control. |
| 285 | */ |
| 286 | static struct list referenced_filters = LIST_HEAD_INIT(referenced_filters); |
| 287 | |
| 288 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 289 | /* This is the memory pool containing struct lua for applets |
| 290 | * (including cli). |
| 291 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 292 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 293 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 294 | /* Used for Socket connection. */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 295 | static struct proxy *socket_proxy; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 296 | static struct server *socket_tcp; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 297 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 298 | static struct server *socket_ssl; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 299 | #endif |
| 300 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 301 | /* List head of the function called at the initialisation time. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 302 | struct list hlua_init_functions[MAX_THREADS + 1]; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 303 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 304 | /* The following variables contains the reference of the different |
| 305 | * Lua classes. These references are useful for identify metadata |
| 306 | * associated with an object. |
| 307 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 308 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 309 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 310 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 311 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 312 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 313 | static int class_http_ref; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 314 | static int class_http_msg_ref; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 315 | static int class_httpclient_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 316 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 317 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 318 | static int class_applet_http_ref; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 319 | static int class_txn_reply_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 320 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 321 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 322 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 323 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 324 | * because a task may remain alive during all the haproxy execution. |
| 325 | */ |
| 326 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 327 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 328 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 329 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 330 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 331 | * it is used for preventing infinite loops. |
| 332 | * |
| 333 | * I test the scheer with an infinite loop containing one incrementation |
| 334 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 335 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 336 | * to one interrupt each 10 000 instructions. |
| 337 | * |
| 338 | * configured | Number of |
| 339 | * instructions | loops executed |
| 340 | * between two | in milions |
| 341 | * forced yields | |
| 342 | * ---------------+--------------- |
| 343 | * 10 | 160 |
| 344 | * 500 | 670 |
| 345 | * 1000 | 680 |
| 346 | * 5000 | 700 |
| 347 | * 7000 | 700 |
| 348 | * 8000 | 700 |
| 349 | * 9000 | 710 <- ceil |
| 350 | * 10000 | 710 |
| 351 | * 100000 | 710 |
| 352 | * 1000000 | 710 |
| 353 | * |
| 354 | */ |
| 355 | static unsigned int hlua_nb_instruction = 10000; |
| 356 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 357 | /* Descriptor for the memory allocation state. The limit is pre-initialised to |
| 358 | * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it |
| 359 | * is replaced with ~0 during post_init after everything was loaded. This way |
| 360 | * it is guaranteed that if limit is ~0 the boot is complete and that if it's |
| 361 | * zero it's not yet limited and proper accounting is required. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 362 | */ |
| 363 | struct hlua_mem_allocator { |
| 364 | size_t allocated; |
| 365 | size_t limit; |
| 366 | }; |
| 367 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 368 | static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 369 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 370 | /* These functions converts types between HAProxy internal args or |
| 371 | * sample and LUA types. Another function permits to check if the |
| 372 | * LUA stack contains arguments according with an required ARG_T |
| 373 | * format. |
| 374 | */ |
| 375 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 376 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 377 | __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] | 378 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 379 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 380 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 381 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 382 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 383 | __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] | 384 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 385 | struct prepend_path { |
| 386 | struct list l; |
| 387 | char *type; |
| 388 | char *path; |
| 389 | }; |
| 390 | |
| 391 | static struct list prepend_path_list = LIST_HEAD_INIT(prepend_path_list); |
| 392 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 393 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 394 | do { \ |
| 395 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 396 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 397 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 398 | } while (0) |
| 399 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 400 | static inline struct hlua_function *new_hlua_function() |
| 401 | { |
| 402 | struct hlua_function *fcn; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 403 | int i; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 404 | |
| 405 | fcn = calloc(1, sizeof(*fcn)); |
| 406 | if (!fcn) |
| 407 | return NULL; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 408 | LIST_APPEND(&referenced_functions, &fcn->l); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 409 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 410 | fcn->function_ref[i] = -1; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 411 | return fcn; |
| 412 | } |
| 413 | |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 414 | static inline void release_hlua_function(struct hlua_function *fcn) |
| 415 | { |
| 416 | if (!fcn) |
| 417 | return; |
| 418 | if (fcn->name) |
| 419 | ha_free(&fcn->name); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 420 | LIST_DELETE(&fcn->l); |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 421 | ha_free(&fcn); |
| 422 | } |
| 423 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 424 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 425 | static inline int fcn_ref_to_stack_id(struct hlua_function *fcn) |
| 426 | { |
| 427 | if (fcn->function_ref[0] == -1) |
| 428 | return tid + 1; |
| 429 | return 0; |
| 430 | } |
| 431 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 432 | /* Create a new registered filter. Only its name is filled */ |
| 433 | static inline struct hlua_reg_filter *new_hlua_reg_filter(const char *name) |
| 434 | { |
| 435 | struct hlua_reg_filter *reg_flt; |
| 436 | int i; |
| 437 | |
| 438 | reg_flt = calloc(1, sizeof(*reg_flt)); |
| 439 | if (!reg_flt) |
| 440 | return NULL; |
| 441 | reg_flt->name = strdup(name); |
| 442 | if (!reg_flt->name) { |
| 443 | free(reg_flt); |
| 444 | return NULL; |
| 445 | } |
| 446 | LIST_APPEND(&referenced_filters, ®_flt->l); |
| 447 | for (i = 0; i < MAX_THREADS + 1; i++) { |
| 448 | reg_flt->flt_ref[i] = -1; |
| 449 | reg_flt->fun_ref[i] = -1; |
| 450 | } |
| 451 | return reg_flt; |
| 452 | } |
| 453 | |
| 454 | /* Release a registered filter */ |
| 455 | static inline void release_hlua_reg_filter(struct hlua_reg_filter *reg_flt) |
| 456 | { |
| 457 | if (!reg_flt) |
| 458 | return; |
| 459 | if (reg_flt->name) |
| 460 | ha_free(®_flt->name); |
| 461 | LIST_DELETE(®_flt->l); |
| 462 | ha_free(®_flt); |
| 463 | } |
| 464 | |
| 465 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 466 | static inline int reg_flt_to_stack_id(struct hlua_reg_filter *reg_flt) |
| 467 | { |
| 468 | if (reg_flt->fun_ref[0] == -1) |
| 469 | return tid + 1; |
| 470 | return 0; |
| 471 | } |
| 472 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 473 | /* Used to check an Lua function type in the stack. It creates and |
| 474 | * returns a reference of the function. This function throws an |
| 475 | * error if the rgument is not a "function". |
| 476 | */ |
| 477 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 478 | { |
| 479 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 480 | 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] | 481 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 482 | } |
| 483 | lua_pushvalue(L, argno); |
| 484 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 485 | } |
| 486 | |
Christopher Faulet | ba9e21d | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 487 | /* Used to check an Lua table type in the stack. It creates and |
| 488 | * returns a reference of the table. This function throws an |
| 489 | * error if the rgument is not a "table". |
| 490 | */ |
| 491 | __LJMP unsigned int hlua_checktable(lua_State *L, int argno) |
| 492 | { |
| 493 | if (!lua_istable(L, argno)) { |
| 494 | const char *msg = lua_pushfstring(L, "table expected, got %s", luaL_typename(L, argno)); |
| 495 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 496 | } |
| 497 | lua_pushvalue(L, argno); |
| 498 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 499 | } |
| 500 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 501 | /* Return the string that is of the top of the stack. */ |
| 502 | const char *hlua_get_top_error_string(lua_State *L) |
| 503 | { |
| 504 | if (lua_gettop(L) < 1) |
| 505 | return "unknown error"; |
| 506 | if (lua_type(L, -1) != LUA_TSTRING) |
| 507 | return "unknown error"; |
| 508 | return lua_tostring(L, -1); |
| 509 | } |
| 510 | |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 511 | __LJMP const char *hlua_traceback(lua_State *L, const char* sep) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 512 | { |
| 513 | lua_Debug ar; |
| 514 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 515 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 516 | |
| 517 | while (lua_getstack(L, level++, &ar)) { |
| 518 | |
| 519 | /* Add separator */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 520 | if (b_data(msg)) |
| 521 | chunk_appendf(msg, "%s", sep); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 522 | |
| 523 | /* Fill fields: |
| 524 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 525 | * 'l': fills in the field currentline; |
| 526 | * 'n': fills in the field name and namewhat; |
| 527 | * 't': fills in the field istailcall; |
| 528 | */ |
| 529 | lua_getinfo(L, "Slnt", &ar); |
| 530 | |
| 531 | /* Append code localisation */ |
| 532 | if (ar.currentline > 0) |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 533 | chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 534 | else |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 535 | chunk_appendf(msg, "%s: ", ar.short_src); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 536 | |
| 537 | /* |
| 538 | * Get function name |
| 539 | * |
| 540 | * if namewhat is no empty, name is defined. |
| 541 | * what contains "Lua" for Lua function, "C" for C function, |
| 542 | * or "main" for main code. |
| 543 | */ |
| 544 | 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] | 545 | chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */ |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 546 | |
| 547 | 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] | 548 | chunk_appendf(msg, "in main chunk"); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 549 | |
| 550 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 551 | chunk_appendf(msg, "in function line %d", ar.linedefined); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 552 | |
| 553 | else /* nothing left... */ |
| 554 | chunk_appendf(msg, "?"); |
| 555 | |
| 556 | |
| 557 | /* Display tailed call */ |
| 558 | if (ar.istailcall) |
| 559 | chunk_appendf(msg, " ..."); |
| 560 | } |
| 561 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 562 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 566 | /* This function check the number of arguments available in the |
| 567 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 568 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 569 | */ |
| 570 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 571 | { |
| 572 | if (lua_gettop(L) == nb) |
| 573 | return; |
| 574 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 575 | } |
| 576 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 577 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 578 | * and the line number where the error is encountered. |
| 579 | */ |
| 580 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 581 | { |
| 582 | va_list argp; |
| 583 | va_start(argp, fmt); |
| 584 | luaL_where(L, 1); |
| 585 | lua_pushvfstring(L, fmt, argp); |
| 586 | va_end(argp); |
| 587 | lua_concat(L, 2); |
| 588 | return 1; |
| 589 | } |
| 590 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 591 | /* This functions is used with sample fetch and converters. It |
| 592 | * converts the HAProxy configuration argument in a lua stack |
| 593 | * values. |
| 594 | * |
| 595 | * It takes an array of "arg", and each entry of the array is |
| 596 | * converted and pushed in the LUA stack. |
| 597 | */ |
| 598 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 599 | { |
| 600 | switch (arg->type) { |
| 601 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 602 | case ARGT_TIME: |
| 603 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 604 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 605 | break; |
| 606 | |
| 607 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 608 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 609 | break; |
| 610 | |
| 611 | case ARGT_IPV4: |
| 612 | case ARGT_IPV6: |
| 613 | case ARGT_MSK4: |
| 614 | case ARGT_MSK6: |
| 615 | case ARGT_FE: |
| 616 | case ARGT_BE: |
| 617 | case ARGT_TAB: |
| 618 | case ARGT_SRV: |
| 619 | case ARGT_USR: |
| 620 | case ARGT_MAP: |
| 621 | default: |
| 622 | lua_pushnil(L); |
| 623 | break; |
| 624 | } |
| 625 | return 1; |
| 626 | } |
| 627 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 628 | /* 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] | 629 | * 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] | 630 | * with sample fetch wrappers. The input arguments are given to the |
| 631 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 632 | */ |
| 633 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 634 | { |
| 635 | switch (lua_type(L, ud)) { |
| 636 | |
| 637 | case LUA_TNUMBER: |
| 638 | case LUA_TBOOLEAN: |
| 639 | arg->type = ARGT_SINT; |
| 640 | arg->data.sint = lua_tointeger(L, ud); |
| 641 | break; |
| 642 | |
| 643 | case LUA_TSTRING: |
| 644 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 645 | 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] | 646 | /* 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] | 647 | 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] | 648 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 649 | break; |
| 650 | |
| 651 | case LUA_TUSERDATA: |
| 652 | case LUA_TNIL: |
| 653 | case LUA_TTABLE: |
| 654 | case LUA_TFUNCTION: |
| 655 | case LUA_TTHREAD: |
| 656 | case LUA_TLIGHTUSERDATA: |
| 657 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 658 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 659 | break; |
| 660 | } |
| 661 | return 1; |
| 662 | } |
| 663 | |
| 664 | /* the following functions are used to convert a struct sample |
| 665 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 666 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 667 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 668 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 669 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 670 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 671 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 672 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 673 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 674 | break; |
| 675 | |
| 676 | case SMP_T_BIN: |
| 677 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 678 | 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] | 679 | break; |
| 680 | |
| 681 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 682 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 683 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 684 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 685 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 686 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 687 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 688 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 689 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 690 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 691 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 692 | 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] | 693 | break; |
| 694 | default: |
| 695 | lua_pushnil(L); |
| 696 | break; |
| 697 | } |
| 698 | break; |
| 699 | |
| 700 | case SMP_T_IPV4: |
| 701 | case SMP_T_IPV6: |
| 702 | 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] | 703 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 704 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 705 | 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] | 706 | else |
| 707 | lua_pushnil(L); |
| 708 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 709 | default: |
| 710 | lua_pushnil(L); |
| 711 | break; |
| 712 | } |
| 713 | return 1; |
| 714 | } |
| 715 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 716 | /* the following functions are used to convert a struct sample |
| 717 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 718 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 719 | */ |
| 720 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 721 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 722 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 723 | |
| 724 | case SMP_T_BIN: |
| 725 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 726 | 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] | 727 | break; |
| 728 | |
| 729 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 730 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 731 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 732 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 733 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 734 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 735 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 736 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 737 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 738 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 739 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 740 | 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] | 741 | break; |
| 742 | default: |
| 743 | lua_pushstring(L, ""); |
| 744 | break; |
| 745 | } |
| 746 | break; |
| 747 | |
| 748 | case SMP_T_SINT: |
| 749 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 750 | case SMP_T_IPV4: |
| 751 | case SMP_T_IPV6: |
| 752 | 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] | 753 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 754 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 755 | 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] | 756 | else |
| 757 | lua_pushstring(L, ""); |
| 758 | break; |
| 759 | default: |
| 760 | lua_pushstring(L, ""); |
| 761 | break; |
| 762 | } |
| 763 | return 1; |
| 764 | } |
| 765 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 766 | /* the following functions are used to convert an Lua type in a |
| 767 | * struct sample. This is useful to provide data from a converter |
| 768 | * to the LUA code. |
| 769 | */ |
| 770 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 771 | { |
| 772 | switch (lua_type(L, ud)) { |
| 773 | |
| 774 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 775 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 776 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 777 | break; |
| 778 | |
| 779 | |
| 780 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 781 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 782 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 783 | break; |
| 784 | |
| 785 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 786 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 787 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 788 | 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] | 789 | /* 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] | 790 | 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] | 791 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 792 | break; |
| 793 | |
| 794 | case LUA_TUSERDATA: |
| 795 | case LUA_TNIL: |
| 796 | case LUA_TTABLE: |
| 797 | case LUA_TFUNCTION: |
| 798 | case LUA_TTHREAD: |
| 799 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 800 | case LUA_TNONE: |
| 801 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 802 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 803 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 804 | break; |
| 805 | } |
| 806 | return 1; |
| 807 | } |
| 808 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 809 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 810 | * 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] | 811 | * 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] | 812 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 813 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 814 | * 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] | 815 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 816 | __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] | 817 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 818 | { |
| 819 | int min_arg; |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 820 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 821 | struct proxy *px; |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 822 | struct userlist *ul; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 823 | struct my_regex *reg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 824 | const char *msg = NULL; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 825 | char *sname, *pname, *err = NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 826 | |
| 827 | idx = 0; |
| 828 | min_arg = ARGM(mask); |
| 829 | mask >>= ARGM_BITS; |
| 830 | |
| 831 | while (1) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 832 | struct buffer tmp = BUF_NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 833 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 834 | /* Check for mandatory arguments. */ |
| 835 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 836 | if (idx < min_arg) { |
| 837 | |
| 838 | /* If miss other argument than the first one, we return an error. */ |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 839 | if (idx > 0) { |
| 840 | msg = "Mandatory argument expected"; |
| 841 | goto error; |
| 842 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 843 | |
| 844 | /* If first argument have a certain type, some default values |
| 845 | * may be used. See the function smp_resolve_args(). |
| 846 | */ |
| 847 | switch (mask & ARGT_MASK) { |
| 848 | |
| 849 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 850 | if (!(p->cap & PR_CAP_FE)) { |
| 851 | msg = "Mandatory argument expected"; |
| 852 | goto error; |
| 853 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 854 | argp[idx].data.prx = p; |
| 855 | argp[idx].type = ARGT_FE; |
| 856 | argp[idx+1].type = ARGT_STOP; |
| 857 | break; |
| 858 | |
| 859 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 860 | if (!(p->cap & PR_CAP_BE)) { |
| 861 | msg = "Mandatory argument expected"; |
| 862 | goto error; |
| 863 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 864 | argp[idx].data.prx = p; |
| 865 | argp[idx].type = ARGT_BE; |
| 866 | argp[idx+1].type = ARGT_STOP; |
| 867 | break; |
| 868 | |
| 869 | case ARGT_TAB: |
| 870 | argp[idx].data.prx = p; |
| 871 | argp[idx].type = ARGT_TAB; |
| 872 | argp[idx+1].type = ARGT_STOP; |
| 873 | break; |
| 874 | |
| 875 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 876 | msg = "Mandatory argument expected"; |
| 877 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 878 | break; |
| 879 | } |
| 880 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 881 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 882 | } |
| 883 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 884 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 885 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 886 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 887 | msg = "Last argument expected"; |
| 888 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 892 | argp[idx].type == ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 893 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 894 | } |
| 895 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 896 | /* Convert some argument types. All string in argp[] are for not |
| 897 | * duplicated yet. |
| 898 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 899 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 900 | case ARGT_SINT: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 901 | if (argp[idx].type != ARGT_SINT) { |
| 902 | msg = "integer expected"; |
| 903 | goto error; |
| 904 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 905 | argp[idx].type = ARGT_SINT; |
| 906 | break; |
| 907 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 908 | case ARGT_TIME: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 909 | if (argp[idx].type != ARGT_SINT) { |
| 910 | msg = "integer expected"; |
| 911 | goto error; |
| 912 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 913 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 914 | break; |
| 915 | |
| 916 | case ARGT_SIZE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 917 | if (argp[idx].type != ARGT_SINT) { |
| 918 | msg = "integer expected"; |
| 919 | goto error; |
| 920 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 921 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 922 | break; |
| 923 | |
| 924 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 925 | if (argp[idx].type != ARGT_STR) { |
| 926 | msg = "string expected"; |
| 927 | goto error; |
| 928 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 929 | 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] | 930 | if (!argp[idx].data.prx) { |
| 931 | msg = "frontend doesn't exist"; |
| 932 | goto error; |
| 933 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 934 | argp[idx].type = ARGT_FE; |
| 935 | break; |
| 936 | |
| 937 | case ARGT_BE: |
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 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 942 | 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] | 943 | if (!argp[idx].data.prx) { |
| 944 | msg = "backend doesn't exist"; |
| 945 | goto error; |
| 946 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 947 | argp[idx].type = ARGT_BE; |
| 948 | break; |
| 949 | |
| 950 | case ARGT_TAB: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 951 | if (argp[idx].type != ARGT_STR) { |
| 952 | msg = "string expected"; |
| 953 | goto error; |
| 954 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 955 | 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] | 956 | if (!argp[idx].data.t) { |
| 957 | msg = "table doesn't exist"; |
| 958 | goto error; |
| 959 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 960 | argp[idx].type = ARGT_TAB; |
| 961 | break; |
| 962 | |
| 963 | case ARGT_SRV: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 964 | if (argp[idx].type != ARGT_STR) { |
| 965 | msg = "string expected"; |
| 966 | goto error; |
| 967 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 968 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 969 | if (sname) { |
| 970 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 971 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 972 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 973 | if (!px) { |
| 974 | msg = "backend doesn't exist"; |
| 975 | goto error; |
| 976 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 977 | } |
| 978 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 979 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 980 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 981 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 982 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 983 | if (!argp[idx].data.srv) { |
| 984 | msg = "server doesn't exist"; |
| 985 | goto error; |
| 986 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 987 | argp[idx].type = ARGT_SRV; |
| 988 | break; |
| 989 | |
| 990 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 991 | if (argp[idx].type != ARGT_STR) { |
| 992 | msg = "string expected"; |
| 993 | goto error; |
| 994 | } |
| 995 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 996 | msg = "invalid IPv4 address"; |
| 997 | goto error; |
| 998 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 999 | argp[idx].type = ARGT_IPV4; |
| 1000 | break; |
| 1001 | |
| 1002 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1003 | if (argp[idx].type == ARGT_SINT) |
| 1004 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 1005 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1006 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 1007 | msg = "invalid IPv4 mask"; |
| 1008 | goto error; |
| 1009 | } |
| 1010 | } |
| 1011 | else { |
| 1012 | msg = "integer or string expected"; |
| 1013 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1014 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1015 | argp[idx].type = ARGT_MSK4; |
| 1016 | break; |
| 1017 | |
| 1018 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1019 | if (argp[idx].type != ARGT_STR) { |
| 1020 | msg = "string expected"; |
| 1021 | goto error; |
| 1022 | } |
| 1023 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 1024 | msg = "invalid IPv6 address"; |
| 1025 | goto error; |
| 1026 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1027 | argp[idx].type = ARGT_IPV6; |
| 1028 | break; |
| 1029 | |
| 1030 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1031 | if (argp[idx].type == ARGT_SINT) |
| 1032 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 1033 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1034 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 1035 | msg = "invalid IPv6 mask"; |
| 1036 | goto error; |
| 1037 | } |
| 1038 | } |
| 1039 | else { |
| 1040 | msg = "integer or string expected"; |
| 1041 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1042 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 1043 | argp[idx].type = ARGT_MSK6; |
| 1044 | break; |
| 1045 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 1046 | case ARGT_REG: |
| 1047 | if (argp[idx].type != ARGT_STR) { |
| 1048 | msg = "string expected"; |
| 1049 | goto error; |
| 1050 | } |
| 1051 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 1052 | if (!reg) { |
| 1053 | msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'", |
| 1054 | argp[idx].data.str.area, err); |
| 1055 | free(err); |
| 1056 | goto error; |
| 1057 | } |
| 1058 | argp[idx].type = ARGT_REG; |
| 1059 | argp[idx].data.reg = reg; |
| 1060 | break; |
| 1061 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1062 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1063 | if (argp[idx].type != ARGT_STR) { |
| 1064 | msg = "string expected"; |
| 1065 | goto error; |
| 1066 | } |
| 1067 | if (p->uri_auth && p->uri_auth->userlist && |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1068 | strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0) |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1069 | ul = p->uri_auth->userlist; |
| 1070 | else |
| 1071 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 1072 | |
| 1073 | if (!ul) { |
| 1074 | msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area); |
| 1075 | goto error; |
| 1076 | } |
| 1077 | argp[idx].type = ARGT_USR; |
| 1078 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1079 | break; |
| 1080 | |
| 1081 | case ARGT_STR: |
| 1082 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 1083 | msg = "unable to duplicate string arg"; |
| 1084 | goto error; |
| 1085 | } |
| 1086 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1087 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1088 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1089 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1090 | msg = "type not yet supported"; |
| 1091 | goto error; |
| 1092 | break; |
| 1093 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | /* Check for type of argument. */ |
| 1097 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1098 | msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 1099 | arg_type_names[(mask & ARGT_MASK)], |
| 1100 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 1101 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | /* Next argument. */ |
| 1105 | mask >>= ARGT_BITS; |
| 1106 | idx++; |
| 1107 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1108 | return 0; |
| 1109 | |
| 1110 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 1111 | free_args(argp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1112 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 1113 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1114 | } |
| 1115 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1116 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1117 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1118 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1119 | * |
| 1120 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1121 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 1122 | */ |
| 1123 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 1124 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1125 | struct hlua **hlua = lua_getextraspace(L); |
| 1126 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1127 | } |
| 1128 | static inline void hlua_sethlua(struct hlua *hlua) |
| 1129 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1130 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 1131 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1132 | } |
| 1133 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1134 | /* This function is used to send logs. It try to send on screen (stderr) |
| 1135 | * and on the default syslog server. |
| 1136 | */ |
| 1137 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 1138 | { |
| 1139 | struct tm tm; |
| 1140 | char *p; |
| 1141 | |
| 1142 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1143 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1144 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1145 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 1146 | /* Break the message if exceed the buffer size. */ |
| 1147 | *(p-4) = ' '; |
| 1148 | *(p-3) = '.'; |
| 1149 | *(p-2) = '.'; |
| 1150 | *(p-1) = '.'; |
| 1151 | break; |
| 1152 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1153 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1154 | *p = *msg; |
| 1155 | else |
| 1156 | *p = '.'; |
| 1157 | } |
| 1158 | *p = '\0'; |
| 1159 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1160 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1161 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 1162 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 1163 | return; |
| 1164 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 1165 | get_localtime(date.tv_sec, &tm); |
| 1166 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1167 | 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] | 1168 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1169 | fflush(stderr); |
| 1170 | } |
| 1171 | } |
| 1172 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1173 | /* This function just ensure that the yield will be always |
| 1174 | * returned with a timeout and permit to set some flags |
| 1175 | */ |
| 1176 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1177 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1178 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1179 | struct hlua *hlua; |
| 1180 | |
| 1181 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1182 | hlua = hlua_gethlua(L); |
| 1183 | if (!hlua) { |
| 1184 | return; |
| 1185 | } |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1186 | |
| 1187 | /* Set the wake timeout. If timeout is required, we set |
| 1188 | * the expiration time. |
| 1189 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1190 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1191 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1192 | hlua->flags |= flags; |
| 1193 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1194 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1195 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1196 | } |
| 1197 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1198 | /* This function initialises the Lua environment stored in the stream. |
| 1199 | * 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] | 1200 | * 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] | 1201 | * |
| 1202 | * This function is particular. it initialises a new Lua thread. If the |
| 1203 | * initialisation fails (example: out of memory error), the lua function |
| 1204 | * throws an error (longjmp). |
| 1205 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1206 | * 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] | 1207 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1208 | * threads appear, the safe environment set a lock to ensure only one |
| 1209 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1210 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1211 | * |
| 1212 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1213 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1214 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1215 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1216 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1217 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1218 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1219 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1220 | 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] | 1221 | { |
| 1222 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1223 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1224 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1225 | lua->wake_time = TICK_ETERNITY; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1226 | lua->state_id = state_id; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1227 | LIST_INIT(&lua->com); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1228 | if (!already_safe) { |
| 1229 | if (!SET_SAFE_LJMP_PARENT(lua)) { |
| 1230 | lua->Tref = LUA_REFNIL; |
| 1231 | return 0; |
| 1232 | } |
| 1233 | } |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1234 | lua->T = lua_newthread(hlua_states[state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1235 | if (!lua->T) { |
| 1236 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1237 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1238 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1239 | return 0; |
| 1240 | } |
| 1241 | hlua_sethlua(lua); |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1242 | lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1243 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1244 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1245 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1246 | return 1; |
| 1247 | } |
| 1248 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1249 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1250 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 1251 | * is not freed. |
| 1252 | */ |
| 1253 | void hlua_ctx_destroy(struct hlua *lua) |
| 1254 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1255 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1256 | return; |
| 1257 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1258 | if (!lua->T) |
| 1259 | goto end; |
| 1260 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1261 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1262 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1263 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1264 | if (!SET_SAFE_LJMP(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1265 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1266 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1267 | RESET_SAFE_LJMP(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1268 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1269 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1270 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1271 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1272 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1273 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1274 | * without error, we run the GC on the thread pointer. Its freed all |
| 1275 | * the unused memory. |
| 1276 | * If the thread is finnish with an error or is currently yielded, |
| 1277 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1278 | * so e run the GC on the main thread. |
| 1279 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1280 | * the garbage collection. |
| 1281 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1282 | if (lua->gc_count) { |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1283 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1284 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1285 | lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1286 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1287 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1288 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1289 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1290 | |
| 1291 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1292 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | /* This function is used to restore the Lua context when a coroutine |
| 1296 | * fails. This function copy the common memory between old coroutine |
| 1297 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1298 | * replaced by the new coroutine. |
| 1299 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1300 | * as string error message and it is copied in the new stack. |
| 1301 | */ |
| 1302 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1303 | { |
| 1304 | lua_State *T; |
| 1305 | int new_ref; |
| 1306 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1307 | /* New Lua coroutine. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1308 | T = lua_newthread(hlua_states[lua->state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1309 | if (!T) |
| 1310 | return 0; |
| 1311 | |
| 1312 | /* Copy last error message. */ |
| 1313 | if (keep_msg) |
| 1314 | lua_xmove(lua->T, T, 1); |
| 1315 | |
| 1316 | /* Copy data between the coroutines. */ |
| 1317 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1318 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1319 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1320 | |
| 1321 | /* Destroy old data. */ |
| 1322 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1323 | |
| 1324 | /* The thread is garbage collected by Lua. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1325 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1326 | |
| 1327 | /* Fill the struct with the new coroutine values. */ |
| 1328 | lua->Mref = new_ref; |
| 1329 | lua->T = T; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1330 | lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1331 | |
| 1332 | /* Set context. */ |
| 1333 | hlua_sethlua(lua); |
| 1334 | |
| 1335 | return 1; |
| 1336 | } |
| 1337 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1338 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1339 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1340 | struct hlua *hlua; |
| 1341 | |
| 1342 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1343 | hlua = hlua_gethlua(L); |
| 1344 | if (!hlua) |
| 1345 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1346 | |
| 1347 | /* Lua cannot yield when its returning from a function, |
| 1348 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1349 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1350 | */ |
| 1351 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1352 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1353 | return; |
| 1354 | } |
| 1355 | |
| 1356 | /* restore the interrupt condition. */ |
| 1357 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1358 | |
| 1359 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1360 | * If the state is not yieldable, trying yield causes an error. |
| 1361 | */ |
| 1362 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1363 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1364 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1365 | /* If we cannot yield, update the clock and check the timeout. */ |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 1366 | clock_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1367 | hlua->run_time += now_ms - hlua->start_time; |
| 1368 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1369 | lua_pushfstring(L, "execution timeout"); |
| 1370 | WILL_LJMP(lua_error(L)); |
| 1371 | } |
| 1372 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1373 | /* Update the start time. */ |
| 1374 | hlua->start_time = now_ms; |
| 1375 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1376 | /* Try to interrupt the process at the end of the current |
| 1377 | * unyieldable function. |
| 1378 | */ |
| 1379 | 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] | 1380 | } |
| 1381 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1382 | /* This function start or resumes the Lua stack execution. If the flag |
| 1383 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1384 | * The function return an error. |
| 1385 | * |
| 1386 | * The function can returns 4 values: |
| 1387 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1388 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1389 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1390 | * - 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] | 1391 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1392 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1393 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1394 | * 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] | 1395 | * LUA code. |
| 1396 | */ |
| 1397 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1398 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1399 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1400 | int nres; |
| 1401 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1402 | int ret; |
| 1403 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1404 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1405 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1406 | /* Initialise run time counter. */ |
| 1407 | if (!HLUA_IS_RUNNING(lua)) |
| 1408 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1409 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1410 | /* Lock the whole Lua execution. This lock must be before the |
| 1411 | * label "resume_execution". |
| 1412 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1413 | if (lua->state_id == 0) |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 1414 | lua_take_global_lock(); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1415 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1416 | resume_execution: |
| 1417 | |
| 1418 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1419 | * instructions. it is used for preventing infinite loops. |
| 1420 | */ |
| 1421 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1422 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1423 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1424 | HLUA_SET_RUN(lua); |
| 1425 | HLUA_CLR_CTRLYIELD(lua); |
| 1426 | HLUA_CLR_WAKERESWR(lua); |
| 1427 | HLUA_CLR_WAKEREQWR(lua); |
Christopher Faulet | 1f43a34 | 2021-08-04 17:58:21 +0200 | [diff] [blame] | 1428 | HLUA_CLR_NOYIELD(lua); |
| 1429 | if (!yield_allowed) |
| 1430 | HLUA_SET_NOYIELD(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1431 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1432 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1433 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1434 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1435 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1436 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1437 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1438 | 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] | 1439 | #else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1440 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1441 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1442 | switch (ret) { |
| 1443 | |
| 1444 | case LUA_OK: |
| 1445 | ret = HLUA_E_OK; |
| 1446 | break; |
| 1447 | |
| 1448 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1449 | /* Check if the execution timeout is expired. It it is the case, we |
| 1450 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1451 | */ |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 1452 | clock_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1453 | lua->run_time += now_ms - lua->start_time; |
| 1454 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16: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_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1457 | break; |
| 1458 | } |
| 1459 | /* Process the forced yield. if the general yield is not allowed or |
| 1460 | * if no task were associated this the current Lua execution |
| 1461 | * coroutine, we resume the execution. Else we want to return in the |
| 1462 | * scheduler and we want to be waked up again, to continue the |
| 1463 | * current Lua execution. So we schedule our own task. |
| 1464 | */ |
| 1465 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1466 | if (!yield_allowed || !lua->task) |
| 1467 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1468 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1469 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1470 | if (!yield_allowed) { |
| 1471 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1472 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1473 | break; |
| 1474 | } |
| 1475 | ret = HLUA_E_AGAIN; |
| 1476 | break; |
| 1477 | |
| 1478 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1479 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1480 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1481 | * because the errors ares the only one mean to return immediately |
| 1482 | * from and lua execution. |
| 1483 | */ |
| 1484 | if (lua->flags & HLUA_EXIT) { |
| 1485 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1486 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1487 | break; |
| 1488 | } |
| 1489 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1490 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1491 | if (!lua_checkstack(lua->T, 1)) { |
| 1492 | ret = HLUA_E_ERR; |
| 1493 | break; |
| 1494 | } |
| 1495 | msg = lua_tostring(lua->T, -1); |
| 1496 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1497 | lua_pop(lua->T, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 1498 | trace = hlua_traceback(lua->T, ", "); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1499 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1500 | 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] | 1501 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1502 | 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] | 1503 | ret = HLUA_E_ERRMSG; |
| 1504 | break; |
| 1505 | |
| 1506 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1507 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1508 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1509 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1510 | break; |
| 1511 | |
| 1512 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1513 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1514 | if (!lua_checkstack(lua->T, 1)) { |
| 1515 | ret = HLUA_E_ERR; |
| 1516 | break; |
| 1517 | } |
| 1518 | msg = lua_tostring(lua->T, -1); |
| 1519 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1520 | lua_pop(lua->T, 1); |
| 1521 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1522 | 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] | 1523 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1524 | 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] | 1525 | ret = HLUA_E_ERRMSG; |
| 1526 | break; |
| 1527 | |
| 1528 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1529 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1530 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1531 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1532 | break; |
| 1533 | } |
| 1534 | |
| 1535 | switch (ret) { |
| 1536 | case HLUA_E_AGAIN: |
| 1537 | break; |
| 1538 | |
| 1539 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1540 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1541 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1542 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1543 | break; |
| 1544 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1545 | case HLUA_E_ETMOUT: |
| 1546 | case HLUA_E_NOMEM: |
| 1547 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1548 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1549 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1550 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1551 | hlua_ctx_renew(lua, 0); |
| 1552 | break; |
| 1553 | |
| 1554 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1555 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1556 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1557 | break; |
| 1558 | } |
| 1559 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1560 | /* This is the main exit point, remove the Lua lock. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1561 | if (lua->state_id == 0) |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 1562 | lua_drop_global_lock(); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1563 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1564 | return ret; |
| 1565 | } |
| 1566 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1567 | /* This function exit the current code. */ |
| 1568 | __LJMP static int hlua_done(lua_State *L) |
| 1569 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1570 | struct hlua *hlua; |
| 1571 | |
| 1572 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1573 | hlua = hlua_gethlua(L); |
| 1574 | if (!hlua) |
| 1575 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1576 | |
| 1577 | hlua->flags |= HLUA_EXIT; |
| 1578 | WILL_LJMP(lua_error(L)); |
| 1579 | |
| 1580 | return 0; |
| 1581 | } |
| 1582 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1583 | /* This function is an LUA binding. It provides a function |
| 1584 | * for deleting ACL from a referenced ACL file. |
| 1585 | */ |
| 1586 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1587 | { |
| 1588 | const char *name; |
| 1589 | const char *key; |
| 1590 | struct pat_ref *ref; |
| 1591 | |
| 1592 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1593 | |
| 1594 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1595 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1596 | |
| 1597 | ref = pat_ref_lookup(name); |
| 1598 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1599 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1600 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1601 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1602 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1603 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1604 | return 0; |
| 1605 | } |
| 1606 | |
| 1607 | /* This function is an LUA binding. It provides a function |
| 1608 | * for deleting map entry from a referenced map file. |
| 1609 | */ |
| 1610 | static int hlua_del_map(lua_State *L) |
| 1611 | { |
| 1612 | const char *name; |
| 1613 | const char *key; |
| 1614 | struct pat_ref *ref; |
| 1615 | |
| 1616 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1617 | |
| 1618 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1619 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1620 | |
| 1621 | ref = pat_ref_lookup(name); |
| 1622 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1623 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1624 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1625 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1626 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1627 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1628 | return 0; |
| 1629 | } |
| 1630 | |
| 1631 | /* This function is an LUA binding. It provides a function |
| 1632 | * for adding ACL pattern from a referenced ACL file. |
| 1633 | */ |
| 1634 | static int hlua_add_acl(lua_State *L) |
| 1635 | { |
| 1636 | const char *name; |
| 1637 | const char *key; |
| 1638 | struct pat_ref *ref; |
| 1639 | |
| 1640 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1641 | |
| 1642 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1643 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1644 | |
| 1645 | ref = pat_ref_lookup(name); |
| 1646 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1647 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1648 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1649 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1650 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1651 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1652 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1653 | return 0; |
| 1654 | } |
| 1655 | |
| 1656 | /* This function is an LUA binding. It provides a function |
| 1657 | * for setting map pattern and sample from a referenced map |
| 1658 | * file. |
| 1659 | */ |
| 1660 | static int hlua_set_map(lua_State *L) |
| 1661 | { |
| 1662 | const char *name; |
| 1663 | const char *key; |
| 1664 | const char *value; |
| 1665 | struct pat_ref *ref; |
| 1666 | |
| 1667 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1668 | |
| 1669 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1670 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1671 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1672 | |
| 1673 | ref = pat_ref_lookup(name); |
| 1674 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1675 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1676 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1677 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1678 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1679 | pat_ref_set(ref, key, value, NULL); |
| 1680 | else |
| 1681 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1682 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1683 | return 0; |
| 1684 | } |
| 1685 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1686 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1687 | * 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] | 1688 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1689 | * the name of the object (_G[<name>] = <metable> ). |
| 1690 | * |
| 1691 | * A metable is a table that modify the standard behavior of a standard |
| 1692 | * access to the associated data. The entries of this new metatable are |
| 1693 | * defined as is: |
| 1694 | * |
| 1695 | * http://lua-users.org/wiki/MetatableEvents |
| 1696 | * |
| 1697 | * __index |
| 1698 | * |
| 1699 | * we access an absent field in a table, the result is nil. This is |
| 1700 | * true, but it is not the whole truth. Actually, such access triggers |
| 1701 | * the interpreter to look for an __index metamethod: If there is no |
| 1702 | * such method, as usually happens, then the access results in nil; |
| 1703 | * otherwise, the metamethod will provide the result. |
| 1704 | * |
| 1705 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1706 | * the key does not appear in the table, but the metatable has an __index |
| 1707 | * property: |
| 1708 | * |
| 1709 | * - if the value is a function, the function is called, passing in the |
| 1710 | * table and the key; the return value of that function is returned as |
| 1711 | * the result. |
| 1712 | * |
| 1713 | * - if the value is another table, the value of the key in that table is |
| 1714 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1715 | * table's metatable has an __index property, then it continues on up) |
| 1716 | * |
| 1717 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1718 | * |
| 1719 | * http://www.lua.org/pil/13.4.1.html |
| 1720 | * |
| 1721 | * __newindex |
| 1722 | * |
| 1723 | * Like __index, but control property assignment. |
| 1724 | * |
| 1725 | * __mode - Control weak references. A string value with one or both |
| 1726 | * of the characters 'k' and 'v' which specifies that the the |
| 1727 | * keys and/or values in the table are weak references. |
| 1728 | * |
| 1729 | * __call - Treat a table like a function. When a table is followed by |
| 1730 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1731 | * a __call key pointing to a function, that function is invoked |
| 1732 | * (passing any specified arguments) and the return value is |
| 1733 | * returned. |
| 1734 | * |
| 1735 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1736 | * called, if the metatable for myTable has a __metatable |
| 1737 | * key, the value of that key is returned instead of the |
| 1738 | * actual metatable. |
| 1739 | * |
| 1740 | * __tostring - Control string representation. When the builtin |
| 1741 | * "tostring( myTable )" function is called, if the metatable |
| 1742 | * for myTable has a __tostring property set to a function, |
| 1743 | * that function is invoked (passing myTable to it) and the |
| 1744 | * return value is used as the string representation. |
| 1745 | * |
| 1746 | * __len - Control table length. When the table length is requested using |
| 1747 | * the length operator ( '#' ), if the metatable for myTable has |
| 1748 | * a __len key pointing to a function, that function is invoked |
| 1749 | * (passing myTable to it) and the return value used as the value |
| 1750 | * of "#myTable". |
| 1751 | * |
| 1752 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1753 | * collected, if the metatable has a __gc field pointing to a |
| 1754 | * function, that function is first invoked, passing the userdata |
| 1755 | * to it. The __gc metamethod is not called for tables. |
| 1756 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1757 | * |
| 1758 | * Special metamethods for redefining standard operators: |
| 1759 | * http://www.lua.org/pil/13.1.html |
| 1760 | * |
| 1761 | * __add "+" |
| 1762 | * __sub "-" |
| 1763 | * __mul "*" |
| 1764 | * __div "/" |
| 1765 | * __unm "!" |
| 1766 | * __pow "^" |
| 1767 | * __concat ".." |
| 1768 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1769 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1770 | * http://www.lua.org/pil/13.2.html |
| 1771 | * |
| 1772 | * __eq "==" |
| 1773 | * __lt "<" |
| 1774 | * __le "<=" |
| 1775 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1776 | |
| 1777 | /* |
| 1778 | * |
| 1779 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1780 | * Class Map |
| 1781 | * |
| 1782 | * |
| 1783 | */ |
| 1784 | |
| 1785 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1786 | * a class session, otherwise it throws an error. |
| 1787 | */ |
| 1788 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1789 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1790 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1791 | } |
| 1792 | |
| 1793 | /* This function is the map constructor. It don't need |
| 1794 | * the class Map object. It creates and return a new Map |
| 1795 | * object. It must be called only during "body" or "init" |
| 1796 | * context because it process some filesystem accesses. |
| 1797 | */ |
| 1798 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1799 | { |
| 1800 | const char *fn; |
| 1801 | int match = PAT_MATCH_STR; |
| 1802 | struct sample_conv conv; |
| 1803 | const char *file = ""; |
| 1804 | int line = 0; |
| 1805 | lua_Debug ar; |
| 1806 | char *err = NULL; |
| 1807 | struct arg args[2]; |
| 1808 | |
| 1809 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1810 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1811 | |
| 1812 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1813 | |
| 1814 | if (lua_gettop(L) >= 2) { |
| 1815 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1816 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1817 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1818 | } |
| 1819 | |
| 1820 | /* Get Lua filename and line number. */ |
| 1821 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1822 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1823 | if (ar.currentline > 0) { /* is there info? */ |
| 1824 | file = ar.short_src; |
| 1825 | line = ar.currentline; |
| 1826 | } |
| 1827 | } |
| 1828 | |
| 1829 | /* fill fake sample_conv struct. */ |
| 1830 | conv.kw = ""; /* unused. */ |
| 1831 | conv.process = NULL; /* unused. */ |
| 1832 | conv.arg_mask = 0; /* unused. */ |
| 1833 | conv.val_args = NULL; /* unused. */ |
| 1834 | conv.out_type = SMP_T_STR; |
| 1835 | conv.private = (void *)(long)match; |
| 1836 | switch (match) { |
| 1837 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1838 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1839 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1840 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1841 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1842 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1843 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1844 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1845 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1846 | default: |
| 1847 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1848 | } |
| 1849 | |
| 1850 | /* fill fake args. */ |
| 1851 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1852 | args[0].data.str.area = strdup(fn); |
| 1853 | args[0].data.str.data = strlen(fn); |
| 1854 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1855 | args[1].type = ARGT_STOP; |
| 1856 | |
| 1857 | /* load the map. */ |
| 1858 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1859 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1860 | * free the err variable. |
| 1861 | */ |
| 1862 | luaL_where(L, 1); |
| 1863 | lua_pushfstring(L, "'new': %s.", err); |
| 1864 | lua_concat(L, 2); |
| 1865 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1866 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1867 | WILL_LJMP(lua_error(L)); |
| 1868 | } |
| 1869 | |
| 1870 | /* create the lua object. */ |
| 1871 | lua_newtable(L); |
| 1872 | lua_pushlightuserdata(L, args[0].data.map); |
| 1873 | lua_rawseti(L, -2, 0); |
| 1874 | |
| 1875 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1876 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1877 | lua_setmetatable(L, -2); |
| 1878 | |
| 1879 | |
| 1880 | return 1; |
| 1881 | } |
| 1882 | |
| 1883 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1884 | { |
| 1885 | struct map_descriptor *desc; |
| 1886 | struct pattern *pat; |
| 1887 | struct sample smp; |
| 1888 | |
| 1889 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1890 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1891 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1892 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1893 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1894 | } |
| 1895 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1896 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1897 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1898 | 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] | 1899 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1900 | } |
| 1901 | |
| 1902 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1903 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1904 | if (str) |
| 1905 | lua_pushstring(L, ""); |
| 1906 | else |
| 1907 | lua_pushnil(L); |
| 1908 | return 1; |
| 1909 | } |
| 1910 | |
| 1911 | /* 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] | 1912 | 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] | 1913 | return 1; |
| 1914 | } |
| 1915 | |
| 1916 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1917 | { |
| 1918 | return _hlua_map_lookup(L, 0); |
| 1919 | } |
| 1920 | |
| 1921 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1922 | { |
| 1923 | return _hlua_map_lookup(L, 1); |
| 1924 | } |
| 1925 | |
| 1926 | /* |
| 1927 | * |
| 1928 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1929 | * Class Socket |
| 1930 | * |
| 1931 | * |
| 1932 | */ |
| 1933 | |
| 1934 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1935 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1936 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1937 | } |
| 1938 | |
| 1939 | /* 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] | 1940 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1941 | * received. |
| 1942 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1943 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1944 | { |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1945 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame^] | 1946 | struct conn_stream *cs = appctx_cs(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1947 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1948 | if (ctx->die) { |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame] | 1949 | cs_shutw(cs); |
| 1950 | cs_shutr(cs); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1951 | cs_ic(cs)->flags |= CF_READ_NULL; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1952 | notification_wake(&ctx->wake_on_read); |
| 1953 | notification_wake(&ctx->wake_on_write); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1954 | stream_shutdown(__cs_strm(cs), SF_ERR_KILLED); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1955 | } |
| 1956 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1957 | /* If we can't write, wakeup the pending write signals. */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1958 | if (channel_output_closed(cs_ic(cs))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1959 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1960 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1961 | /* If we can't read, wakeup the pending read signals. */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1962 | if (channel_input_closed(cs_oc(cs))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1963 | notification_wake(&ctx->wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1964 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1965 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1966 | * to be notified whenever the connection completes. |
| 1967 | */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1968 | if (cs_opposite(cs)->state < CS_ST_EST) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 1969 | cs_cant_get(cs); |
| 1970 | cs_rx_conn_blk(cs); |
| 1971 | cs_rx_endp_more(cs); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1972 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1973 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1974 | |
| 1975 | /* This function is called after the connect. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1976 | ctx->connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1977 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1978 | /* Wake the tasks which wants to write if the buffer have available space. */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1979 | if (channel_may_recv(cs_ic(cs))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1980 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1981 | |
| 1982 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1983 | if (!channel_is_empty(cs_oc(cs))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1984 | notification_wake(&ctx->wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1985 | |
| 1986 | /* Some data were injected in the buffer, notify the stream |
| 1987 | * interface. |
| 1988 | */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1989 | if (!channel_is_empty(cs_ic(cs))) |
Christopher Faulet | 13045f0 | 2022-04-01 14:23:38 +0200 | [diff] [blame] | 1990 | cs_update(cs); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1991 | |
| 1992 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1993 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1994 | */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1995 | if (notification_registered(&ctx->wake_on_write)) |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 1996 | cs_rx_endp_more(cs); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1997 | } |
| 1998 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1999 | /* This function is called when the "struct stream" is destroyed. |
| 2000 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2001 | * Wake all the pending signals. |
| 2002 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 2003 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2004 | { |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2005 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2006 | struct xref *peer; |
| 2007 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2008 | /* Remove my link in the original object. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2009 | peer = xref_get_peer_and_lock(&ctx->xref); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2010 | if (peer) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2011 | xref_disconnect(&ctx->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2012 | |
| 2013 | /* Wake all the task waiting for me. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2014 | notification_wake(&ctx->wake_on_read); |
| 2015 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2016 | } |
| 2017 | |
| 2018 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2019 | * uses this object. If the stream does not exists, just quit. |
| 2020 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2021 | * pending signal can rest in the read and write lists. destroy |
| 2022 | * it. |
| 2023 | */ |
| 2024 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 2025 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2026 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2027 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2028 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2029 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2030 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 2031 | |
| 2032 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2033 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2034 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2035 | return 0; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2036 | |
| 2037 | ctx = container_of(peer, struct hlua_csk_ctx, xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2038 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2039 | /* Set the flag which destroy the session. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2040 | ctx->die = 1; |
| 2041 | appctx_wakeup(ctx->appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2042 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2043 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2044 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2045 | return 0; |
| 2046 | } |
| 2047 | |
| 2048 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2049 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2050 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2051 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2052 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2053 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2054 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2055 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2056 | struct hlua *hlua; |
| 2057 | |
| 2058 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2059 | hlua = hlua_gethlua(L); |
| 2060 | if (!hlua) |
| 2061 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2062 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2063 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2064 | |
| 2065 | /* Check if we run on the same thread than the xreator thread. |
| 2066 | * We cannot access to the socket if the thread is different. |
| 2067 | */ |
| 2068 | if (socket->tid != tid) |
| 2069 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2070 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2071 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2072 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2073 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2074 | |
| 2075 | hlua->gc_count--; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2076 | ctx = container_of(peer, struct hlua_csk_ctx, xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2077 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2078 | /* Set the flag which destroy the session. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2079 | ctx->die = 1; |
| 2080 | appctx_wakeup(ctx->appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2081 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2082 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2083 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2084 | return 0; |
| 2085 | } |
| 2086 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2087 | /* The close function calls close_helper. |
| 2088 | */ |
| 2089 | __LJMP static int hlua_socket_close(lua_State *L) |
| 2090 | { |
| 2091 | MAY_LJMP(check_args(L, 1, "close")); |
| 2092 | return hlua_socket_close_helper(L); |
| 2093 | } |
| 2094 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2095 | /* This Lua function assumes that the stack contain three parameters. |
| 2096 | * 1 - USERDATA containing a struct socket |
| 2097 | * 2 - INTEGER with values of the macro defined below |
| 2098 | * If the integer is -1, we must read at most one line. |
| 2099 | * If the integer is -2, we ust read all the data until the |
| 2100 | * end of the stream. |
| 2101 | * If the integer is positive value, we must read a number of |
| 2102 | * bytes corresponding to this value. |
| 2103 | */ |
| 2104 | #define HLSR_READ_LINE (-1) |
| 2105 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2106 | __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] | 2107 | { |
| 2108 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2109 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2110 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2111 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2112 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2113 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2114 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2115 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2116 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2117 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2118 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2119 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 2120 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2121 | struct stream *s; |
| 2122 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2123 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2124 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2125 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2126 | hlua = hlua_gethlua(L); |
| 2127 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2128 | /* Check if this lua stack is schedulable. */ |
| 2129 | if (!hlua || !hlua->task) |
| 2130 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 2131 | "'frontend', 'backend' or 'task'")); |
| 2132 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2133 | /* Check if we run on the same thread than the xreator thread. |
| 2134 | * We cannot access to the socket if the thread is different. |
| 2135 | */ |
| 2136 | if (socket->tid != tid) |
| 2137 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2138 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2139 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2140 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2141 | if (!peer) |
| 2142 | goto no_peer; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2143 | |
| 2144 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2145 | appctx = csk_ctx->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame^] | 2146 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2147 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2148 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2149 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2150 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2151 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2152 | if (nblk < 0) /* Connection close. */ |
| 2153 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2154 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2155 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2156 | |
| 2157 | /* remove final \r\n. */ |
| 2158 | if (nblk == 1) { |
| 2159 | if (blk1[len1-1] == '\n') { |
| 2160 | len1--; |
| 2161 | skip_at_end++; |
| 2162 | if (blk1[len1-1] == '\r') { |
| 2163 | len1--; |
| 2164 | skip_at_end++; |
| 2165 | } |
| 2166 | } |
| 2167 | } |
| 2168 | else { |
| 2169 | if (blk2[len2-1] == '\n') { |
| 2170 | len2--; |
| 2171 | skip_at_end++; |
| 2172 | if (blk2[len2-1] == '\r') { |
| 2173 | len2--; |
| 2174 | skip_at_end++; |
| 2175 | } |
| 2176 | } |
| 2177 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2178 | } |
| 2179 | |
| 2180 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2181 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2182 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2183 | if (nblk < 0) /* Connection close. */ |
| 2184 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2185 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2186 | goto connection_empty; |
| 2187 | } |
| 2188 | |
| 2189 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2190 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2191 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2192 | if (nblk < 0) /* Connection close. */ |
| 2193 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2194 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2195 | goto connection_empty; |
| 2196 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2197 | missing_bytes = wanted - socket->b.n; |
| 2198 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2199 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2200 | len1 = missing_bytes; |
| 2201 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2202 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2203 | } |
| 2204 | |
| 2205 | len = len1; |
| 2206 | |
| 2207 | luaL_addlstring(&socket->b, blk1, len1); |
| 2208 | if (nblk == 2) { |
| 2209 | len += len2; |
| 2210 | luaL_addlstring(&socket->b, blk2, len2); |
| 2211 | } |
| 2212 | |
| 2213 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2214 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2215 | |
| 2216 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2217 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2218 | |
| 2219 | /* If the pattern reclaim to read all the data |
| 2220 | * in the connection, got out. |
| 2221 | */ |
| 2222 | if (wanted == HLSR_READ_ALL) |
| 2223 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2224 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2225 | goto connection_empty; |
| 2226 | |
| 2227 | /* Return result. */ |
| 2228 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2229 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2230 | return 1; |
| 2231 | |
| 2232 | connection_closed: |
| 2233 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2234 | xref_unlock(&socket->xref, peer); |
| 2235 | |
| 2236 | no_peer: |
| 2237 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2238 | /* If the buffer containds data. */ |
| 2239 | if (socket->b.n > 0) { |
| 2240 | luaL_pushresult(&socket->b); |
| 2241 | return 1; |
| 2242 | } |
| 2243 | lua_pushnil(L); |
| 2244 | lua_pushstring(L, "connection closed."); |
| 2245 | return 2; |
| 2246 | |
| 2247 | connection_empty: |
| 2248 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2249 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_read, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2250 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2251 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2252 | } |
| 2253 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2254 | 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] | 2255 | return 0; |
| 2256 | } |
| 2257 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2258 | /* This Lua function gets two parameters. The first one can be string |
| 2259 | * or a number. If the string is "*l", the user requires one line. If |
| 2260 | * 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] | 2261 | * If the value is a number, the user require a number of bytes equal |
| 2262 | * to the value. The default value is "*l" (a line). |
| 2263 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2264 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2265 | * integer takes this values: |
| 2266 | * -1 : read a line |
| 2267 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2268 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2269 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2270 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2271 | * concatenated with the read data. |
| 2272 | */ |
| 2273 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2274 | { |
| 2275 | int wanted = HLSR_READ_LINE; |
| 2276 | const char *pattern; |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2277 | int lastarg, type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2278 | char *error; |
| 2279 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2280 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2281 | |
| 2282 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2283 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2284 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2285 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2286 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2287 | /* Check if we run on the same thread than the xreator thread. |
| 2288 | * We cannot access to the socket if the thread is different. |
| 2289 | */ |
| 2290 | if (socket->tid != tid) |
| 2291 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2292 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2293 | /* check for pattern. */ |
| 2294 | if (lua_gettop(L) >= 2) { |
| 2295 | type = lua_type(L, 2); |
| 2296 | if (type == LUA_TSTRING) { |
| 2297 | pattern = lua_tostring(L, 2); |
| 2298 | if (strcmp(pattern, "*a") == 0) |
| 2299 | wanted = HLSR_READ_ALL; |
| 2300 | else if (strcmp(pattern, "*l") == 0) |
| 2301 | wanted = HLSR_READ_LINE; |
| 2302 | else { |
| 2303 | wanted = strtoll(pattern, &error, 10); |
| 2304 | if (*error != '\0') |
| 2305 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2306 | } |
| 2307 | } |
| 2308 | else if (type == LUA_TNUMBER) { |
| 2309 | wanted = lua_tointeger(L, 2); |
| 2310 | if (wanted < 0) |
| 2311 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2312 | } |
| 2313 | } |
| 2314 | |
| 2315 | /* Set pattern. */ |
| 2316 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2317 | |
| 2318 | /* Check if we would replace the top by itself. */ |
| 2319 | if (lua_gettop(L) != 2) |
| 2320 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2321 | |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2322 | /* Save index of the top of the stack because since buffers are used, it |
| 2323 | * may change |
| 2324 | */ |
| 2325 | lastarg = lua_gettop(L); |
| 2326 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2327 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2328 | luaL_buffinit(L, &socket->b); |
| 2329 | |
| 2330 | /* Check prefix. */ |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2331 | if (lastarg >= 3) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2332 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2333 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2334 | pattern = lua_tolstring(L, 3, &len); |
| 2335 | luaL_addlstring(&socket->b, pattern, len); |
| 2336 | } |
| 2337 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2338 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2339 | } |
| 2340 | |
| 2341 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2342 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2343 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2344 | 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] | 2345 | { |
| 2346 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2347 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2348 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2349 | struct appctx *appctx; |
| 2350 | size_t buf_len; |
| 2351 | const char *buf; |
| 2352 | int len; |
| 2353 | int send_len; |
| 2354 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2355 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2356 | struct stream *s; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2357 | struct conn_stream *cs; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2358 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2359 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2360 | hlua = hlua_gethlua(L); |
| 2361 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2362 | /* Check if this lua stack is schedulable. */ |
| 2363 | if (!hlua || !hlua->task) |
| 2364 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2365 | "'frontend', 'backend' or 'task'")); |
| 2366 | |
| 2367 | /* Get object */ |
| 2368 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2369 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2370 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2371 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2372 | /* Check if we run on the same thread than the xreator thread. |
| 2373 | * We cannot access to the socket if the thread is different. |
| 2374 | */ |
| 2375 | if (socket->tid != tid) |
| 2376 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2377 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2378 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2379 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2380 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2381 | lua_pushinteger(L, -1); |
| 2382 | return 1; |
| 2383 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2384 | |
| 2385 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2386 | appctx = csk_ctx->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame^] | 2387 | cs = appctx_cs(appctx); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2388 | s = __cs_strm(cs); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2389 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2390 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2391 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2392 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2393 | lua_pushinteger(L, -1); |
| 2394 | return 1; |
| 2395 | } |
| 2396 | |
| 2397 | /* Update the input buffer data. */ |
| 2398 | buf += sent; |
| 2399 | send_len = buf_len - sent; |
| 2400 | |
| 2401 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2402 | if (sent >= buf_len) { |
| 2403 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2404 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2405 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2406 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2407 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2408 | * the request buffer if its not required. |
| 2409 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2410 | if (s->req.buf.size == 0) { |
Christopher Faulet | 8f45eec | 2022-04-01 17:19:36 +0200 | [diff] [blame] | 2411 | if (!cs_alloc_ibuf(cs, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2412 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2413 | } |
| 2414 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2415 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2416 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2417 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2418 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2419 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2420 | |
| 2421 | /* send data */ |
| 2422 | if (len < send_len) |
| 2423 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2424 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2425 | |
| 2426 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2427 | * the data" (-2) are not expected because the available length |
| 2428 | * is tested. |
| 2429 | * Other unknown error are also not expected. |
| 2430 | */ |
| 2431 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2432 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2433 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2434 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2435 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2436 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2437 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2438 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2439 | return 1; |
| 2440 | } |
| 2441 | |
| 2442 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2443 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2444 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2445 | s->req.rex = TICK_ETERNITY; |
| 2446 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2447 | |
| 2448 | /* Update length sent. */ |
| 2449 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2450 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2451 | |
| 2452 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2453 | if (sent + len >= buf_len) { |
| 2454 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2455 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2456 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2457 | |
| 2458 | hlua_socket_write_yield_return: |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2459 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2460 | xref_unlock(&socket->xref, peer); |
| 2461 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2462 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2463 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2464 | 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] | 2465 | return 0; |
| 2466 | } |
| 2467 | |
| 2468 | /* This function initiate the send of data. It just check the input |
| 2469 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2470 | * 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] | 2471 | * "hlua_socket_write_yield" that can yield. |
| 2472 | * |
| 2473 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2474 | * the associated object. The second is a string buffer. The third is |
| 2475 | * a facultative integer that represents where is the buffer position |
| 2476 | * of the start of the data that can send. The first byte is the |
| 2477 | * position "1". The default value is "1". The fourth argument is a |
| 2478 | * facultative integer that represents where is the buffer position |
| 2479 | * of the end of the data that can send. The default is the last byte. |
| 2480 | */ |
| 2481 | static int hlua_socket_send(struct lua_State *L) |
| 2482 | { |
| 2483 | int i; |
| 2484 | int j; |
| 2485 | const char *buf; |
| 2486 | size_t buf_len; |
| 2487 | |
| 2488 | /* Check number of arguments. */ |
| 2489 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2490 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2491 | |
| 2492 | /* Get the string. */ |
| 2493 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2494 | |
| 2495 | /* Get and check j. */ |
| 2496 | if (lua_gettop(L) == 4) { |
| 2497 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2498 | if (j < 0) |
| 2499 | j = buf_len + j + 1; |
| 2500 | if (j > buf_len) |
| 2501 | j = buf_len + 1; |
| 2502 | lua_pop(L, 1); |
| 2503 | } |
| 2504 | else |
| 2505 | j = buf_len; |
| 2506 | |
| 2507 | /* Get and check i. */ |
| 2508 | if (lua_gettop(L) == 3) { |
| 2509 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2510 | if (i < 0) |
| 2511 | i = buf_len + i + 1; |
| 2512 | if (i > buf_len) |
| 2513 | i = buf_len + 1; |
| 2514 | lua_pop(L, 1); |
| 2515 | } else |
| 2516 | i = 1; |
| 2517 | |
| 2518 | /* Check bth i and j. */ |
| 2519 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2520 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2521 | return 1; |
| 2522 | } |
| 2523 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2524 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2525 | return 1; |
| 2526 | } |
| 2527 | if (i == 0) |
| 2528 | i = 1; |
| 2529 | if (j == 0) |
| 2530 | j = 1; |
| 2531 | |
| 2532 | /* Pop the string. */ |
| 2533 | lua_pop(L, 1); |
| 2534 | |
| 2535 | /* Update the buffer length. */ |
| 2536 | buf += i - 1; |
| 2537 | buf_len = j - i + 1; |
| 2538 | lua_pushlstring(L, buf, buf_len); |
| 2539 | |
| 2540 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2541 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2542 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2543 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2544 | } |
| 2545 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2546 | #define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345") |
Christopher Faulet | e6465b3 | 2021-10-22 15:36:08 +0200 | [diff] [blame] | 2547 | __LJMP static inline int hlua_socket_info(struct lua_State *L, const struct sockaddr_storage *addr) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2548 | { |
| 2549 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2550 | int ret; |
| 2551 | int len; |
| 2552 | char *p; |
| 2553 | |
| 2554 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2555 | if (ret <= 0) { |
| 2556 | lua_pushnil(L); |
| 2557 | return 1; |
| 2558 | } |
| 2559 | |
| 2560 | if (ret == AF_UNIX) { |
| 2561 | lua_pushstring(L, buffer+1); |
| 2562 | return 1; |
| 2563 | } |
| 2564 | else if (ret == AF_INET6) { |
| 2565 | buffer[0] = '['; |
| 2566 | len = strlen(buffer); |
| 2567 | buffer[len] = ']'; |
| 2568 | len++; |
| 2569 | buffer[len] = ':'; |
| 2570 | len++; |
| 2571 | p = buffer; |
| 2572 | } |
| 2573 | else if (ret == AF_INET) { |
| 2574 | p = buffer + 1; |
| 2575 | len = strlen(p); |
| 2576 | p[len] = ':'; |
| 2577 | len++; |
| 2578 | } |
| 2579 | else { |
| 2580 | lua_pushnil(L); |
| 2581 | return 1; |
| 2582 | } |
| 2583 | |
| 2584 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2585 | lua_pushnil(L); |
| 2586 | return 1; |
| 2587 | } |
| 2588 | |
| 2589 | lua_pushstring(L, p); |
| 2590 | return 1; |
| 2591 | } |
| 2592 | |
| 2593 | /* Returns information about the peer of the connection. */ |
| 2594 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2595 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2596 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2597 | struct xref *peer; |
| 2598 | struct appctx *appctx; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2599 | struct conn_stream *cs; |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2600 | const struct sockaddr_storage *dst; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2601 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2602 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2603 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2604 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2605 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2606 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2607 | /* Check if we run on the same thread than the xreator thread. |
| 2608 | * We cannot access to the socket if the thread is different. |
| 2609 | */ |
| 2610 | if (socket->tid != tid) |
| 2611 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2612 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2613 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2614 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2615 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2616 | lua_pushnil(L); |
| 2617 | return 1; |
| 2618 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2619 | |
| 2620 | appctx = container_of(peer, struct hlua_csk_ctx, xref)->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame^] | 2621 | cs = appctx_cs(appctx); |
Christopher Faulet | 8da67aa | 2022-03-29 17:53:09 +0200 | [diff] [blame] | 2622 | dst = cs_dst(cs_opposite(cs)); |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2623 | if (!dst) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2624 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2625 | lua_pushnil(L); |
| 2626 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2627 | } |
| 2628 | |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2629 | ret = MAY_LJMP(hlua_socket_info(L, dst)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2630 | xref_unlock(&socket->xref, peer); |
| 2631 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2632 | } |
| 2633 | |
| 2634 | /* Returns information about my connection side. */ |
| 2635 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2636 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2637 | struct hlua_socket *socket; |
| 2638 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2639 | struct appctx *appctx; |
| 2640 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2641 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2642 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2643 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2644 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2645 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2646 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2647 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2648 | /* Check if we run on the same thread than the xreator thread. |
| 2649 | * We cannot access to the socket if the thread is different. |
| 2650 | */ |
| 2651 | if (socket->tid != tid) |
| 2652 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2653 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2654 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2655 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2656 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2657 | lua_pushnil(L); |
| 2658 | return 1; |
| 2659 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2660 | |
| 2661 | appctx = container_of(peer, struct hlua_csk_ctx, xref)->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame^] | 2662 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2663 | |
Christopher Faulet | 95a61e8 | 2021-12-22 14:22:03 +0100 | [diff] [blame] | 2664 | conn = cs_conn(s->csb); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2665 | if (!conn || !conn_get_src(conn)) { |
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 | return 1; |
| 2669 | } |
| 2670 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2671 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2672 | xref_unlock(&socket->xref, peer); |
| 2673 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2674 | } |
| 2675 | |
| 2676 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2677 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2678 | .obj_type = OBJ_TYPE_APPLET, |
| 2679 | .name = "<LUA_TCP>", |
| 2680 | .fct = hlua_socket_handler, |
| 2681 | .release = hlua_socket_release, |
| 2682 | }; |
| 2683 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2684 | __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] | 2685 | { |
| 2686 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2687 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2688 | struct xref *peer; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2689 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2690 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2691 | struct stream *s; |
| 2692 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2693 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2694 | hlua = hlua_gethlua(L); |
| 2695 | if (!hlua) |
| 2696 | return 0; |
| 2697 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2698 | /* Check if we run on the same thread than the xreator thread. |
| 2699 | * We cannot access to the socket if the thread is different. |
| 2700 | */ |
| 2701 | if (socket->tid != tid) |
| 2702 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2703 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2704 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2705 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2706 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2707 | lua_pushnil(L); |
| 2708 | lua_pushstring(L, "Can't connect"); |
| 2709 | return 2; |
| 2710 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2711 | |
| 2712 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2713 | appctx = csk_ctx->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame^] | 2714 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2715 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2716 | /* Check if we run on the same thread than the xreator thread. |
| 2717 | * We cannot access to the socket if the thread is different. |
| 2718 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2719 | if (socket->tid != tid) { |
| 2720 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2721 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2722 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2723 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2724 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2725 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2726 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2727 | lua_pushnil(L); |
| 2728 | lua_pushstring(L, "Can't connect"); |
| 2729 | return 2; |
| 2730 | } |
| 2731 | |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 2732 | appctx = __cs_appctx(s->csf); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2733 | |
| 2734 | /* Check for connection established. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2735 | if (csk_ctx->connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2736 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2737 | lua_pushinteger(L, 1); |
| 2738 | return 1; |
| 2739 | } |
| 2740 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2741 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2742 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2743 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2744 | } |
| 2745 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2746 | 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] | 2747 | return 0; |
| 2748 | } |
| 2749 | |
| 2750 | /* This function fail or initite the connection. */ |
| 2751 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2752 | { |
| 2753 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2754 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2755 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2756 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2757 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2758 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2759 | int low, high; |
| 2760 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2761 | struct xref *peer; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2762 | struct conn_stream *cs; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2763 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2764 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2765 | if (lua_gettop(L) < 2) |
| 2766 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2767 | |
| 2768 | /* Get args. */ |
| 2769 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2770 | |
| 2771 | /* Check if we run on the same thread than the xreator thread. |
| 2772 | * We cannot access to the socket if the thread is different. |
| 2773 | */ |
| 2774 | if (socket->tid != tid) |
| 2775 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2776 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2777 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2778 | if (lua_gettop(L) >= 3) { |
| 2779 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2780 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2781 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2782 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2783 | * that are not enclosed within square brackets. |
| 2784 | */ |
| 2785 | if (port > 0) { |
| 2786 | luaL_buffinit(L, &b); |
| 2787 | luaL_addstring(&b, ip); |
| 2788 | luaL_addchar(&b, ':'); |
| 2789 | luaL_pushresult(&b); |
| 2790 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2791 | } |
| 2792 | } |
| 2793 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2794 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2795 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2796 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2797 | lua_pushnil(L); |
| 2798 | return 1; |
| 2799 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2800 | |
| 2801 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2802 | 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] | 2803 | if (!addr) { |
| 2804 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2805 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2806 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2807 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2808 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2809 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2810 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2811 | if (port == -1) { |
| 2812 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2813 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2814 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2815 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2816 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2817 | if (port == -1) { |
| 2818 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2819 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2820 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2821 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2822 | } |
| 2823 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2824 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2825 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2826 | appctx = csk_ctx->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame^] | 2827 | cs = appctx_cs(appctx); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2828 | s = __cs_strm(cs); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2829 | |
Christopher Faulet | 8da67aa | 2022-03-29 17:53:09 +0200 | [diff] [blame] | 2830 | if (!sockaddr_alloc(&cs_opposite(cs)->dst, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2831 | xref_unlock(&socket->xref, peer); |
| 2832 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2833 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2834 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2835 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2836 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2837 | if (!hlua) |
| 2838 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2839 | |
| 2840 | /* inform the stream that we want to be notified whenever the |
| 2841 | * connection completes. |
| 2842 | */ |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 2843 | cs_cant_get(s->csf); |
| 2844 | cs_rx_endp_more(s->csf); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2845 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2846 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2847 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2848 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2849 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2850 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2851 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2852 | } |
| 2853 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2854 | |
| 2855 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2856 | /* Return yield waiting for connection. */ |
| 2857 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2858 | 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] | 2859 | |
| 2860 | return 0; |
| 2861 | } |
| 2862 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2863 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2864 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2865 | { |
| 2866 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2867 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2868 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2869 | |
| 2870 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2871 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2872 | |
| 2873 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2874 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2875 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2876 | lua_pushnil(L); |
| 2877 | return 1; |
| 2878 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2879 | |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame^] | 2880 | s = appctx_strm(container_of(peer, struct hlua_csk_ctx, xref)->appctx); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2881 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2882 | s->target = &socket_ssl->obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2883 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2884 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2885 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2886 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2887 | |
| 2888 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2889 | { |
| 2890 | return 0; |
| 2891 | } |
| 2892 | |
| 2893 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2894 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2895 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2896 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2897 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2898 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2899 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2900 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2901 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2902 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2903 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2904 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2905 | /* convert the timeout to millis */ |
| 2906 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2907 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2908 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2909 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2910 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2911 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2912 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2913 | 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] | 2914 | |
| 2915 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2916 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2917 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2918 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2919 | /* Check if we run on the same thread than the xreator thread. |
| 2920 | * We cannot access to the socket if the thread is different. |
| 2921 | */ |
| 2922 | if (socket->tid != tid) |
| 2923 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2924 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2925 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2926 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2927 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2928 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2929 | WILL_LJMP(lua_error(L)); |
| 2930 | return 0; |
| 2931 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2932 | |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame^] | 2933 | s = appctx_strm(container_of(peer, struct hlua_csk_ctx, xref)->appctx); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2934 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2935 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2936 | s->req.rto = tmout; |
| 2937 | s->req.wto = tmout; |
| 2938 | s->res.rto = tmout; |
| 2939 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2940 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2941 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2942 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2943 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2944 | |
| 2945 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2946 | task_queue(s->task); |
| 2947 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2948 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2949 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2950 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2951 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2952 | } |
| 2953 | |
| 2954 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2955 | { |
| 2956 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2957 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2958 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2959 | struct session *sess; |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 2960 | struct conn_stream *cs; |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 2961 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2962 | |
| 2963 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2964 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2965 | hlua_pusherror(L, "socket: full stack"); |
| 2966 | goto out_fail_conf; |
| 2967 | } |
| 2968 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2969 | /* Create the object: obj[0] = userdata. */ |
| 2970 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2971 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2972 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2973 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2974 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2975 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2976 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2977 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2978 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2979 | goto out_fail_conf; |
| 2980 | } |
| 2981 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2982 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2983 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2984 | lua_setmetatable(L, -2); |
| 2985 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2986 | /* Create the applet context */ |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 2987 | appctx = appctx_new(&update_applet, NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2988 | if (!appctx) { |
| 2989 | hlua_pusherror(L, "socket: out of memory"); |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 2990 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2991 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2992 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2993 | ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
| 2994 | ctx->connected = 0; |
| 2995 | ctx->appctx = appctx; |
| 2996 | ctx->die = 0; |
| 2997 | LIST_INIT(&ctx->wake_on_write); |
| 2998 | LIST_INIT(&ctx->wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2999 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3000 | /* Now create a session, task and stream for this applet */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 3001 | sess = session_new(socket_proxy, NULL, &appctx->obj_type); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3002 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3003 | hlua_pusherror(L, "socket: out of memory"); |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 3004 | goto out_fail_appctx; |
| 3005 | } |
| 3006 | |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 3007 | cs = cs_new_from_applet(appctx->endp, sess, &BUF_NULL); |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 3008 | if (!cs) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3009 | hlua_pusherror(L, "socket: out of memory"); |
Christopher Faulet | 2479e5f | 2022-01-19 14:50:11 +0100 | [diff] [blame] | 3010 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3011 | } |
| 3012 | |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 3013 | s = DISGUISE(cs_strm(cs)); |
Christopher Faulet | 2479e5f | 2022-01-19 14:50:11 +0100 | [diff] [blame] | 3014 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3015 | /* Initialise cross reference between stream and Lua socket object. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3016 | xref_create(&socket->xref, &ctx->xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3017 | |
Christopher Faulet | 6b0a0fb | 2022-04-04 11:29:28 +0200 | [diff] [blame] | 3018 | /* Configure "right" conn-stream. this "si" is used to connect |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3019 | * and retrieve data from the server. The connection is initialized |
| 3020 | * with the "struct server". |
| 3021 | */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 3022 | cs_set_state(s->csb, CS_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3023 | |
| 3024 | /* Force destination server. */ |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 3025 | s->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
| 3026 | s->target = &socket_tcp->obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3027 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3028 | return 1; |
| 3029 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3030 | out_fail_sess: |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 3031 | session_free(sess); |
| 3032 | out_fail_appctx: |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3033 | appctx_free(appctx); |
| 3034 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3035 | WILL_LJMP(lua_error(L)); |
| 3036 | return 0; |
| 3037 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 3038 | |
| 3039 | /* |
| 3040 | * |
| 3041 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3042 | * Class Channel |
| 3043 | * |
| 3044 | * |
| 3045 | */ |
| 3046 | |
| 3047 | /* Returns the struct hlua_channel join to the class channel in the |
| 3048 | * stack entry "ud" or throws an argument error. |
| 3049 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3050 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3051 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3052 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3053 | } |
| 3054 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3055 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 3056 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3057 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 3058 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3059 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3060 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3061 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3062 | return 0; |
| 3063 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3064 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3065 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3066 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3067 | |
| 3068 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3069 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 3070 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3071 | return 1; |
| 3072 | } |
| 3073 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3074 | /* Helper function returning a filter attached to a channel at the position <ud> |
| 3075 | * in the stack, filling the current offset and length of the filter. If no |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 3076 | * filter is attached, NULL is returned and <offset> and <len> are not |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3077 | * initialized. |
| 3078 | */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3079 | 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] | 3080 | { |
| 3081 | struct filter *filter = NULL; |
| 3082 | |
| 3083 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 3084 | struct hlua_flt_ctx *flt_ctx; |
| 3085 | |
| 3086 | filter = lua_touserdata (L, -1); |
| 3087 | flt_ctx = filter->ctx; |
| 3088 | if (hlua_filter_from_payload(filter)) { |
| 3089 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 3090 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 3091 | } |
| 3092 | } |
| 3093 | |
| 3094 | lua_pop(L, 1); |
| 3095 | return filter; |
| 3096 | } |
| 3097 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3098 | /* Copies <len> bytes of data present in the channel's buffer, starting at the |
| 3099 | * 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] | 3100 | * responsibility to ensure <len> and <offset> are valid. It always return the |
| 3101 | * length of the built string. <len> may be 0, in this case, an empty string is |
| 3102 | * created and 0 is returned. |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3103 | */ |
| 3104 | 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] | 3105 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3106 | size_t block1, block2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3107 | luaL_Buffer b; |
| 3108 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3109 | block1 = len; |
| 3110 | if (block1 > b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset))) |
| 3111 | block1 = b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset)); |
| 3112 | block2 = len - block1; |
| 3113 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3114 | luaL_buffinit(L, &b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3115 | luaL_addlstring(&b, b_peek(&chn->buf, offset), block1); |
| 3116 | if (block2) |
| 3117 | luaL_addlstring(&b, b_orig(&chn->buf), block2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3118 | luaL_pushresult(&b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3119 | return len; |
| 3120 | } |
| 3121 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3122 | /* Inserts the string <str> to the channel's buffer at the offset <offset>. This |
| 3123 | * function returns -1 if data cannot be copied. Otherwise, it returns the |
| 3124 | * number of bytes copied. |
| 3125 | */ |
| 3126 | static int _hlua_channel_insert(struct channel *chn, lua_State *L, struct ist str, size_t offset) |
| 3127 | { |
| 3128 | int ret = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3129 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3130 | /* Nothing to do, just return */ |
| 3131 | if (unlikely(istlen(str) == 0)) |
| 3132 | goto end; |
| 3133 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3134 | if (istlen(str) > c_room(chn)) { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3135 | ret = -1; |
| 3136 | goto end; |
| 3137 | } |
| 3138 | ret = b_insert_blk(&chn->buf, offset, istptr(str), istlen(str)); |
| 3139 | |
| 3140 | end: |
| 3141 | return ret; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3142 | } |
| 3143 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3144 | /* Removes <len> bytes of data at the absolute position <offset>. |
| 3145 | */ |
| 3146 | static void _hlua_channel_delete(struct channel *chn, size_t offset, size_t len) |
| 3147 | { |
| 3148 | size_t end = offset + len; |
| 3149 | |
| 3150 | if (b_peek(&chn->buf, end) != b_tail(&chn->buf)) |
| 3151 | b_move(&chn->buf, b_peek_ofs(&chn->buf, end), |
| 3152 | b_data(&chn->buf) - end, -len); |
| 3153 | b_sub(&chn->buf, len); |
| 3154 | } |
| 3155 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3156 | /* Copies input data in the channel's buffer. It is possible to set a specific |
| 3157 | * offset (0 by default) and a length (all remaining input data starting for the |
| 3158 | * offset by default). If there is not enough input data and more data can be |
| 3159 | * received, this function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3160 | * |
| 3161 | * From an action, All input data are considered. For a filter, the offset and |
| 3162 | * 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] | 3163 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3164 | __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] | 3165 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3166 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3167 | struct filter *filter; |
| 3168 | size_t input, output; |
| 3169 | int offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3170 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3171 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3172 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3173 | output = co_data(chn); |
| 3174 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3175 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3176 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3177 | if (filter && !hlua_filter_from_payload(filter)) |
| 3178 | WILL_LJMP(lua_error(L)); |
| 3179 | |
| 3180 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3181 | if (lua_gettop(L) > 1) { |
| 3182 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3183 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3184 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3185 | offset += output; |
| 3186 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3187 | lua_pushfstring(L, "offset out of range."); |
| 3188 | WILL_LJMP(lua_error(L)); |
| 3189 | } |
| 3190 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3191 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3192 | if (lua_gettop(L) == 3) { |
| 3193 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3194 | if (!len) |
| 3195 | goto dup; |
| 3196 | if (len == -1) |
| 3197 | len = global.tune.bufsize; |
| 3198 | if (len < 0) { |
| 3199 | lua_pushfstring(L, "length out of range."); |
| 3200 | WILL_LJMP(lua_error(L)); |
| 3201 | } |
| 3202 | } |
| 3203 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3204 | if (offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3205 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3206 | /* Yield waiting for more data, as requested */ |
| 3207 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_data_yield, TICK_ETERNITY, 0)); |
| 3208 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3209 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3210 | } |
| 3211 | |
| 3212 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3213 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3214 | return 1; |
| 3215 | } |
| 3216 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3217 | /* Copies the first line (including the trailing LF) of input data in the |
| 3218 | * channel's buffer. It is possible to set a specific offset (0 by default) and |
| 3219 | * a length (all remaining input data starting for the offset by default). If |
| 3220 | * there is not enough input data and more data can be received, the function |
| 3221 | * yields. If a length is explicitly specified, no more data are |
| 3222 | * copied. Otherwise, if no LF is found and more data can be received, this |
| 3223 | * function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3224 | * |
| 3225 | * From an action, All input data are considered. For a filter, the offset and |
| 3226 | * 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] | 3227 | */ |
| 3228 | __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] | 3229 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3230 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3231 | struct filter *filter; |
| 3232 | size_t l, input, output; |
| 3233 | int offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3234 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3235 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3236 | output = co_data(chn); |
| 3237 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3238 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3239 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3240 | if (filter && !hlua_filter_from_payload(filter)) |
| 3241 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3242 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3243 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3244 | if (lua_gettop(L) > 1) { |
| 3245 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3246 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3247 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3248 | offset += output; |
| 3249 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3250 | lua_pushfstring(L, "offset out of range."); |
| 3251 | WILL_LJMP(lua_error(L)); |
| 3252 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3253 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3254 | |
| 3255 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3256 | if (lua_gettop(L) == 3) { |
| 3257 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3258 | if (!len) |
| 3259 | goto dup; |
| 3260 | if (len == -1) |
| 3261 | len = global.tune.bufsize; |
| 3262 | if (len < 0) { |
| 3263 | lua_pushfstring(L, "length out of range."); |
| 3264 | WILL_LJMP(lua_error(L)); |
| 3265 | } |
| 3266 | } |
| 3267 | |
| 3268 | for (l = 0; l < len; l++) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3269 | if (l + offset >= output + input) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3270 | break; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3271 | if (*(b_peek(&chn->buf, offset + l)) == '\n') { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3272 | len = l+1; |
| 3273 | goto dup; |
| 3274 | } |
| 3275 | } |
| 3276 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3277 | if (offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3278 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3279 | /* Yield waiting for more data */ |
| 3280 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_line_yield, TICK_ETERNITY, 0)); |
| 3281 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3282 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3283 | } |
| 3284 | |
| 3285 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3286 | _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3287 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3288 | } |
| 3289 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3290 | /* [ DEPRECATED ] |
| 3291 | * |
| 3292 | * Duplicate all input data foud in the channel's buffer. The data are not |
| 3293 | * removed from the buffer. This function relies on _hlua_channel_dup(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3294 | * |
| 3295 | * From an action, All input data are considered. For a filter, the offset and |
| 3296 | * 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] | 3297 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3298 | __LJMP static int hlua_channel_dup(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3299 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3300 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3301 | struct filter *filter; |
| 3302 | size_t offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3303 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3304 | MAY_LJMP(check_args(L, 1, "dup")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3305 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3306 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3307 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3308 | WILL_LJMP(lua_error(L)); |
| 3309 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3310 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3311 | offset = co_data(chn); |
| 3312 | len = ci_data(chn); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3313 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3314 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3315 | if (filter && !hlua_filter_from_payload(filter)) |
| 3316 | WILL_LJMP(lua_error(L)); |
| 3317 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3318 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3319 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3320 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3321 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3322 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3323 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3324 | return 1; |
| 3325 | } |
| 3326 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3327 | /* [ DEPRECATED ] |
| 3328 | * |
| 3329 | * Get all input data foud in the channel's buffer. The data are removed from |
| 3330 | * the buffer after the copy. This function relies on _hlua_channel_dup() and |
| 3331 | * _hlua_channel_delete(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3332 | * |
| 3333 | * From an action, All input data are considered. For a filter, the offset and |
| 3334 | * 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] | 3335 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3336 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3337 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3338 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3339 | struct filter *filter; |
| 3340 | size_t offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3341 | int ret; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3342 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3343 | MAY_LJMP(check_args(L, 1, "get")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3344 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3345 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3346 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3347 | WILL_LJMP(lua_error(L)); |
| 3348 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3349 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3350 | offset = co_data(chn); |
| 3351 | len = ci_data(chn); |
| 3352 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3353 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3354 | if (filter && !hlua_filter_from_payload(filter)) |
| 3355 | WILL_LJMP(lua_error(L)); |
| 3356 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3357 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3358 | lua_pushnil(L); |
| 3359 | return 1; |
| 3360 | } |
| 3361 | |
| 3362 | ret = _hlua_channel_dup(chn, L, offset, len); |
| 3363 | _hlua_channel_delete(chn, offset, ret); |
| 3364 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3365 | } |
| 3366 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3367 | /* This functions consumes and returns one line. If the channel is closed, |
| 3368 | * 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] | 3369 | * 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] | 3370 | * value. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3371 | * |
| 3372 | * From an action, All input data are considered. For a filter, the offset and |
| 3373 | * 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] | 3374 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3375 | __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] | 3376 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3377 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3378 | struct filter *filter; |
| 3379 | size_t l, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3380 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3381 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3382 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3383 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3384 | offset = co_data(chn); |
| 3385 | len = ci_data(chn); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3386 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3387 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3388 | if (filter && !hlua_filter_from_payload(filter)) |
| 3389 | WILL_LJMP(lua_error(L)); |
| 3390 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3391 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3392 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3393 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3394 | } |
| 3395 | |
| 3396 | for (l = 0; l < len; l++) { |
| 3397 | if (*(b_peek(&chn->buf, offset+l)) == '\n') { |
| 3398 | len = l+1; |
| 3399 | goto dup; |
| 3400 | } |
| 3401 | } |
| 3402 | |
| 3403 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3404 | /* Yield waiting for more data */ |
| 3405 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0)); |
| 3406 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3407 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3408 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3409 | ret = _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3410 | _hlua_channel_delete(chn, offset, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3411 | return 1; |
| 3412 | } |
| 3413 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3414 | /* [ DEPRECATED ] |
| 3415 | * |
| 3416 | * Check arguments for the function "hlua_channel_getline_yield". |
| 3417 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3418 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3419 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3420 | struct channel *chn; |
| 3421 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3422 | MAY_LJMP(check_args(L, 1, "getline")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3423 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3424 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3425 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3426 | WILL_LJMP(lua_error(L)); |
| 3427 | } |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3428 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3429 | } |
| 3430 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3431 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3432 | * available input data are returned. The offset may be negactive to start from |
| 3433 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3434 | * size. |
| 3435 | */ |
| 3436 | __LJMP static int hlua_channel_get_data(lua_State *L) |
| 3437 | { |
| 3438 | struct channel *chn; |
| 3439 | |
| 3440 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3441 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 3442 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3443 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3444 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3445 | WILL_LJMP(lua_error(L)); |
| 3446 | } |
| 3447 | return MAY_LJMP(hlua_channel_get_data_yield(L, 0, 0)); |
| 3448 | } |
| 3449 | |
| 3450 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3451 | * available input data are returned. The offset may be negactive to start from |
| 3452 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3453 | * size. |
| 3454 | */ |
| 3455 | __LJMP static int hlua_channel_get_line(lua_State *L) |
| 3456 | { |
| 3457 | struct channel *chn; |
| 3458 | |
| 3459 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3460 | WILL_LJMP(luaL_error(L, "'line' expects at most 2 arguments")); |
| 3461 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3462 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3463 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3464 | WILL_LJMP(lua_error(L)); |
| 3465 | } |
| 3466 | return MAY_LJMP(hlua_channel_get_line_yield(L, 0, 0)); |
| 3467 | } |
| 3468 | |
| 3469 | /* Appends a string into the input side of channel. It returns the length of the |
| 3470 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3471 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3472 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3473 | * |
| 3474 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3475 | */ |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3476 | __LJMP static int hlua_channel_append(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3477 | { |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3478 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3479 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3480 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3481 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3482 | int ret; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3483 | |
| 3484 | MAY_LJMP(check_args(L, 2, "append")); |
| 3485 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3486 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3487 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3488 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3489 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3490 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3491 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3492 | offset = co_data(chn); |
| 3493 | len = ci_data(chn); |
| 3494 | |
| 3495 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3496 | if (filter && !hlua_filter_from_payload(filter)) |
| 3497 | WILL_LJMP(lua_error(L)); |
| 3498 | |
| 3499 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3500 | if (ret > 0 && filter) { |
| 3501 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3502 | |
| 3503 | flt_update_offsets(filter, chn, ret); |
| 3504 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3505 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3506 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3507 | return 1; |
| 3508 | } |
| 3509 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3510 | /* Prepends a string into the input side of channel. It returns the length of the |
| 3511 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3512 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3513 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3514 | * |
| 3515 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3516 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3517 | __LJMP static int hlua_channel_prepend(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3518 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3519 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3520 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3521 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3522 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3523 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3524 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3525 | MAY_LJMP(check_args(L, 2, "prepend")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3526 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3527 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3528 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3529 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3530 | WILL_LJMP(lua_error(L)); |
| 3531 | } |
| 3532 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3533 | offset = co_data(chn); |
| 3534 | len = ci_data(chn); |
| 3535 | |
| 3536 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3537 | if (filter && !hlua_filter_from_payload(filter)) |
| 3538 | WILL_LJMP(lua_error(L)); |
| 3539 | |
| 3540 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3541 | if (ret > 0 && filter) { |
| 3542 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3543 | |
| 3544 | flt_update_offsets(filter, chn, ret); |
| 3545 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3546 | } |
| 3547 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3548 | lua_pushinteger(L, ret); |
| 3549 | return 1; |
| 3550 | } |
| 3551 | |
| 3552 | /* Inserts a given amount of input data at the given offset by a string |
| 3553 | * content. By default the string is appended at the end of input data. It |
| 3554 | * returns the length of the written string, or -1 if the channel is closed or |
| 3555 | * if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3556 | * |
| 3557 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3558 | */ |
| 3559 | __LJMP static int hlua_channel_insert_data(lua_State *L) |
| 3560 | { |
| 3561 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3562 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3563 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3564 | size_t sz, input, output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3565 | int ret, offset; |
| 3566 | |
| 3567 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 3568 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
| 3569 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3570 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3571 | |
| 3572 | output = co_data(chn); |
| 3573 | input = ci_data(chn); |
| 3574 | |
| 3575 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3576 | if (filter && !hlua_filter_from_payload(filter)) |
| 3577 | WILL_LJMP(lua_error(L)); |
| 3578 | |
| 3579 | offset = input + output; |
| 3580 | if (lua_gettop(L) > 2) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3581 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3582 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3583 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3584 | offset += output; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3585 | if (offset < output || offset > output + input) { |
| 3586 | lua_pushfstring(L, "offset out of range."); |
| 3587 | WILL_LJMP(lua_error(L)); |
| 3588 | } |
| 3589 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3590 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3591 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3592 | WILL_LJMP(lua_error(L)); |
| 3593 | } |
| 3594 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3595 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3596 | if (ret > 0 && filter) { |
| 3597 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3598 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3599 | flt_update_offsets(filter, chn, ret); |
| 3600 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3601 | } |
| 3602 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3603 | lua_pushinteger(L, ret); |
| 3604 | return 1; |
| 3605 | } |
| 3606 | /* Replaces a given amount of input data at the given offset by a string |
| 3607 | * content. By default all remaining data are removed (offset = 0 and len = |
| 3608 | * -1). It returns the length of the written string, or -1 if the channel is |
| 3609 | * closed or if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3610 | * |
| 3611 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3612 | */ |
| 3613 | __LJMP static int hlua_channel_set_data(lua_State *L) |
| 3614 | { |
| 3615 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3616 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3617 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3618 | size_t sz, input, output; |
| 3619 | int ret, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3620 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3621 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 3622 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 3623 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3624 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3625 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3626 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3627 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3628 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3629 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3630 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3631 | output = co_data(chn); |
| 3632 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3633 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3634 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3635 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3636 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3637 | |
| 3638 | offset = output; |
| 3639 | if (lua_gettop(L) > 2) { |
| 3640 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3641 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3642 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3643 | offset += output; |
| 3644 | if (offset < output || offset > input + output) { |
| 3645 | lua_pushfstring(L, "offset out of range."); |
| 3646 | WILL_LJMP(lua_error(L)); |
| 3647 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3648 | } |
| 3649 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3650 | len = output + input - offset; |
| 3651 | if (lua_gettop(L) == 4) { |
| 3652 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 3653 | if (!len) |
| 3654 | goto set; |
| 3655 | if (len == -1) |
| 3656 | len = output + input - offset; |
| 3657 | if (len < 0 || offset + len > output + input) { |
| 3658 | lua_pushfstring(L, "length out of range."); |
| 3659 | WILL_LJMP(lua_error(L)); |
| 3660 | } |
| 3661 | } |
| 3662 | |
| 3663 | set: |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3664 | /* 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] | 3665 | if (sz > c_room(chn) + len) |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3666 | lua_pushinteger(L, -1); |
| 3667 | else { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3668 | _hlua_channel_delete(chn, offset, len); |
| 3669 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3670 | if (filter) { |
| 3671 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3672 | |
| 3673 | len -= (ret > 0 ? ret : 0); |
| 3674 | flt_update_offsets(filter, chn, -len); |
| 3675 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3676 | } |
| 3677 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3678 | lua_pushinteger(L, ret); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3679 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3680 | return 1; |
| 3681 | } |
| 3682 | |
| 3683 | /* 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] | 3684 | * 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] | 3685 | * the removed data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3686 | * |
| 3687 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3688 | */ |
| 3689 | __LJMP static int hlua_channel_del_data(lua_State *L) |
| 3690 | { |
| 3691 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3692 | struct filter *filter; |
| 3693 | size_t input, output; |
| 3694 | int offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3695 | |
| 3696 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3697 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
| 3698 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3699 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3700 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3701 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3702 | WILL_LJMP(lua_error(L)); |
| 3703 | } |
| 3704 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3705 | output = co_data(chn); |
| 3706 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3707 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3708 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3709 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3710 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3711 | |
| 3712 | offset = output; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 3713 | if (lua_gettop(L) > 1) { |
| 3714 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3715 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3716 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3717 | offset += output; |
| 3718 | if (offset < output || offset > input + output) { |
| 3719 | lua_pushfstring(L, "offset out of range."); |
| 3720 | WILL_LJMP(lua_error(L)); |
| 3721 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3722 | } |
| 3723 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3724 | len = output + input - offset; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 3725 | if (lua_gettop(L) == 3) { |
| 3726 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3727 | if (!len) |
| 3728 | goto end; |
| 3729 | if (len == -1) |
| 3730 | len = output + input - offset; |
| 3731 | if (len < 0 || offset + len > output + input) { |
| 3732 | lua_pushfstring(L, "length out of range."); |
| 3733 | WILL_LJMP(lua_error(L)); |
| 3734 | } |
| 3735 | } |
| 3736 | |
| 3737 | _hlua_channel_delete(chn, offset, len); |
| 3738 | if (filter) { |
| 3739 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3740 | |
| 3741 | flt_update_offsets(filter, chn, -len); |
| 3742 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3743 | } |
| 3744 | |
| 3745 | end: |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3746 | lua_pushinteger(L, len); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3747 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3748 | } |
| 3749 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3750 | /* Append data in the output side of the buffer. This data is immediately |
| 3751 | * sent. The function returns the amount of data written. If the buffer |
| 3752 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3753 | * if the channel is closed. |
| 3754 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3755 | __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] | 3756 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3757 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3758 | struct filter *filter; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3759 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3760 | size_t offset, len, sz; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3761 | int l, ret; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3762 | struct hlua *hlua; |
| 3763 | |
| 3764 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3765 | hlua = hlua_gethlua(L); |
| 3766 | if (!hlua) { |
| 3767 | lua_pushnil(L); |
| 3768 | return 1; |
| 3769 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3770 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3771 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3772 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3773 | l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3774 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3775 | offset = co_data(chn); |
| 3776 | len = ci_data(chn); |
| 3777 | |
| 3778 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3779 | if (filter && !hlua_filter_from_payload(filter)) |
| 3780 | WILL_LJMP(lua_error(L)); |
| 3781 | |
| 3782 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3783 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3784 | lua_pushinteger(L, -1); |
| 3785 | return 1; |
| 3786 | } |
| 3787 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3788 | len = c_room(chn); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3789 | if (len > sz -l) { |
| 3790 | if (filter) { |
| 3791 | lua_pushinteger(L, -1); |
| 3792 | return 1; |
| 3793 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3794 | len = sz - l; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3795 | } |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3796 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3797 | ret = _hlua_channel_insert(chn, L, ist2(str, len), offset); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3798 | if (ret == -1) { |
| 3799 | lua_pop(L, 1); |
| 3800 | lua_pushinteger(L, -1); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3801 | return 1; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3802 | } |
| 3803 | if (ret) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3804 | if (filter) { |
| 3805 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3806 | |
| 3807 | |
| 3808 | flt_update_offsets(filter, chn, ret); |
| 3809 | FLT_OFF(filter, chn) += ret; |
| 3810 | flt_ctx->cur_off[CHN_IDX(chn)] += ret; |
| 3811 | } |
| 3812 | else |
| 3813 | c_adv(chn, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3814 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3815 | l += ret; |
| 3816 | lua_pop(L, 1); |
| 3817 | lua_pushinteger(L, l); |
| 3818 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3819 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3820 | if (l < sz) { |
| 3821 | /* Yield only if the channel's output is not empty. |
| 3822 | * Otherwise it means we cannot add more data. */ |
| 3823 | if (co_data(chn) == 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3824 | return 1; |
| 3825 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3826 | /* If we are waiting for space in the response buffer, we |
| 3827 | * must set the flag WAKERESWR. This flag required the task |
| 3828 | * wake up if any activity is detected on the response buffer. |
| 3829 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3830 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3831 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3832 | else |
| 3833 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3834 | 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] | 3835 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3836 | |
| 3837 | return 1; |
| 3838 | } |
| 3839 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3840 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3841 | * yield the LUA process, and resume it without checking the |
| 3842 | * input arguments. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3843 | * |
| 3844 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3845 | */ |
| 3846 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3847 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3848 | struct channel *chn; |
| 3849 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3850 | MAY_LJMP(check_args(L, 2, "send")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3851 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3852 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3853 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3854 | WILL_LJMP(lua_error(L)); |
| 3855 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3856 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3857 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3858 | } |
| 3859 | |
| 3860 | /* This function forward and amount of butes. The data pass from |
| 3861 | * the input side of the buffer to the output side, and can be |
| 3862 | * forwarded. This function never fails. |
| 3863 | * |
| 3864 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3865 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3866 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3867 | __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] | 3868 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3869 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3870 | struct filter *filter; |
| 3871 | size_t offset, len, fwd; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3872 | int l, max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3873 | struct hlua *hlua; |
| 3874 | |
| 3875 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3876 | hlua = hlua_gethlua(L); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3877 | if (!hlua) { |
| 3878 | lua_pushnil(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3879 | return 1; |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3880 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3881 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3882 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3883 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3884 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3885 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3886 | offset = co_data(chn); |
| 3887 | len = ci_data(chn); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3888 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3889 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3890 | if (filter && !hlua_filter_from_payload(filter)) |
| 3891 | WILL_LJMP(lua_error(L)); |
| 3892 | |
| 3893 | max = fwd - l; |
| 3894 | if (max > len) |
| 3895 | max = len; |
| 3896 | |
| 3897 | if (filter) { |
| 3898 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3899 | |
| 3900 | FLT_OFF(filter, chn) += max; |
| 3901 | flt_ctx->cur_off[CHN_IDX(chn)] += max; |
| 3902 | flt_ctx->cur_len[CHN_IDX(chn)] -= max; |
| 3903 | } |
| 3904 | else |
| 3905 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3906 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3907 | l += max; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3908 | lua_pop(L, 1); |
| 3909 | lua_pushinteger(L, l); |
| 3910 | |
| 3911 | /* Check if it miss bytes to forward. */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3912 | if (l < fwd) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3913 | /* The the input channel or the output channel are closed, we |
| 3914 | * must return the amount of data forwarded. |
| 3915 | */ |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3916 | 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] | 3917 | return 1; |
| 3918 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3919 | /* If we are waiting for space data in the response buffer, we |
| 3920 | * must set the flag WAKERESWR. This flag required the task |
| 3921 | * wake up if any activity is detected on the response buffer. |
| 3922 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3923 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3924 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3925 | else |
| 3926 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3927 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3928 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3929 | 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] | 3930 | } |
| 3931 | |
| 3932 | return 1; |
| 3933 | } |
| 3934 | |
| 3935 | /* Just check the input and prepare the stack for the previous |
| 3936 | * function "hlua_channel_forward_yield" |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3937 | * |
| 3938 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3939 | */ |
| 3940 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3941 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3942 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3943 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3944 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3945 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3946 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3947 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3948 | WILL_LJMP(lua_error(L)); |
| 3949 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3950 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3951 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3952 | } |
| 3953 | |
| 3954 | /* Just returns the number of bytes available in the input |
| 3955 | * side of the buffer. This function never fails. |
| 3956 | */ |
| 3957 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3958 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3959 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3960 | struct filter *filter; |
| 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, "input")); |
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 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3969 | if (filter || !IS_HTX_STRM(chn_strm(chn))) |
| 3970 | lua_pushinteger(L, input); |
| 3971 | else { |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3972 | struct htx *htx = htxbuf(&chn->buf); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3973 | |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3974 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3975 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3976 | return 1; |
| 3977 | } |
| 3978 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3979 | /* Returns true if the channel is full. */ |
| 3980 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3981 | { |
| 3982 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3983 | |
| 3984 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3985 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3986 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3987 | * applet). |
| 3988 | */ |
| 3989 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3990 | return 1; |
| 3991 | } |
| 3992 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3993 | /* Returns true if the channel may still receive data. */ |
| 3994 | __LJMP static int hlua_channel_may_recv(lua_State *L) |
| 3995 | { |
| 3996 | struct channel *chn; |
| 3997 | |
| 3998 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 3999 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4000 | lua_pushboolean(L, (!channel_input_closed(chn) && channel_may_recv(chn))); |
| 4001 | return 1; |
| 4002 | } |
| 4003 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 4004 | /* Returns true if the channel is the response channel. */ |
| 4005 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 4006 | { |
| 4007 | struct channel *chn; |
| 4008 | |
| 4009 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 4010 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4011 | |
| 4012 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 4013 | return 1; |
| 4014 | } |
| 4015 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4016 | /* Just returns the number of bytes available in the output |
| 4017 | * side of the buffer. This function never fails. |
| 4018 | */ |
| 4019 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 4020 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4021 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4022 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4023 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4024 | MAY_LJMP(check_args(L, 1, "output")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4025 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4026 | |
| 4027 | output = co_data(chn); |
| 4028 | input = ci_data(chn); |
| 4029 | hlua_channel_filter(L, 1, chn, &output, &input); |
| 4030 | |
| 4031 | lua_pushinteger(L, output); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4032 | return 1; |
| 4033 | } |
| 4034 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4035 | /* |
| 4036 | * |
| 4037 | * |
| 4038 | * Class Fetches |
| 4039 | * |
| 4040 | * |
| 4041 | */ |
| 4042 | |
| 4043 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4044 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4045 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4046 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4047 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4048 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4049 | } |
| 4050 | |
| 4051 | /* This function creates and push in the stack a fetch object according |
| 4052 | * with a current TXN. |
| 4053 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4054 | 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] | 4055 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4056 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4057 | |
| 4058 | /* Check stack size. */ |
| 4059 | if (!lua_checkstack(L, 3)) |
| 4060 | return 0; |
| 4061 | |
| 4062 | /* Create the object: obj[0] = userdata. |
| 4063 | * Note that the base of the Fetches object is the |
| 4064 | * transaction object. |
| 4065 | */ |
| 4066 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4067 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4068 | lua_rawseti(L, -2, 0); |
| 4069 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4070 | hsmp->s = txn->s; |
| 4071 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4072 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4073 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4074 | |
| 4075 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 4076 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 4077 | lua_setmetatable(L, -2); |
| 4078 | |
| 4079 | return 1; |
| 4080 | } |
| 4081 | |
| 4082 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 4083 | * It uses closure argument to store the associated sample-fetch. It |
| 4084 | * returns only one argument or throws an error. An error is thrown |
| 4085 | * only if an error is encountered during the argument parsing. If |
| 4086 | * the "sample-fetch" function fails, nil is returned. |
| 4087 | */ |
| 4088 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 4089 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4090 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4091 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4092 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4093 | int i; |
| 4094 | struct sample smp; |
| 4095 | |
| 4096 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4097 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4098 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4099 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4100 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4101 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 4102 | /* Check execution authorization. */ |
| 4103 | if (f->use & SMP_USE_HTTP_ANY && |
| 4104 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 4105 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 4106 | "is not available in Lua services", f->kw); |
| 4107 | WILL_LJMP(lua_error(L)); |
| 4108 | } |
| 4109 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4110 | /* Get extra arguments. */ |
| 4111 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 4112 | if (i >= ARGM_NBARGS) |
| 4113 | break; |
| 4114 | hlua_lua2arg(L, i + 2, &args[i]); |
| 4115 | } |
| 4116 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4117 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4118 | |
| 4119 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4120 | 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] | 4121 | |
| 4122 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4123 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4124 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4125 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4126 | } |
| 4127 | |
| 4128 | /* Initialise the sample. */ |
| 4129 | memset(&smp, 0, sizeof(smp)); |
| 4130 | |
| 4131 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4132 | 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] | 4133 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4134 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4135 | lua_pushstring(L, ""); |
| 4136 | else |
| 4137 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4138 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4139 | } |
| 4140 | |
| 4141 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4142 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4143 | hlua_smp2lua_str(L, &smp); |
| 4144 | else |
| 4145 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4146 | |
| 4147 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4148 | free_args(args); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4149 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4150 | |
| 4151 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4152 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4153 | WILL_LJMP(lua_error(L)); |
| 4154 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4155 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4156 | |
| 4157 | /* |
| 4158 | * |
| 4159 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4160 | * Class Converters |
| 4161 | * |
| 4162 | * |
| 4163 | */ |
| 4164 | |
| 4165 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4166 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4167 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4168 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4169 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4170 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4171 | } |
| 4172 | |
| 4173 | /* This function creates and push in the stack a Converters object |
| 4174 | * according with a current TXN. |
| 4175 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4176 | 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] | 4177 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4178 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4179 | |
| 4180 | /* Check stack size. */ |
| 4181 | if (!lua_checkstack(L, 3)) |
| 4182 | return 0; |
| 4183 | |
| 4184 | /* Create the object: obj[0] = userdata. |
| 4185 | * Note that the base of the Converters object is the |
| 4186 | * same than the TXN object. |
| 4187 | */ |
| 4188 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4189 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4190 | lua_rawseti(L, -2, 0); |
| 4191 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4192 | hsmp->s = txn->s; |
| 4193 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4194 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4195 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4196 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4197 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4198 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 4199 | lua_setmetatable(L, -2); |
| 4200 | |
| 4201 | return 1; |
| 4202 | } |
| 4203 | |
| 4204 | /* This function is an LUA binding. It is called with each converter. |
| 4205 | * It uses closure argument to store the associated converter. It |
| 4206 | * returns only one argument or throws an error. An error is thrown |
| 4207 | * only if an error is encountered during the argument parsing. If |
| 4208 | * the converter function function fails, nil is returned. |
| 4209 | */ |
| 4210 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 4211 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4212 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4213 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4214 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4215 | int i; |
| 4216 | struct sample smp; |
| 4217 | |
| 4218 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4219 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4220 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4221 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4222 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4223 | |
| 4224 | /* Get extra arguments. */ |
| 4225 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 4226 | if (i >= ARGM_NBARGS) |
| 4227 | break; |
| 4228 | hlua_lua2arg(L, i + 3, &args[i]); |
| 4229 | } |
| 4230 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4231 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4232 | |
| 4233 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4234 | 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] | 4235 | |
| 4236 | /* Run the special args checker. */ |
| 4237 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 4238 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4239 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4240 | } |
| 4241 | |
| 4242 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4243 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4244 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 4245 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4246 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4247 | } |
| 4248 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4249 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 4250 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4251 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4252 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4253 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4254 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4255 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4256 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4257 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 4258 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4259 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4260 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4261 | } |
| 4262 | |
| 4263 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4264 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4265 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4266 | lua_pushstring(L, ""); |
| 4267 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4268 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4269 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4270 | } |
| 4271 | |
| 4272 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4273 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4274 | hlua_smp2lua_str(L, &smp); |
| 4275 | else |
| 4276 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4277 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4278 | free_args(args); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4279 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4280 | |
| 4281 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4282 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4283 | WILL_LJMP(lua_error(L)); |
| 4284 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4285 | } |
| 4286 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4287 | /* |
| 4288 | * |
| 4289 | * |
| 4290 | * Class AppletTCP |
| 4291 | * |
| 4292 | * |
| 4293 | */ |
| 4294 | |
| 4295 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4296 | * a class stream, otherwise it throws an error. |
| 4297 | */ |
| 4298 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 4299 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4300 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4301 | } |
| 4302 | |
| 4303 | /* This function creates and push in the stack an Applet object |
| 4304 | * according with a current TXN. |
| 4305 | */ |
| 4306 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 4307 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4308 | struct hlua_appctx *luactx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame^] | 4309 | struct stream *s = appctx_strm(ctx); |
Christopher Faulet | 2da02ae | 2022-02-24 13:45:27 +0100 | [diff] [blame] | 4310 | struct proxy *p; |
| 4311 | |
| 4312 | ALREADY_CHECKED(s); |
| 4313 | p = s->be; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4314 | |
| 4315 | /* Check stack size. */ |
| 4316 | if (!lua_checkstack(L, 3)) |
| 4317 | return 0; |
| 4318 | |
| 4319 | /* Create the object: obj[0] = userdata. |
| 4320 | * Note that the base of the Converters object is the |
| 4321 | * same than the TXN object. |
| 4322 | */ |
| 4323 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4324 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4325 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4326 | luactx->appctx = ctx; |
| 4327 | luactx->htxn.s = s; |
| 4328 | luactx->htxn.p = p; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4329 | |
| 4330 | /* Create the "f" field that contains a list of fetches. */ |
| 4331 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4332 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4333 | return 0; |
| 4334 | lua_settable(L, -3); |
| 4335 | |
| 4336 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4337 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4338 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4339 | return 0; |
| 4340 | lua_settable(L, -3); |
| 4341 | |
| 4342 | /* Create the "c" field that contains a list of converters. */ |
| 4343 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4344 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4345 | return 0; |
| 4346 | lua_settable(L, -3); |
| 4347 | |
| 4348 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4349 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4350 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4351 | return 0; |
| 4352 | lua_settable(L, -3); |
| 4353 | |
| 4354 | /* Pop a class stream metatable and affect it to the table. */ |
| 4355 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 4356 | lua_setmetatable(L, -2); |
| 4357 | |
| 4358 | return 1; |
| 4359 | } |
| 4360 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4361 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 4362 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4363 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4364 | struct stream *s; |
| 4365 | const char *name; |
| 4366 | size_t len; |
| 4367 | struct sample smp; |
| 4368 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4369 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4370 | 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] | 4371 | |
| 4372 | /* It is useles to retrieve the stream, but this function |
| 4373 | * runs only in a stream context. |
| 4374 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4375 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4376 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4377 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4378 | |
| 4379 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4380 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4381 | hlua_lua2smp(L, 3, &smp); |
| 4382 | |
| 4383 | /* Store the sample in a variable. */ |
| 4384 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4385 | |
| 4386 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4387 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4388 | else |
| 4389 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4390 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4391 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4392 | } |
| 4393 | |
| 4394 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 4395 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4396 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4397 | struct stream *s; |
| 4398 | const char *name; |
| 4399 | size_t len; |
| 4400 | struct sample smp; |
| 4401 | |
| 4402 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4403 | |
| 4404 | /* It is useles to retrieve the stream, but this function |
| 4405 | * runs only in a stream context. |
| 4406 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4407 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4408 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4409 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4410 | |
| 4411 | /* Unset the variable. */ |
| 4412 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4413 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4414 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4415 | } |
| 4416 | |
| 4417 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 4418 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4419 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4420 | struct stream *s; |
| 4421 | const char *name; |
| 4422 | size_t len; |
| 4423 | struct sample smp; |
| 4424 | |
| 4425 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4426 | |
| 4427 | /* It is useles to retrieve the stream, but this function |
| 4428 | * runs only in a stream context. |
| 4429 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4430 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4431 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4432 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4433 | |
| 4434 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 4435 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4436 | lua_pushnil(L); |
| 4437 | return 1; |
| 4438 | } |
| 4439 | |
| 4440 | return hlua_smp2lua(L, &smp); |
| 4441 | } |
| 4442 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4443 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 4444 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4445 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4446 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4447 | struct hlua *hlua; |
| 4448 | |
| 4449 | /* 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] | 4450 | if (!s->hlua) |
| 4451 | return 0; |
| 4452 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4453 | |
| 4454 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4455 | |
| 4456 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4457 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4458 | |
| 4459 | /* Get and store new value. */ |
| 4460 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4461 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4462 | |
| 4463 | return 0; |
| 4464 | } |
| 4465 | |
| 4466 | __LJMP static int hlua_applet_tcp_get_priv(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)); |
| 4469 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4470 | struct hlua *hlua; |
| 4471 | |
| 4472 | /* 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] | 4473 | if (!s->hlua) { |
| 4474 | lua_pushnil(L); |
| 4475 | return 1; |
| 4476 | } |
| 4477 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4478 | |
| 4479 | /* Push configuration index in the stack. */ |
| 4480 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4481 | |
| 4482 | return 1; |
| 4483 | } |
| 4484 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4485 | /* If expected data not yet available, it returns a yield. This function |
| 4486 | * consumes the data in the buffer. It returns a string containing the |
| 4487 | * data. This string can be empty. |
| 4488 | */ |
| 4489 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4490 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4491 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame^] | 4492 | struct conn_stream *cs = appctx_cs(luactx->appctx); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4493 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4494 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4495 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4496 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4497 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4498 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4499 | /* Read the maximum amount of data available. */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4500 | ret = co_getline_nc(cs_oc(cs), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4501 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4502 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4503 | if (ret == 0) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 4504 | cs_cant_get(cs); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4505 | 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] | 4506 | } |
| 4507 | |
| 4508 | /* End of data: commit the total strings and return. */ |
| 4509 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4510 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4511 | return 1; |
| 4512 | } |
| 4513 | |
| 4514 | /* Ensure that the block 2 length is usable. */ |
| 4515 | if (ret == 1) |
| 4516 | len2 = 0; |
| 4517 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4518 | /* don't check the max length read and don't check. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4519 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4520 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4521 | |
| 4522 | /* Consume input channel output buffer data. */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4523 | co_skip(cs_oc(cs), len1 + len2); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4524 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4525 | return 1; |
| 4526 | } |
| 4527 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4528 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4529 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 4530 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4531 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4532 | |
| 4533 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4534 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4535 | |
| 4536 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 4537 | } |
| 4538 | |
| 4539 | /* If expected data not yet available, it returns a yield. This function |
| 4540 | * consumes the data in the buffer. It returns a string containing the |
| 4541 | * data. This string can be empty. |
| 4542 | */ |
| 4543 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4544 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4545 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame^] | 4546 | struct conn_stream *cs = appctx_cs(luactx->appctx); |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4547 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4548 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4549 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4550 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4551 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4552 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4553 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4554 | /* Read the maximum amount of data available. */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4555 | ret = co_getblk_nc(cs_oc(cs), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4556 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4557 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4558 | if (ret == 0) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 4559 | cs_cant_get(cs); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4560 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4561 | } |
| 4562 | |
| 4563 | /* End of data: commit the total strings and return. */ |
| 4564 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4565 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4566 | return 1; |
| 4567 | } |
| 4568 | |
| 4569 | /* Ensure that the block 2 length is usable. */ |
| 4570 | if (ret == 1) |
| 4571 | len2 = 0; |
| 4572 | |
| 4573 | if (len == -1) { |
| 4574 | |
| 4575 | /* If len == -1, catenate all the data avalaile and |
| 4576 | * yield because we want to get all the data until |
| 4577 | * the end of data stream. |
| 4578 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4579 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4580 | luaL_addlstring(&luactx->b, blk2, len2); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4581 | co_skip(cs_oc(cs), len1 + len2); |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 4582 | cs_cant_get(cs); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4583 | 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] | 4584 | |
| 4585 | } else { |
| 4586 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4587 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4588 | if (len1 > len) |
| 4589 | len1 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4590 | luaL_addlstring(&luactx->b, blk1, len1); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4591 | len -= len1; |
| 4592 | |
| 4593 | /* Copy the second block. */ |
| 4594 | if (len2 > len) |
| 4595 | len2 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4596 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4597 | len -= len2; |
| 4598 | |
| 4599 | /* Consume input channel output buffer data. */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4600 | co_skip(cs_oc(cs), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4601 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4602 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4603 | if (len > 0) { |
| 4604 | lua_pushinteger(L, len); |
| 4605 | lua_replace(L, 2); |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 4606 | cs_cant_get(cs); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4607 | 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] | 4608 | } |
| 4609 | |
| 4610 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4611 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4612 | return 1; |
| 4613 | } |
| 4614 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4615 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4616 | hlua_pusherror(L, "Lua: internal error"); |
| 4617 | WILL_LJMP(lua_error(L)); |
| 4618 | return 0; |
| 4619 | } |
| 4620 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4621 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4622 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 4623 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4624 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4625 | int len = -1; |
| 4626 | |
| 4627 | if (lua_gettop(L) > 2) |
| 4628 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4629 | if (lua_gettop(L) >= 2) { |
| 4630 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4631 | lua_pop(L, 1); |
| 4632 | } |
| 4633 | |
| 4634 | /* Confirm or set the required length */ |
| 4635 | lua_pushinteger(L, len); |
| 4636 | |
| 4637 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4638 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4639 | |
| 4640 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4641 | } |
| 4642 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4643 | /* Append data in the output side of the buffer. This data is immediately |
| 4644 | * sent. The function returns the amount of data written. If the buffer |
| 4645 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4646 | * if the channel is closed. |
| 4647 | */ |
| 4648 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4649 | { |
| 4650 | size_t len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4651 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4652 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4653 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame^] | 4654 | struct conn_stream *cs = appctx_cs(luactx->appctx); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4655 | struct channel *chn = cs_ic(cs); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4656 | int max; |
| 4657 | |
| 4658 | /* Get the max amount of data which can write as input in the channel. */ |
| 4659 | max = channel_recv_max(chn); |
| 4660 | if (max > (len - l)) |
| 4661 | max = len - l; |
| 4662 | |
| 4663 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4664 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4665 | |
| 4666 | /* update counters. */ |
| 4667 | l += max; |
| 4668 | lua_pop(L, 1); |
| 4669 | lua_pushinteger(L, l); |
| 4670 | |
| 4671 | /* If some data is not send, declares the situation to the |
| 4672 | * applet, and returns a yield. |
| 4673 | */ |
| 4674 | if (l < len) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 4675 | cs_rx_room_blk(cs); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4676 | 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] | 4677 | } |
| 4678 | |
| 4679 | return 1; |
| 4680 | } |
| 4681 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4682 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4683 | * yield the LUA process, and resume it without checking the |
| 4684 | * input arguments. |
| 4685 | */ |
| 4686 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4687 | { |
| 4688 | MAY_LJMP(check_args(L, 2, "send")); |
| 4689 | lua_pushinteger(L, 0); |
| 4690 | |
| 4691 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4692 | } |
| 4693 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4694 | /* |
| 4695 | * |
| 4696 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4697 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4698 | * |
| 4699 | * |
| 4700 | */ |
| 4701 | |
| 4702 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4703 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4704 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4705 | __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] | 4706 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4707 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4708 | } |
| 4709 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4710 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4711 | * according with a current TXN. |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4712 | * It relies on the caller to have already reserved the room in ctx->svcctx |
| 4713 | * for the local storage of hlua_http_ctx. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4714 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4715 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4716 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4717 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4718 | struct hlua_appctx *luactx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4719 | struct hlua_txn htxn; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame^] | 4720 | struct stream *s = appctx_strm(ctx); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4721 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4722 | struct htx *htx; |
| 4723 | struct htx_blk *blk; |
| 4724 | struct htx_sl *sl; |
| 4725 | struct ist path; |
| 4726 | unsigned long long len = 0; |
| 4727 | int32_t pos; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4728 | struct http_uri_parser parser; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4729 | |
| 4730 | /* Check stack size. */ |
| 4731 | if (!lua_checkstack(L, 3)) |
| 4732 | return 0; |
| 4733 | |
| 4734 | /* Create the object: obj[0] = userdata. |
| 4735 | * Note that the base of the Converters object is the |
| 4736 | * same than the TXN object. |
| 4737 | */ |
| 4738 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4739 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4740 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4741 | luactx->appctx = ctx; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4742 | http_ctx->status = 200; /* Default status code returned. */ |
| 4743 | http_ctx->reason = NULL; /* Use default reason based on status */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4744 | luactx->htxn.s = s; |
| 4745 | luactx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4746 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4747 | /* Create the "f" field that contains a list of fetches. */ |
| 4748 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4749 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4750 | return 0; |
| 4751 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4752 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4753 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4754 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4755 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4756 | return 0; |
| 4757 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4758 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4759 | /* Create the "c" field that contains a list of converters. */ |
| 4760 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4761 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4762 | return 0; |
| 4763 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4764 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4765 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4766 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4767 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4768 | return 0; |
| 4769 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4770 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4771 | htx = htxbuf(&s->req.buf); |
| 4772 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4773 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4774 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4775 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4776 | /* Stores the request method. */ |
| 4777 | lua_pushstring(L, "method"); |
| 4778 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4779 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4780 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4781 | /* Stores the http version. */ |
| 4782 | lua_pushstring(L, "version"); |
| 4783 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4784 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4785 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4786 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4787 | * the array on the top of the stack. |
| 4788 | */ |
| 4789 | lua_pushstring(L, "headers"); |
| 4790 | htxn.s = s; |
| 4791 | htxn.p = px; |
| 4792 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4793 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4794 | return 0; |
| 4795 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4796 | |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4797 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 4798 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4799 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4800 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4801 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4802 | p = path.ptr; |
Tim Duesterhus | 4c8f75f | 2021-11-06 15:14:44 +0100 | [diff] [blame] | 4803 | end = istend(path); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4804 | q = p; |
| 4805 | while (q < end && *q != '?') |
| 4806 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4807 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4808 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4809 | lua_pushstring(L, "path"); |
| 4810 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4811 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4812 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4813 | /* Stores the query string. */ |
| 4814 | lua_pushstring(L, "qs"); |
| 4815 | if (*q == '?') |
| 4816 | q++; |
| 4817 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4818 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4819 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4820 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4821 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4822 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4823 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4824 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4825 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4826 | break; |
| 4827 | if (type == HTX_BLK_DATA) |
| 4828 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4829 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4830 | if (htx->extra != ULLONG_MAX) |
| 4831 | len += htx->extra; |
| 4832 | |
| 4833 | /* Stores the request path. */ |
| 4834 | lua_pushstring(L, "length"); |
| 4835 | lua_pushinteger(L, len); |
| 4836 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4837 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4838 | /* Create an empty array of HTTP request headers. */ |
| 4839 | lua_pushstring(L, "response"); |
| 4840 | lua_newtable(L); |
| 4841 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4842 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4843 | /* Pop a class stream metatable and affect it to the table. */ |
| 4844 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4845 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4846 | |
| 4847 | return 1; |
| 4848 | } |
| 4849 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4850 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4851 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4852 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4853 | struct stream *s; |
| 4854 | const char *name; |
| 4855 | size_t len; |
| 4856 | struct sample smp; |
| 4857 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4858 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4859 | 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] | 4860 | |
| 4861 | /* It is useles to retrieve the stream, but this function |
| 4862 | * runs only in a stream context. |
| 4863 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4864 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4865 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4866 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4867 | |
| 4868 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4869 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4870 | hlua_lua2smp(L, 3, &smp); |
| 4871 | |
| 4872 | /* Store the sample in a variable. */ |
| 4873 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4874 | |
| 4875 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4876 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4877 | else |
| 4878 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4879 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4880 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4881 | } |
| 4882 | |
| 4883 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4884 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4885 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4886 | struct stream *s; |
| 4887 | const char *name; |
| 4888 | size_t len; |
| 4889 | struct sample smp; |
| 4890 | |
| 4891 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4892 | |
| 4893 | /* It is useles to retrieve the stream, but this function |
| 4894 | * runs only in a stream context. |
| 4895 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4896 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4897 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4898 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4899 | |
| 4900 | /* Unset the variable. */ |
| 4901 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4902 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4903 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4904 | } |
| 4905 | |
| 4906 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4907 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4908 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4909 | struct stream *s; |
| 4910 | const char *name; |
| 4911 | size_t len; |
| 4912 | struct sample smp; |
| 4913 | |
| 4914 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4915 | |
| 4916 | /* It is useles to retrieve the stream, but this function |
| 4917 | * runs only in a stream context. |
| 4918 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4919 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4920 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4921 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4922 | |
| 4923 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 4924 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4925 | lua_pushnil(L); |
| 4926 | return 1; |
| 4927 | } |
| 4928 | |
| 4929 | return hlua_smp2lua(L, &smp); |
| 4930 | } |
| 4931 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4932 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4933 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4934 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4935 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4936 | struct hlua *hlua; |
| 4937 | |
| 4938 | /* 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] | 4939 | if (!s->hlua) |
| 4940 | return 0; |
| 4941 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4942 | |
| 4943 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4944 | |
| 4945 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4946 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4947 | |
| 4948 | /* Get and store new value. */ |
| 4949 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4950 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4951 | |
| 4952 | return 0; |
| 4953 | } |
| 4954 | |
| 4955 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4956 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4957 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4958 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4959 | struct hlua *hlua; |
| 4960 | |
| 4961 | /* 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] | 4962 | if (!s->hlua) { |
| 4963 | lua_pushnil(L); |
| 4964 | return 1; |
| 4965 | } |
| 4966 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4967 | |
| 4968 | /* Push configuration index in the stack. */ |
| 4969 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4970 | |
| 4971 | return 1; |
| 4972 | } |
| 4973 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4974 | /* If expected data not yet available, it returns a yield. This function |
| 4975 | * consumes the data in the buffer. It returns a string containing the |
| 4976 | * data. This string can be empty. |
| 4977 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4978 | __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] | 4979 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4980 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame^] | 4981 | struct conn_stream *cs = appctx_cs(luactx->appctx); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4982 | struct channel *req = cs_oc(cs); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4983 | struct htx *htx; |
| 4984 | struct htx_blk *blk; |
| 4985 | size_t count; |
| 4986 | int stop = 0; |
| 4987 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4988 | htx = htx_from_buf(&req->buf); |
| 4989 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4990 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4991 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4992 | while (count && !stop && blk) { |
| 4993 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4994 | uint32_t sz = htx_get_blksz(blk); |
| 4995 | struct ist v; |
| 4996 | uint32_t vlen; |
| 4997 | char *nl; |
| 4998 | |
| 4999 | vlen = sz; |
| 5000 | if (vlen > count) { |
| 5001 | if (type != HTX_BLK_DATA) |
| 5002 | break; |
| 5003 | vlen = count; |
| 5004 | } |
| 5005 | |
| 5006 | switch (type) { |
| 5007 | case HTX_BLK_UNUSED: |
| 5008 | break; |
| 5009 | |
| 5010 | case HTX_BLK_DATA: |
| 5011 | v = htx_get_blk_value(htx, blk); |
| 5012 | v.len = vlen; |
| 5013 | nl = istchr(v, '\n'); |
| 5014 | if (nl != NULL) { |
| 5015 | stop = 1; |
| 5016 | vlen = nl - v.ptr + 1; |
| 5017 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5018 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5019 | break; |
| 5020 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5021 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5022 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5023 | stop = 1; |
| 5024 | break; |
| 5025 | |
| 5026 | default: |
| 5027 | break; |
| 5028 | } |
| 5029 | |
Willy Tarreau | 8424004 | 2022-02-28 16:51:23 +0100 | [diff] [blame] | 5030 | c_rew(req, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5031 | count -= vlen; |
| 5032 | if (sz == vlen) |
| 5033 | blk = htx_remove_blk(htx, blk); |
| 5034 | else { |
| 5035 | htx_cut_data_blk(htx, blk, vlen); |
| 5036 | break; |
| 5037 | } |
| 5038 | } |
| 5039 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5040 | /* The message was fully consumed and no more data are expected |
| 5041 | * (EOM flag set). |
| 5042 | */ |
| 5043 | if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) |
| 5044 | stop = 1; |
| 5045 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5046 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5047 | if (!stop) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 5048 | cs_cant_get(cs); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5049 | 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] | 5050 | } |
| 5051 | |
| 5052 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5053 | luaL_pushresult(&luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5054 | return 1; |
| 5055 | } |
| 5056 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5057 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5058 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5059 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5060 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5061 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5062 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5063 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5064 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5065 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5066 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5067 | } |
| 5068 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5069 | /* If expected data not yet available, it returns a yield. This function |
| 5070 | * consumes the data in the buffer. It returns a string containing the |
| 5071 | * data. This string can be empty. |
| 5072 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5073 | __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] | 5074 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5075 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame^] | 5076 | struct conn_stream *cs = appctx_cs(luactx->appctx); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 5077 | struct channel *req = cs_oc(cs); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5078 | struct htx *htx; |
| 5079 | struct htx_blk *blk; |
| 5080 | size_t count; |
| 5081 | int len; |
| 5082 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5083 | htx = htx_from_buf(&req->buf); |
| 5084 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5085 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 5086 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5087 | while (count && len && blk) { |
| 5088 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5089 | uint32_t sz = htx_get_blksz(blk); |
| 5090 | struct ist v; |
| 5091 | uint32_t vlen; |
| 5092 | |
| 5093 | vlen = sz; |
| 5094 | if (len > 0 && vlen > len) |
| 5095 | vlen = len; |
| 5096 | if (vlen > count) { |
| 5097 | if (type != HTX_BLK_DATA) |
| 5098 | break; |
| 5099 | vlen = count; |
| 5100 | } |
| 5101 | |
| 5102 | switch (type) { |
| 5103 | case HTX_BLK_UNUSED: |
| 5104 | break; |
| 5105 | |
| 5106 | case HTX_BLK_DATA: |
| 5107 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5108 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5109 | break; |
| 5110 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5111 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5112 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5113 | len = 0; |
| 5114 | break; |
| 5115 | |
| 5116 | default: |
| 5117 | break; |
| 5118 | } |
| 5119 | |
Willy Tarreau | 8424004 | 2022-02-28 16:51:23 +0100 | [diff] [blame] | 5120 | c_rew(req, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5121 | count -= vlen; |
| 5122 | if (len > 0) |
| 5123 | len -= vlen; |
| 5124 | if (sz == vlen) |
| 5125 | blk = htx_remove_blk(htx, blk); |
| 5126 | else { |
| 5127 | htx_cut_data_blk(htx, blk, vlen); |
| 5128 | break; |
| 5129 | } |
| 5130 | } |
| 5131 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5132 | /* The message was fully consumed and no more data are expected |
| 5133 | * (EOM flag set). |
| 5134 | */ |
| 5135 | if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) |
| 5136 | len = 0; |
| 5137 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5138 | htx_to_buf(htx, &req->buf); |
| 5139 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5140 | /* If we are no other data available, yield waiting for new data. */ |
| 5141 | if (len) { |
| 5142 | if (len > 0) { |
| 5143 | lua_pushinteger(L, len); |
| 5144 | lua_replace(L, 2); |
| 5145 | } |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 5146 | cs_cant_get(cs); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5147 | 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] | 5148 | } |
| 5149 | |
| 5150 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5151 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5152 | return 1; |
| 5153 | } |
| 5154 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5155 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5156 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 5157 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5158 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5159 | int len = -1; |
| 5160 | |
| 5161 | /* Check arguments. */ |
| 5162 | if (lua_gettop(L) > 2) |
| 5163 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 5164 | if (lua_gettop(L) >= 2) { |
| 5165 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5166 | lua_pop(L, 1); |
| 5167 | } |
| 5168 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5169 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5170 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5171 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5172 | luaL_buffinit(L, &luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5173 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5174 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5175 | } |
| 5176 | |
| 5177 | /* Append data in the output side of the buffer. This data is immediately |
| 5178 | * sent. The function returns the amount of data written. If the buffer |
| 5179 | * cannot contain the data, the function yields. The function returns -1 |
| 5180 | * if the channel is closed. |
| 5181 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5182 | __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] | 5183 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5184 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame^] | 5185 | struct conn_stream *cs = appctx_cs(luactx->appctx); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 5186 | struct channel *res = cs_ic(cs); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5187 | struct htx *htx = htx_from_buf(&res->buf); |
| 5188 | const char *data; |
| 5189 | size_t len; |
| 5190 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 5191 | int max; |
| 5192 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 5193 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5194 | if (!max) |
| 5195 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5196 | |
| 5197 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5198 | |
| 5199 | /* Get the max amount of data which can write as input in the channel. */ |
| 5200 | if (max > (len - l)) |
| 5201 | max = len - l; |
| 5202 | |
| 5203 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 5204 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5205 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5206 | |
| 5207 | /* update counters. */ |
| 5208 | l += max; |
| 5209 | lua_pop(L, 1); |
| 5210 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5211 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5212 | /* If some data is not send, declares the situation to the |
| 5213 | * applet, and returns a yield. |
| 5214 | */ |
| 5215 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5216 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5217 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 5218 | cs_rx_room_blk(cs); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5219 | 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] | 5220 | } |
| 5221 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5222 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5223 | return 1; |
| 5224 | } |
| 5225 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5226 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5227 | * yield the LUA process, and resume it without checking the |
| 5228 | * input arguments. |
| 5229 | */ |
| 5230 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 5231 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5232 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5233 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5234 | |
| 5235 | /* We want to send some data. Headers must be sent. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5236 | if (!(http_ctx->flags & APPLET_HDR_SENT)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5237 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 5238 | WILL_LJMP(lua_error(L)); |
| 5239 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5240 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5241 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5242 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5243 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5244 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5245 | } |
| 5246 | |
| 5247 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 5248 | { |
| 5249 | const char *name; |
| 5250 | int ret; |
| 5251 | |
| 5252 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5253 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5254 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 5255 | |
| 5256 | /* Push in the stack the "response" entry. */ |
| 5257 | ret = lua_getfield(L, 1, "response"); |
| 5258 | if (ret != LUA_TTABLE) { |
| 5259 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 5260 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 5261 | WILL_LJMP(lua_error(L)); |
| 5262 | } |
| 5263 | |
| 5264 | /* check if the header is already registered if it is not |
| 5265 | * the case, register it. |
| 5266 | */ |
| 5267 | ret = lua_getfield(L, -1, name); |
| 5268 | if (ret == LUA_TNIL) { |
| 5269 | |
| 5270 | /* Entry not found. */ |
| 5271 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 5272 | |
| 5273 | /* Insert the new header name in the array in the top of the stack. |
| 5274 | * It left the new array in the top of the stack. |
| 5275 | */ |
| 5276 | lua_newtable(L); |
| 5277 | lua_pushvalue(L, 2); |
| 5278 | lua_pushvalue(L, -2); |
| 5279 | lua_settable(L, -4); |
| 5280 | |
| 5281 | } else if (ret != LUA_TTABLE) { |
| 5282 | |
| 5283 | /* corruption error. */ |
| 5284 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 5285 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 5286 | WILL_LJMP(lua_error(L)); |
| 5287 | } |
| 5288 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5289 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5290 | * the header value as new entry. |
| 5291 | */ |
| 5292 | lua_pushvalue(L, 3); |
| 5293 | ret = lua_rawlen(L, -2); |
| 5294 | lua_rawseti(L, -2, ret + 1); |
| 5295 | lua_pushboolean(L, 1); |
| 5296 | return 1; |
| 5297 | } |
| 5298 | |
| 5299 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 5300 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5301 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5302 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5303 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5304 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5305 | |
| 5306 | if (status < 100 || status > 599) { |
| 5307 | lua_pushboolean(L, 0); |
| 5308 | return 1; |
| 5309 | } |
| 5310 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5311 | http_ctx->status = status; |
| 5312 | http_ctx->reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5313 | lua_pushboolean(L, 1); |
| 5314 | return 1; |
| 5315 | } |
| 5316 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5317 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5318 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5319 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5320 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5321 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame^] | 5322 | struct conn_stream *cs = appctx_cs(luactx->appctx); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 5323 | struct channel *res = cs_ic(cs); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5324 | struct htx *htx; |
| 5325 | struct htx_sl *sl; |
| 5326 | struct h1m h1m; |
| 5327 | const char *status, *reason; |
| 5328 | const char *name, *value; |
| 5329 | size_t nlen, vlen; |
| 5330 | unsigned int flags; |
| 5331 | |
| 5332 | /* Send the message at once. */ |
| 5333 | htx = htx_from_buf(&res->buf); |
| 5334 | h1m_init_res(&h1m); |
| 5335 | |
| 5336 | /* Use the same http version than the request. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5337 | status = ultoa_r(http_ctx->status, trash.area, trash.size); |
| 5338 | reason = http_ctx->reason; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5339 | if (reason == NULL) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5340 | reason = http_get_reason(http_ctx->status); |
| 5341 | if (http_ctx->flags & APPLET_HTTP11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5342 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5343 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 5344 | } |
| 5345 | else { |
| 5346 | flags = HTX_SL_F_IS_RESP; |
| 5347 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 5348 | } |
| 5349 | if (!sl) { |
| 5350 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5351 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5352 | WILL_LJMP(lua_error(L)); |
| 5353 | } |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5354 | sl->info.res.status = http_ctx->status; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5355 | |
| 5356 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 5357 | lua_pushvalue(L, 0); |
| 5358 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 5359 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5360 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5361 | WILL_LJMP(lua_error(L)); |
| 5362 | } |
| 5363 | |
| 5364 | /* Browse the list of headers. */ |
| 5365 | lua_pushnil(L); |
| 5366 | while(lua_next(L, -2) != 0) { |
| 5367 | /* We expect a string as -2. */ |
| 5368 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 5369 | 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] | 5370 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5371 | lua_typename(L, lua_type(L, -2))); |
| 5372 | WILL_LJMP(lua_error(L)); |
| 5373 | } |
| 5374 | name = lua_tolstring(L, -2, &nlen); |
| 5375 | |
| 5376 | /* We expect an array as -1. */ |
| 5377 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 5378 | 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] | 5379 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5380 | name, |
| 5381 | lua_typename(L, lua_type(L, -1))); |
| 5382 | WILL_LJMP(lua_error(L)); |
| 5383 | } |
| 5384 | |
| 5385 | /* Browse the table who is on the top of the stack. */ |
| 5386 | lua_pushnil(L); |
| 5387 | while(lua_next(L, -2) != 0) { |
| 5388 | int id; |
| 5389 | |
| 5390 | /* We expect a number as -2. */ |
| 5391 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 5392 | 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] | 5393 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5394 | name, |
| 5395 | lua_typename(L, lua_type(L, -2))); |
| 5396 | WILL_LJMP(lua_error(L)); |
| 5397 | } |
| 5398 | id = lua_tointeger(L, -2); |
| 5399 | |
| 5400 | /* We expect a string as -2. */ |
| 5401 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 5402 | 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] | 5403 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5404 | name, id, |
| 5405 | lua_typename(L, lua_type(L, -1))); |
| 5406 | WILL_LJMP(lua_error(L)); |
| 5407 | } |
| 5408 | value = lua_tolstring(L, -1, &vlen); |
| 5409 | |
| 5410 | /* Simple Protocol checks. */ |
Christopher Faulet | 545fbba | 2021-09-28 09:36:25 +0200 | [diff] [blame] | 5411 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) { |
| 5412 | int ret; |
| 5413 | |
| 5414 | ret = h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
| 5415 | if (ret < 0) { |
| 5416 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 5417 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
| 5418 | name); |
| 5419 | WILL_LJMP(lua_error(L)); |
| 5420 | } |
| 5421 | else if (ret == 0) |
| 5422 | goto next; /* Skip it */ |
| 5423 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5424 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 5425 | struct ist v = ist2(value, vlen); |
| 5426 | int ret; |
| 5427 | |
| 5428 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 5429 | if (ret < 0) { |
| 5430 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5431 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5432 | name); |
| 5433 | WILL_LJMP(lua_error(L)); |
| 5434 | } |
| 5435 | else if (ret == 0) |
| 5436 | goto next; /* Skip it */ |
| 5437 | } |
| 5438 | |
| 5439 | /* Add a new header */ |
| 5440 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 5441 | 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] | 5442 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5443 | name); |
| 5444 | WILL_LJMP(lua_error(L)); |
| 5445 | } |
| 5446 | next: |
| 5447 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5448 | lua_pop(L, 1); |
| 5449 | } |
| 5450 | |
| 5451 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5452 | lua_pop(L, 1); |
| 5453 | } |
| 5454 | |
| 5455 | if (h1m.flags & H1_MF_CHNK) |
| 5456 | h1m.flags &= ~H1_MF_CLEN; |
| 5457 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5458 | h1m.flags |= H1_MF_XFER_LEN; |
| 5459 | |
| 5460 | /* Uset HTX start-line flags */ |
| 5461 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5462 | flags |= HTX_SL_F_XFER_ENC; |
| 5463 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5464 | flags |= HTX_SL_F_XFER_LEN; |
| 5465 | if (h1m.flags & H1_MF_CHNK) |
| 5466 | flags |= HTX_SL_F_CHNK; |
| 5467 | else if (h1m.flags & H1_MF_CLEN) |
| 5468 | flags |= HTX_SL_F_CLEN; |
| 5469 | if (h1m.body_len == 0) |
| 5470 | flags |= HTX_SL_F_BODYLESS; |
| 5471 | } |
| 5472 | sl->flags |= flags; |
| 5473 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5474 | /* 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] | 5475 | * and the status code implies the presence of a message body, we must |
| 5476 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5477 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 5478 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5479 | */ |
| 5480 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5481 | http_ctx->status >= 200 && http_ctx->status != 204 && http_ctx->status != 304) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5482 | /* Add a new header */ |
| 5483 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 5484 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 5485 | 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] | 5486 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5487 | WILL_LJMP(lua_error(L)); |
| 5488 | } |
| 5489 | } |
| 5490 | |
| 5491 | /* Finalize headers. */ |
| 5492 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 5493 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5494 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5495 | WILL_LJMP(lua_error(L)); |
| 5496 | } |
| 5497 | |
| 5498 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 5499 | b_reset(&res->buf); |
| 5500 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 5501 | WILL_LJMP(lua_error(L)); |
| 5502 | } |
| 5503 | |
| 5504 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5505 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5506 | |
| 5507 | /* Headers sent, set the flag. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5508 | http_ctx->flags |= APPLET_HDR_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5509 | return 0; |
| 5510 | |
| 5511 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5512 | /* We will build the status line and the headers of the HTTP response. |
| 5513 | * We will try send at once if its not possible, we give back the hand |
| 5514 | * waiting for more room. |
| 5515 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5516 | __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] | 5517 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5518 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame^] | 5519 | struct conn_stream *cs = appctx_cs(luactx->appctx); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 5520 | struct channel *res = cs_ic(cs); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5521 | |
| 5522 | if (co_data(res)) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 5523 | cs_rx_room_blk(cs); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5524 | 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] | 5525 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5526 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5527 | } |
| 5528 | |
| 5529 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5530 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5531 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5532 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5533 | } |
| 5534 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5535 | /* |
| 5536 | * |
| 5537 | * |
| 5538 | * Class HTTP |
| 5539 | * |
| 5540 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5541 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5542 | |
| 5543 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5544 | * a class stream, otherwise it throws an error. |
| 5545 | */ |
| 5546 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5547 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5548 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 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 | /* This function creates and push in the stack a HTTP object |
| 5552 | * according with a current TXN. |
| 5553 | */ |
| 5554 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5555 | { |
| 5556 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5557 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5558 | /* Check stack size. */ |
| 5559 | if (!lua_checkstack(L, 3)) |
| 5560 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5561 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5562 | /* Create the object: obj[0] = userdata. |
| 5563 | * Note that the base of the Converters object is the |
| 5564 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5565 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5566 | lua_newtable(L); |
| 5567 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5568 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5569 | |
| 5570 | htxn->s = txn->s; |
| 5571 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 5572 | htxn->dir = txn->dir; |
| 5573 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5574 | |
| 5575 | /* Pop a class stream metatable and affect it to the table. */ |
| 5576 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5577 | lua_setmetatable(L, -2); |
| 5578 | |
| 5579 | return 1; |
| 5580 | } |
| 5581 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5582 | /* This function creates and returns an array containing the status-line |
| 5583 | * elements. This function does not fails. |
| 5584 | */ |
| 5585 | __LJMP static int hlua_http_get_stline(lua_State *L, struct htx_sl *sl) |
| 5586 | { |
| 5587 | /* Create the table. */ |
| 5588 | lua_newtable(L); |
| 5589 | |
| 5590 | if (sl->flags & HTX_SL_F_IS_RESP) { |
| 5591 | lua_pushstring(L, "version"); |
| 5592 | lua_pushlstring(L, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); |
| 5593 | lua_settable(L, -3); |
| 5594 | lua_pushstring(L, "code"); |
| 5595 | lua_pushlstring(L, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); |
| 5596 | lua_settable(L, -3); |
| 5597 | lua_pushstring(L, "reason"); |
| 5598 | lua_pushlstring(L, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); |
| 5599 | lua_settable(L, -3); |
| 5600 | } |
| 5601 | else { |
| 5602 | lua_pushstring(L, "method"); |
| 5603 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 5604 | lua_settable(L, -3); |
| 5605 | lua_pushstring(L, "uri"); |
| 5606 | lua_pushlstring(L, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); |
| 5607 | lua_settable(L, -3); |
| 5608 | lua_pushstring(L, "version"); |
| 5609 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 5610 | lua_settable(L, -3); |
| 5611 | } |
| 5612 | return 1; |
| 5613 | } |
| 5614 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5615 | /* This function creates ans returns an array of HTTP headers. |
| 5616 | * This function does not fails. It is used as wrapper with the |
| 5617 | * 2 following functions. |
| 5618 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5619 | __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] | 5620 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5621 | struct htx *htx; |
| 5622 | int32_t pos; |
| 5623 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5624 | /* Create the table. */ |
| 5625 | lua_newtable(L); |
| 5626 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5627 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5628 | htx = htxbuf(&msg->chn->buf); |
| 5629 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5630 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5631 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5632 | struct ist n, v; |
| 5633 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5634 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5635 | if (type == HTX_BLK_HDR) { |
| 5636 | n = htx_get_blk_name(htx,blk); |
| 5637 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5638 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5639 | else if (type == HTX_BLK_EOH) |
| 5640 | break; |
| 5641 | else |
| 5642 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5643 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5644 | /* Check for existing entry: |
| 5645 | * assume that the table is on the top of the stack, and |
| 5646 | * push the key in the stack, the function lua_gettable() |
| 5647 | * perform the lookup. |
| 5648 | */ |
| 5649 | lua_pushlstring(L, n.ptr, n.len); |
| 5650 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5651 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5652 | switch (lua_type(L, -1)) { |
| 5653 | case LUA_TNIL: |
| 5654 | /* Table not found, create it. */ |
| 5655 | lua_pop(L, 1); /* remove the nil value. */ |
| 5656 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5657 | lua_newtable(L); /* create and push empty table. */ |
| 5658 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5659 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5660 | 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] | 5661 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5662 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5663 | case LUA_TTABLE: |
| 5664 | /* Entry found: push the value in the table. */ |
| 5665 | len = lua_rawlen(L, -1); |
| 5666 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5667 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5668 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5669 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5670 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5671 | default: |
| 5672 | /* Other cases are errors. */ |
| 5673 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5674 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5675 | } |
| 5676 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5677 | return 1; |
| 5678 | } |
| 5679 | |
| 5680 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5681 | { |
| 5682 | struct hlua_txn *htxn; |
| 5683 | |
| 5684 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5685 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5686 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5687 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5688 | WILL_LJMP(lua_error(L)); |
| 5689 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5690 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5691 | } |
| 5692 | |
| 5693 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5694 | { |
| 5695 | struct hlua_txn *htxn; |
| 5696 | |
| 5697 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5698 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5699 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5700 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5701 | WILL_LJMP(lua_error(L)); |
| 5702 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5703 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5704 | } |
| 5705 | |
| 5706 | /* This function replace full header, or just a value in |
| 5707 | * the request or in the response. It is a wrapper fir the |
| 5708 | * 4 following functions. |
| 5709 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5710 | __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] | 5711 | { |
| 5712 | size_t name_len; |
| 5713 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5714 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5715 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5716 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5717 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5718 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5719 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5720 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5721 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5722 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5723 | 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] | 5724 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5725 | return 0; |
| 5726 | } |
| 5727 | |
| 5728 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5729 | { |
| 5730 | struct hlua_txn *htxn; |
| 5731 | |
| 5732 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5733 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5734 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5735 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5736 | WILL_LJMP(lua_error(L)); |
| 5737 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5738 | 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] | 5739 | } |
| 5740 | |
| 5741 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5742 | { |
| 5743 | struct hlua_txn *htxn; |
| 5744 | |
| 5745 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5746 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5747 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5748 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5749 | WILL_LJMP(lua_error(L)); |
| 5750 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5751 | 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] | 5752 | } |
| 5753 | |
| 5754 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5755 | { |
| 5756 | struct hlua_txn *htxn; |
| 5757 | |
| 5758 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5759 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5760 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5761 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5762 | WILL_LJMP(lua_error(L)); |
| 5763 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5764 | 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] | 5765 | } |
| 5766 | |
| 5767 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5768 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5769 | struct hlua_txn *htxn; |
| 5770 | |
| 5771 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5772 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5773 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5774 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5775 | WILL_LJMP(lua_error(L)); |
| 5776 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5777 | 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] | 5778 | } |
| 5779 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5780 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5781 | * It is a wrapper for the 2 following functions. |
| 5782 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5783 | __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] | 5784 | { |
| 5785 | size_t len; |
| 5786 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5787 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5788 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5789 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5790 | ctx.blk = NULL; |
| 5791 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5792 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5793 | return 0; |
| 5794 | } |
| 5795 | |
| 5796 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5797 | { |
| 5798 | struct hlua_txn *htxn; |
| 5799 | |
| 5800 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5801 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5802 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5803 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5804 | WILL_LJMP(lua_error(L)); |
| 5805 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5806 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5807 | } |
| 5808 | |
| 5809 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5810 | { |
| 5811 | struct hlua_txn *htxn; |
| 5812 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5813 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5814 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5815 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5816 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5817 | WILL_LJMP(lua_error(L)); |
| 5818 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5819 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5820 | } |
| 5821 | |
| 5822 | /* This function adds an header. It is a wrapper used by |
| 5823 | * the 2 following functions. |
| 5824 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5825 | __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] | 5826 | { |
| 5827 | size_t name_len; |
| 5828 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5829 | size_t value_len; |
| 5830 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5831 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5832 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5833 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5834 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5835 | return 0; |
| 5836 | } |
| 5837 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5838 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5839 | { |
| 5840 | struct hlua_txn *htxn; |
| 5841 | |
| 5842 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5843 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5844 | |
| 5845 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5846 | WILL_LJMP(lua_error(L)); |
| 5847 | |
| 5848 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 5849 | } |
| 5850 | |
| 5851 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5852 | { |
| 5853 | struct hlua_txn *htxn; |
| 5854 | |
| 5855 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5856 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5857 | |
| 5858 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5859 | WILL_LJMP(lua_error(L)); |
| 5860 | |
| 5861 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 5862 | } |
| 5863 | |
| 5864 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5865 | { |
| 5866 | struct hlua_txn *htxn; |
| 5867 | |
| 5868 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5869 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5870 | |
| 5871 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5872 | WILL_LJMP(lua_error(L)); |
| 5873 | |
| 5874 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5875 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 5876 | } |
| 5877 | |
| 5878 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5879 | { |
| 5880 | struct hlua_txn *htxn; |
| 5881 | |
| 5882 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5883 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5884 | |
| 5885 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5886 | WILL_LJMP(lua_error(L)); |
| 5887 | |
| 5888 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5889 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 5890 | } |
| 5891 | |
| 5892 | /* This function set the method. */ |
| 5893 | static int hlua_http_req_set_meth(lua_State *L) |
| 5894 | { |
| 5895 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5896 | size_t name_len; |
| 5897 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5898 | |
| 5899 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5900 | WILL_LJMP(lua_error(L)); |
| 5901 | |
| 5902 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1); |
| 5903 | return 1; |
| 5904 | } |
| 5905 | |
| 5906 | /* This function set the method. */ |
| 5907 | static int hlua_http_req_set_path(lua_State *L) |
| 5908 | { |
| 5909 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5910 | size_t name_len; |
| 5911 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5912 | |
| 5913 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5914 | WILL_LJMP(lua_error(L)); |
| 5915 | |
| 5916 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1); |
| 5917 | return 1; |
| 5918 | } |
| 5919 | |
| 5920 | /* This function set the query-string. */ |
| 5921 | static int hlua_http_req_set_query(lua_State *L) |
| 5922 | { |
| 5923 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5924 | size_t name_len; |
| 5925 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5926 | |
| 5927 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5928 | WILL_LJMP(lua_error(L)); |
| 5929 | |
| 5930 | /* Check length. */ |
| 5931 | if (name_len > trash.size - 1) { |
| 5932 | lua_pushboolean(L, 0); |
| 5933 | return 1; |
| 5934 | } |
| 5935 | |
| 5936 | /* Add the mark question as prefix. */ |
| 5937 | chunk_reset(&trash); |
| 5938 | trash.area[trash.data++] = '?'; |
| 5939 | memcpy(trash.area + trash.data, name, name_len); |
| 5940 | trash.data += name_len; |
| 5941 | |
| 5942 | lua_pushboolean(L, |
| 5943 | http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1); |
| 5944 | return 1; |
| 5945 | } |
| 5946 | |
| 5947 | /* This function set the uri. */ |
| 5948 | static int hlua_http_req_set_uri(lua_State *L) |
| 5949 | { |
| 5950 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5951 | size_t name_len; |
| 5952 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5953 | |
| 5954 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5955 | WILL_LJMP(lua_error(L)); |
| 5956 | |
| 5957 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1); |
| 5958 | return 1; |
| 5959 | } |
| 5960 | |
| 5961 | /* This function set the response code & optionally reason. */ |
| 5962 | static int hlua_http_res_set_status(lua_State *L) |
| 5963 | { |
| 5964 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5965 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5966 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5967 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
| 5968 | |
| 5969 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5970 | WILL_LJMP(lua_error(L)); |
| 5971 | |
| 5972 | http_res_set_status(code, reason, htxn->s); |
| 5973 | return 0; |
| 5974 | } |
| 5975 | |
| 5976 | /* |
| 5977 | * |
| 5978 | * |
| 5979 | * Class HTTPMessage |
| 5980 | * |
| 5981 | * |
| 5982 | */ |
| 5983 | |
| 5984 | /* Returns a struct http_msg if the stack entry "ud" is a class HTTPMessage, |
| 5985 | * otherwise it throws an error. |
| 5986 | */ |
| 5987 | __LJMP static struct http_msg *hlua_checkhttpmsg(lua_State *L, int ud) |
| 5988 | { |
| 5989 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_msg_ref)); |
| 5990 | } |
| 5991 | |
| 5992 | /* Creates and pushes on the stack a HTTP object according with a current TXN. |
| 5993 | */ |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 5994 | 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] | 5995 | { |
| 5996 | /* Check stack size. */ |
| 5997 | if (!lua_checkstack(L, 3)) |
| 5998 | return 0; |
| 5999 | |
| 6000 | lua_newtable(L); |
| 6001 | lua_pushlightuserdata(L, msg); |
| 6002 | lua_rawseti(L, -2, 0); |
| 6003 | |
| 6004 | /* Create the "channel" field that contains the request channel object. */ |
| 6005 | lua_pushstring(L, "channel"); |
| 6006 | if (!hlua_channel_new(L, msg->chn)) |
| 6007 | return 0; |
| 6008 | lua_rawset(L, -3); |
| 6009 | |
| 6010 | /* Pop a class stream metatable and affect it to the table. */ |
| 6011 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_msg_ref); |
| 6012 | lua_setmetatable(L, -2); |
| 6013 | |
| 6014 | return 1; |
| 6015 | } |
| 6016 | |
| 6017 | /* Helper function returning a filter attached to the HTTP message at the |
| 6018 | * position <ud> in the stack, filling the current offset and length of the |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6019 | * filter. If no filter is attached, NULL is returned and <offset> and <len> are |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6020 | * filled with output and input length respectively. |
| 6021 | */ |
| 6022 | static struct filter *hlua_http_msg_filter(lua_State *L, int ud, struct http_msg *msg, size_t *offset, size_t *len) |
| 6023 | { |
| 6024 | struct channel *chn = msg->chn; |
| 6025 | struct htx *htx = htxbuf(&chn->buf); |
| 6026 | struct filter *filter = NULL; |
| 6027 | |
| 6028 | *offset = co_data(msg->chn); |
| 6029 | *len = htx->data - co_data(msg->chn); |
| 6030 | |
| 6031 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 6032 | filter = lua_touserdata (L, -1); |
| 6033 | if (msg->msg_state >= HTTP_MSG_DATA) { |
| 6034 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6035 | |
| 6036 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 6037 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 6038 | } |
| 6039 | } |
| 6040 | |
| 6041 | lua_pop(L, 1); |
| 6042 | return filter; |
| 6043 | } |
| 6044 | |
| 6045 | /* Returns true if the channel attached to the HTTP message is the response |
| 6046 | * channel. |
| 6047 | */ |
| 6048 | __LJMP static int hlua_http_msg_is_resp(lua_State *L) |
| 6049 | { |
| 6050 | struct http_msg *msg; |
| 6051 | |
| 6052 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 6053 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6054 | |
| 6055 | lua_pushboolean(L, !!(msg->chn->flags & CF_ISRESP)); |
| 6056 | return 1; |
| 6057 | } |
| 6058 | |
| 6059 | /* Returns an array containing the elements status-line of the HTTP message. It relies |
| 6060 | * on hlua_http_get_stline(). |
| 6061 | */ |
| 6062 | __LJMP static int hlua_http_msg_get_stline(lua_State *L) |
| 6063 | { |
| 6064 | struct http_msg *msg; |
| 6065 | struct htx *htx; |
| 6066 | struct htx_sl *sl; |
| 6067 | |
| 6068 | MAY_LJMP(check_args(L, 1, "get_stline")); |
| 6069 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6070 | |
| 6071 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6072 | WILL_LJMP(lua_error(L)); |
| 6073 | |
| 6074 | htx = htxbuf(&msg->chn->buf); |
| 6075 | sl = http_get_stline(htx); |
| 6076 | if (!sl) |
| 6077 | return 0; |
| 6078 | return hlua_http_get_stline(L, sl); |
| 6079 | } |
| 6080 | |
| 6081 | /* Returns an array containing all headers of the HTTP message. it relies on |
| 6082 | * hlua_http_get_headers(). |
| 6083 | */ |
| 6084 | __LJMP static int hlua_http_msg_get_headers(lua_State *L) |
| 6085 | { |
| 6086 | struct http_msg *msg; |
| 6087 | |
| 6088 | MAY_LJMP(check_args(L, 1, "get_headers")); |
| 6089 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6090 | |
| 6091 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6092 | WILL_LJMP(lua_error(L)); |
| 6093 | |
| 6094 | return hlua_http_get_headers(L, msg); |
| 6095 | } |
| 6096 | |
| 6097 | /* Deletes all occurrences of an header in the HTTP message matching on its |
| 6098 | * name. It relies on hlua_http_del_hdr(). |
| 6099 | */ |
| 6100 | __LJMP static int hlua_http_msg_del_hdr(lua_State *L) |
| 6101 | { |
| 6102 | struct http_msg *msg; |
| 6103 | |
| 6104 | MAY_LJMP(check_args(L, 2, "del_header")); |
| 6105 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6106 | |
| 6107 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6108 | WILL_LJMP(lua_error(L)); |
| 6109 | |
| 6110 | return hlua_http_del_hdr(L, msg); |
| 6111 | } |
| 6112 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6113 | /* Matches the full value line of all occurrences of an header in the HTTP |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6114 | * message given its name against a regex and replaces it if it matches. It |
| 6115 | * relies on hlua_http_rep_hdr(). |
| 6116 | */ |
| 6117 | __LJMP static int hlua_http_msg_rep_hdr(lua_State *L) |
| 6118 | { |
| 6119 | struct http_msg *msg; |
| 6120 | |
| 6121 | MAY_LJMP(check_args(L, 4, "rep_header")); |
| 6122 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6123 | |
| 6124 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6125 | WILL_LJMP(lua_error(L)); |
| 6126 | |
| 6127 | return hlua_http_rep_hdr(L, msg, 1); |
| 6128 | } |
| 6129 | |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 6130 | /* Matches all comma-separated values of all occurrences of an header in the HTTP |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6131 | * message given its name against a regex and replaces it if it matches. It |
| 6132 | * relies on hlua_http_rep_hdr(). |
| 6133 | */ |
| 6134 | __LJMP static int hlua_http_msg_rep_val(lua_State *L) |
| 6135 | { |
| 6136 | struct http_msg *msg; |
| 6137 | |
| 6138 | MAY_LJMP(check_args(L, 4, "rep_value")); |
| 6139 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6140 | |
| 6141 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6142 | WILL_LJMP(lua_error(L)); |
| 6143 | |
| 6144 | return hlua_http_rep_hdr(L, msg, 0); |
| 6145 | } |
| 6146 | |
| 6147 | /* Add an header in the HTTP message. It relies on hlua_http_add_hdr() */ |
| 6148 | __LJMP static int hlua_http_msg_add_hdr(lua_State *L) |
| 6149 | { |
| 6150 | struct http_msg *msg; |
| 6151 | |
| 6152 | MAY_LJMP(check_args(L, 3, "add_header")); |
| 6153 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6154 | |
| 6155 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6156 | WILL_LJMP(lua_error(L)); |
| 6157 | |
| 6158 | return hlua_http_add_hdr(L, msg); |
| 6159 | } |
| 6160 | |
| 6161 | /* Add an header in the HTTP message removing existing headers with the same |
| 6162 | * name. It relies on hlua_http_del_hdr() and hlua_http_add_hdr(). |
| 6163 | */ |
| 6164 | __LJMP static int hlua_http_msg_set_hdr(lua_State *L) |
| 6165 | { |
| 6166 | struct http_msg *msg; |
| 6167 | |
| 6168 | MAY_LJMP(check_args(L, 3, "set_header")); |
| 6169 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6170 | |
| 6171 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6172 | WILL_LJMP(lua_error(L)); |
| 6173 | |
| 6174 | hlua_http_del_hdr(L, msg); |
| 6175 | return hlua_http_add_hdr(L, msg); |
| 6176 | } |
| 6177 | |
| 6178 | /* Rewrites the request method. It relies on http_req_replace_stline(). */ |
| 6179 | __LJMP static int hlua_http_msg_set_meth(lua_State *L) |
| 6180 | { |
| 6181 | struct stream *s; |
| 6182 | struct http_msg *msg; |
| 6183 | const char *name; |
| 6184 | size_t name_len; |
| 6185 | |
| 6186 | MAY_LJMP(check_args(L, 2, "set_method")); |
| 6187 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6188 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6189 | |
| 6190 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6191 | WILL_LJMP(lua_error(L)); |
| 6192 | |
| 6193 | s = chn_strm(msg->chn); |
| 6194 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, s->be, s) != -1); |
| 6195 | return 1; |
| 6196 | } |
| 6197 | |
| 6198 | /* Rewrites the request path. It relies on http_req_replace_stline(). */ |
| 6199 | __LJMP static int hlua_http_msg_set_path(lua_State *L) |
| 6200 | { |
| 6201 | struct stream *s; |
| 6202 | struct http_msg *msg; |
| 6203 | const char *name; |
| 6204 | size_t name_len; |
| 6205 | |
| 6206 | MAY_LJMP(check_args(L, 2, "set_path")); |
| 6207 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6208 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6209 | |
| 6210 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6211 | WILL_LJMP(lua_error(L)); |
| 6212 | |
| 6213 | s = chn_strm(msg->chn); |
| 6214 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, s->be, s) != -1); |
| 6215 | return 1; |
| 6216 | } |
| 6217 | |
| 6218 | /* Rewrites the request query-string. It relies on http_req_replace_stline(). */ |
| 6219 | __LJMP static int hlua_http_msg_set_query(lua_State *L) |
| 6220 | { |
| 6221 | struct stream *s; |
| 6222 | struct http_msg *msg; |
| 6223 | const char *name; |
| 6224 | size_t name_len; |
| 6225 | |
| 6226 | MAY_LJMP(check_args(L, 2, "set_query")); |
| 6227 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6228 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6229 | |
| 6230 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6231 | WILL_LJMP(lua_error(L)); |
| 6232 | |
| 6233 | /* Check length. */ |
| 6234 | if (name_len > trash.size - 1) { |
| 6235 | lua_pushboolean(L, 0); |
| 6236 | return 1; |
| 6237 | } |
| 6238 | |
| 6239 | /* Add the mark question as prefix. */ |
| 6240 | chunk_reset(&trash); |
| 6241 | trash.area[trash.data++] = '?'; |
| 6242 | memcpy(trash.area + trash.data, name, name_len); |
| 6243 | trash.data += name_len; |
| 6244 | |
| 6245 | s = chn_strm(msg->chn); |
| 6246 | lua_pushboolean(L, http_req_replace_stline(2, trash.area, trash.data, s->be, s) != -1); |
| 6247 | return 1; |
| 6248 | } |
| 6249 | |
| 6250 | /* Rewrites the request URI. It relies on http_req_replace_stline(). */ |
| 6251 | __LJMP static int hlua_http_msg_set_uri(lua_State *L) |
| 6252 | { |
| 6253 | struct stream *s; |
| 6254 | struct http_msg *msg; |
| 6255 | const char *name; |
| 6256 | size_t name_len; |
| 6257 | |
| 6258 | MAY_LJMP(check_args(L, 2, "set_uri")); |
| 6259 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6260 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6261 | |
| 6262 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6263 | WILL_LJMP(lua_error(L)); |
| 6264 | |
| 6265 | s = chn_strm(msg->chn); |
| 6266 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, s->be, s) != -1); |
| 6267 | return 1; |
| 6268 | } |
| 6269 | |
| 6270 | /* Rewrites the response status code. It relies on http_res_set_status(). */ |
| 6271 | __LJMP static int hlua_http_msg_set_status(lua_State *L) |
| 6272 | { |
| 6273 | struct http_msg *msg; |
| 6274 | unsigned int code; |
| 6275 | const char *reason; |
| 6276 | size_t reason_len; |
| 6277 | |
| 6278 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6279 | code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6280 | reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, &reason_len)); |
| 6281 | |
| 6282 | if (!(msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6283 | WILL_LJMP(lua_error(L)); |
| 6284 | |
| 6285 | lua_pushboolean(L, http_res_set_status(code, ist2(reason, reason_len), chn_strm(msg->chn)) != -1); |
| 6286 | return 1; |
| 6287 | } |
| 6288 | |
| 6289 | /* Returns true if the HTTP message is full. */ |
| 6290 | __LJMP static int hlua_http_msg_is_full(lua_State *L) |
| 6291 | { |
| 6292 | struct http_msg *msg; |
| 6293 | |
| 6294 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 6295 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6296 | lua_pushboolean(L, channel_full(msg->chn, 0)); |
| 6297 | return 1; |
| 6298 | } |
| 6299 | |
| 6300 | /* Returns true if the HTTP message may still receive data. */ |
| 6301 | __LJMP static int hlua_http_msg_may_recv(lua_State *L) |
| 6302 | { |
| 6303 | struct http_msg *msg; |
| 6304 | struct htx *htx; |
| 6305 | |
| 6306 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6307 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6308 | htx = htxbuf(&msg->chn->buf); |
| 6309 | lua_pushboolean(L, (htx_expect_more(htx) && !channel_input_closed(msg->chn) && channel_may_recv(msg->chn))); |
| 6310 | return 1; |
| 6311 | } |
| 6312 | |
| 6313 | /* Returns true if the HTTP message EOM was received */ |
| 6314 | __LJMP static int hlua_http_msg_is_eom(lua_State *L) |
| 6315 | { |
| 6316 | struct http_msg *msg; |
| 6317 | struct htx *htx; |
| 6318 | |
| 6319 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6320 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6321 | htx = htxbuf(&msg->chn->buf); |
| 6322 | lua_pushboolean(L, !htx_expect_more(htx)); |
| 6323 | return 1; |
| 6324 | } |
| 6325 | |
| 6326 | /* Returns the number of bytes available in the input side of the HTTP |
| 6327 | * message. This function never fails. |
| 6328 | */ |
| 6329 | __LJMP static int hlua_http_msg_get_in_len(lua_State *L) |
| 6330 | { |
| 6331 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6332 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6333 | |
| 6334 | MAY_LJMP(check_args(L, 1, "input")); |
| 6335 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6336 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6337 | lua_pushinteger(L, input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6338 | return 1; |
| 6339 | } |
| 6340 | |
| 6341 | /* Returns the number of bytes available in the output side of the HTTP |
| 6342 | * message. This function never fails. |
| 6343 | */ |
| 6344 | __LJMP static int hlua_http_msg_get_out_len(lua_State *L) |
| 6345 | { |
| 6346 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6347 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6348 | |
| 6349 | MAY_LJMP(check_args(L, 1, "output")); |
| 6350 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6351 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6352 | lua_pushinteger(L, output); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6353 | return 1; |
| 6354 | } |
| 6355 | |
| 6356 | /* Copies at most <len> bytes of DATA blocks from the HTTP message <msg> |
| 6357 | * starting at the offset <offset> and put it in a string LUA variables. It |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6358 | * returns the built string length. It stops on the first non-DATA HTX |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6359 | * block. This function is called during the payload filtering, so the headers |
| 6360 | * are already scheduled for output (from the filter point of view). |
| 6361 | */ |
| 6362 | static int _hlua_http_msg_dup(struct http_msg *msg, lua_State *L, size_t offset, size_t len) |
| 6363 | { |
| 6364 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 6365 | struct htx_blk *blk; |
| 6366 | struct htx_ret htxret; |
| 6367 | luaL_Buffer b; |
| 6368 | int ret = 0; |
| 6369 | |
| 6370 | luaL_buffinit(L, &b); |
| 6371 | htxret = htx_find_offset(htx, offset); |
| 6372 | for (blk = htxret.blk, offset = htxret.ret; blk && len; blk = htx_get_next_blk(htx, blk)) { |
| 6373 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6374 | struct ist v; |
| 6375 | |
| 6376 | switch (type) { |
| 6377 | case HTX_BLK_UNUSED: |
| 6378 | break; |
| 6379 | |
| 6380 | case HTX_BLK_DATA: |
| 6381 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6382 | v = istadv(v, offset); |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6383 | v = isttrim(v, len); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6384 | |
| 6385 | luaL_addlstring(&b, v.ptr, v.len); |
| 6386 | ret += v.len; |
| 6387 | break; |
| 6388 | |
| 6389 | default: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6390 | if (!ret) |
| 6391 | goto no_data; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6392 | goto end; |
| 6393 | } |
| 6394 | offset = 0; |
| 6395 | } |
| 6396 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6397 | end: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6398 | if (!ret && (htx->flags & HTX_FL_EOM)) |
| 6399 | goto no_data; |
| 6400 | luaL_pushresult(&b); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6401 | return ret; |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6402 | |
| 6403 | no_data: |
| 6404 | /* Remove the empty string and push nil on the stack */ |
| 6405 | lua_pop(L, 1); |
| 6406 | lua_pushnil(L); |
| 6407 | return 0; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6408 | } |
| 6409 | |
| 6410 | /* Copies the string <str> to the HTTP message <msg> at the offset |
| 6411 | * <offset>. This function returns -1 if data cannot be copied. Otherwise, it |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6412 | * returns the amount of data written. This function is responsible to update |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6413 | * the filter context. |
| 6414 | */ |
| 6415 | static int _hlua_http_msg_insert(struct http_msg *msg, struct filter *filter, struct ist str, size_t offset) |
| 6416 | { |
| 6417 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6418 | struct htx_ret htxret; |
| 6419 | int /*max, */ret = 0; |
| 6420 | |
| 6421 | /* Nothing to do, just return */ |
| 6422 | if (unlikely(istlen(str) == 0)) |
| 6423 | goto end; |
| 6424 | |
| 6425 | if (istlen(str) > htx_free_data_space(htx)) { |
| 6426 | ret = -1; |
| 6427 | goto end; |
| 6428 | } |
| 6429 | |
| 6430 | htxret = htx_find_offset(htx, offset); |
| 6431 | if (!htxret.blk || htx_get_blk_type(htxret.blk) != HTX_BLK_DATA) { |
| 6432 | if (!htx_add_last_data(htx, str)) |
| 6433 | goto end; |
| 6434 | } |
| 6435 | else { |
| 6436 | struct ist v = htx_get_blk_value(htx, htxret.blk); |
| 6437 | v.ptr += htxret.ret; |
| 6438 | v.len = 0; |
| 6439 | if (!htx_replace_blk_value(htx, htxret.blk, v, str)) |
| 6440 | goto end; |
| 6441 | } |
| 6442 | ret = str.len; |
| 6443 | if (ret) { |
| 6444 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6445 | flt_update_offsets(filter, msg->chn, ret); |
| 6446 | flt_ctx->cur_len[CHN_IDX(msg->chn)] += ret; |
| 6447 | } |
| 6448 | |
| 6449 | end: |
| 6450 | htx_to_buf(htx, &msg->chn->buf); |
| 6451 | return ret; |
| 6452 | } |
| 6453 | |
| 6454 | /* Helper function removing at most <len> bytes of DATA blocks at the absolute |
| 6455 | * position <offset>. It stops on the first non-DATA HTX block. This function is |
| 6456 | * called during the payload filtering, so the headers are already scheduled for |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6457 | * output (from the filter point of view). This function is responsible to |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6458 | * update the filter context. |
| 6459 | */ |
| 6460 | static void _hlua_http_msg_delete(struct http_msg *msg, struct filter *filter, size_t offset, size_t len) |
| 6461 | { |
| 6462 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6463 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6464 | struct htx_blk *blk; |
| 6465 | struct htx_ret htxret; |
| 6466 | size_t ret = 0; |
| 6467 | |
| 6468 | /* Be sure <len> is always the amount of DATA to remove */ |
| 6469 | if (htx->data == offset+len && htx_get_tail_type(htx) == HTX_BLK_DATA) { |
| 6470 | htx_truncate(htx, offset); |
| 6471 | ret = len; |
| 6472 | goto end; |
| 6473 | } |
| 6474 | |
| 6475 | htxret = htx_find_offset(htx, offset); |
| 6476 | blk = htxret.blk; |
| 6477 | if (htxret.ret) { |
| 6478 | struct ist v; |
| 6479 | |
| 6480 | if (htx_get_blk_type(blk) != HTX_BLK_DATA) |
| 6481 | goto end; |
| 6482 | v = htx_get_blk_value(htx, blk); |
| 6483 | v.ptr += htxret.ret; |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6484 | v = isttrim(v, len); |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6485 | blk = htx_replace_blk_value(htx, blk, v, IST_NULL); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6486 | len -= v.len; |
| 6487 | ret += v.len; |
| 6488 | } |
| 6489 | |
| 6490 | |
| 6491 | while (blk && len) { |
| 6492 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6493 | uint32_t sz = htx_get_blksz(blk); |
| 6494 | |
| 6495 | switch (type) { |
| 6496 | case HTX_BLK_UNUSED: |
| 6497 | break; |
| 6498 | |
| 6499 | case HTX_BLK_DATA: |
| 6500 | if (len < sz) { |
| 6501 | htx_cut_data_blk(htx, blk, len); |
| 6502 | ret += len; |
| 6503 | goto end; |
| 6504 | } |
| 6505 | break; |
| 6506 | |
| 6507 | default: |
| 6508 | goto end; |
| 6509 | } |
| 6510 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6511 | /* Remove all the data block */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6512 | len -= sz; |
| 6513 | ret += sz; |
| 6514 | blk = htx_remove_blk(htx, blk); |
| 6515 | } |
| 6516 | |
| 6517 | end: |
| 6518 | flt_update_offsets(filter, msg->chn, -ret); |
| 6519 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6520 | /* WARNING: we don't call htx_to_buf() on purpose, because we don't want |
| 6521 | * to loose the EOM flag if the message is empty. |
| 6522 | */ |
| 6523 | } |
| 6524 | |
| 6525 | /* Copies input data found in an HTTP message. Unlike the channel function used |
| 6526 | * to duplicate raw data, this one can only be called inside a filter, from |
| 6527 | * http_payload callback. So it cannot yield. An exception is returned if it is |
| 6528 | * called from another callback. If nothing was copied, a nil value is pushed on |
| 6529 | * the stack. |
| 6530 | */ |
| 6531 | __LJMP static int hlua_http_msg_get_body(lua_State *L) |
| 6532 | { |
| 6533 | struct http_msg *msg; |
| 6534 | struct filter *filter; |
| 6535 | size_t output, input; |
| 6536 | int offset, len; |
| 6537 | |
| 6538 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 6539 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 6540 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6541 | |
| 6542 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6543 | WILL_LJMP(lua_error(L)); |
| 6544 | |
| 6545 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6546 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6547 | WILL_LJMP(lua_error(L)); |
| 6548 | |
| 6549 | if (!ci_data(msg->chn) && channel_input_closed(msg->chn)) { |
| 6550 | lua_pushnil(L); |
| 6551 | return 1; |
| 6552 | } |
| 6553 | |
| 6554 | offset = output; |
| 6555 | if (lua_gettop(L) > 1) { |
| 6556 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6557 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6558 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6559 | offset += output; |
| 6560 | if (offset < output || offset > input + output) { |
| 6561 | lua_pushfstring(L, "offset out of range."); |
| 6562 | WILL_LJMP(lua_error(L)); |
| 6563 | } |
| 6564 | } |
| 6565 | len = output + input - offset; |
| 6566 | if (lua_gettop(L) == 3) { |
| 6567 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6568 | if (!len) |
| 6569 | goto dup; |
| 6570 | if (len == -1) |
| 6571 | len = global.tune.bufsize; |
| 6572 | if (len < 0) { |
| 6573 | lua_pushfstring(L, "length out of range."); |
| 6574 | WILL_LJMP(lua_error(L)); |
| 6575 | } |
| 6576 | } |
| 6577 | |
| 6578 | dup: |
| 6579 | _hlua_http_msg_dup(msg, L, offset, len); |
| 6580 | return 1; |
| 6581 | } |
| 6582 | |
| 6583 | /* Appends a string to the HTTP message, after all existing DATA blocks but |
| 6584 | * before the trailers, if any. It returns the amount of data written or -1 if |
| 6585 | * nothing was copied. Unlike the channel function used to append data, this one |
| 6586 | * can only be called inside a filter, from http_payload callback. So it cannot |
| 6587 | * yield. An exception is returned if it is called from another callback. |
| 6588 | */ |
| 6589 | __LJMP static int hlua_http_msg_append(lua_State *L) |
| 6590 | { |
| 6591 | struct http_msg *msg; |
| 6592 | struct filter *filter; |
| 6593 | const char *str; |
| 6594 | size_t offset, len, sz; |
| 6595 | int ret; |
| 6596 | |
| 6597 | MAY_LJMP(check_args(L, 2, "append")); |
| 6598 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6599 | |
| 6600 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6601 | WILL_LJMP(lua_error(L)); |
| 6602 | |
| 6603 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6604 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6605 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6606 | WILL_LJMP(lua_error(L)); |
| 6607 | |
| 6608 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset+len); |
| 6609 | lua_pushinteger(L, ret); |
| 6610 | return 1; |
| 6611 | } |
| 6612 | |
| 6613 | /* Prepends a string to the HTTP message, before all existing DATA blocks. It |
| 6614 | * returns the amount of data written or -1 if nothing was copied. Unlike the |
| 6615 | * channel function used to prepend data, this one can only be called inside a |
| 6616 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6617 | * returned if it is called from another callback. |
| 6618 | */ |
| 6619 | __LJMP static int hlua_http_msg_prepend(lua_State *L) |
| 6620 | { |
| 6621 | struct http_msg *msg; |
| 6622 | struct filter *filter; |
| 6623 | const char *str; |
| 6624 | size_t offset, len, sz; |
| 6625 | int ret; |
| 6626 | |
| 6627 | MAY_LJMP(check_args(L, 2, "prepend")); |
| 6628 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6629 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6630 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6631 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6632 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6633 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6634 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6635 | if (!filter || !hlua_filter_from_payload(filter)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6636 | WILL_LJMP(lua_error(L)); |
| 6637 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6638 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6639 | lua_pushinteger(L, ret); |
| 6640 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6641 | } |
| 6642 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6643 | /* Inserts a string to the HTTP message at a given offset. By default the string |
| 6644 | * is appended at the end of DATA blocks. It returns the amount of data written |
| 6645 | * or -1 if nothing was copied. Unlike the channel function used to insert data, |
| 6646 | * this one can only be called inside a filter, from http_payload callback. So |
| 6647 | * it cannot yield. An exception is returned if it is called from another |
| 6648 | * callback. |
| 6649 | */ |
| 6650 | __LJMP static int hlua_http_msg_insert_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6651 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6652 | struct http_msg *msg; |
| 6653 | struct filter *filter; |
| 6654 | const char *str; |
| 6655 | size_t input, output, sz; |
| 6656 | int offset; |
| 6657 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6658 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6659 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 6660 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
| 6661 | MAY_LJMP(check_args(L, 2, "insert")); |
| 6662 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6663 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6664 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6665 | WILL_LJMP(lua_error(L)); |
| 6666 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6667 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6668 | filter = hlua_http_msg_filter(L, 1, msg, &input, &output); |
| 6669 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6670 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6671 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6672 | offset = input + output; |
| 6673 | if (lua_gettop(L) > 2) { |
| 6674 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6675 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6676 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6677 | offset += output; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6678 | if (offset < output || offset > output + input) { |
| 6679 | lua_pushfstring(L, "offset out of range."); |
| 6680 | WILL_LJMP(lua_error(L)); |
| 6681 | } |
| 6682 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6683 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6684 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6685 | lua_pushinteger(L, ret); |
| 6686 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6687 | } |
| 6688 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6689 | /* Removes a given amount of data from the HTTP message at a given offset. By |
| 6690 | * default all DATA blocks are removed. It returns the amount of data |
| 6691 | * removed. Unlike the channel function used to remove data, this one can only |
| 6692 | * be called inside a filter, from http_payload callback. So it cannot yield. An |
| 6693 | * exception is returned if it is called from another callback. |
| 6694 | */ |
| 6695 | __LJMP static int hlua_http_msg_del_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6696 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6697 | struct http_msg *msg; |
| 6698 | struct filter *filter; |
| 6699 | size_t input, output; |
| 6700 | int offset, len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6701 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6702 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6703 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6704 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6705 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6706 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6707 | WILL_LJMP(lua_error(L)); |
| 6708 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6709 | filter = hlua_http_msg_filter(L, 1, msg, &input, &output); |
| 6710 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6711 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6712 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6713 | offset = input + output; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6714 | if (lua_gettop(L) > 1) { |
| 6715 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6716 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6717 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6718 | offset += output; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6719 | if (offset < output || offset > output + input) { |
| 6720 | lua_pushfstring(L, "offset out of range."); |
| 6721 | WILL_LJMP(lua_error(L)); |
| 6722 | } |
| 6723 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6724 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6725 | len = output + input - offset; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6726 | if (lua_gettop(L) == 3) { |
| 6727 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6728 | if (!len) |
| 6729 | goto end; |
| 6730 | if (len == -1) |
| 6731 | len = output + input - offset; |
| 6732 | if (len < 0 || offset + len > output + input) { |
| 6733 | lua_pushfstring(L, "length out of range."); |
| 6734 | WILL_LJMP(lua_error(L)); |
| 6735 | } |
| 6736 | } |
| 6737 | |
| 6738 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6739 | |
| 6740 | end: |
| 6741 | lua_pushinteger(L, len); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6742 | return 1; |
| 6743 | } |
| 6744 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6745 | /* Replaces a given amount of data at the given offset by a string. By default, |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6746 | * all remaining data are removed, accordingly to the filter context. It returns |
| 6747 | * the amount of data written or -1 if nothing was copied. Unlike the channel |
| 6748 | * function used to replace data, this one can only be called inside a filter, |
| 6749 | * from http_payload callback. So it cannot yield. An exception is returned if |
| 6750 | * it is called from another callback. |
| 6751 | */ |
| 6752 | __LJMP static int hlua_http_msg_set_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6753 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6754 | struct http_msg *msg; |
| 6755 | struct filter *filter; |
| 6756 | struct htx *htx; |
| 6757 | const char *str; |
| 6758 | size_t input, output, sz; |
| 6759 | int offset, len; |
| 6760 | int ret; |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 6761 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6762 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 6763 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 6764 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6765 | |
| 6766 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6767 | WILL_LJMP(lua_error(L)); |
| 6768 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6769 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6770 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6771 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6772 | WILL_LJMP(lua_error(L)); |
| 6773 | |
| 6774 | offset = output; |
| 6775 | if (lua_gettop(L) > 2) { |
| 6776 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6777 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6778 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6779 | offset += output; |
| 6780 | if (offset < output || offset > input + output) { |
| 6781 | lua_pushfstring(L, "offset out of range."); |
| 6782 | WILL_LJMP(lua_error(L)); |
| 6783 | } |
| 6784 | } |
| 6785 | |
| 6786 | len = output + input - offset; |
| 6787 | if (lua_gettop(L) == 4) { |
| 6788 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6789 | if (!len) |
| 6790 | goto set; |
| 6791 | if (len == -1) |
| 6792 | len = output + input - offset; |
| 6793 | if (len < 0 || offset + len > output + input) { |
| 6794 | lua_pushfstring(L, "length out of range."); |
| 6795 | WILL_LJMP(lua_error(L)); |
| 6796 | } |
| 6797 | } |
| 6798 | |
| 6799 | set: |
| 6800 | /* Be sure we can copied the string once input data will be removed. */ |
| 6801 | htx = htx_from_buf(&msg->chn->buf); |
| 6802 | if (sz > htx_free_data_space(htx) + len) |
| 6803 | lua_pushinteger(L, -1); |
| 6804 | else { |
| 6805 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6806 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6807 | lua_pushinteger(L, ret); |
| 6808 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6809 | return 1; |
| 6810 | } |
| 6811 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6812 | /* Prepends data into an HTTP message and forward it, from the filter point of |
| 6813 | * view. It returns the amount of data written or -1 if nothing was sent. Unlike |
| 6814 | * the channel function used to send data, this one can only be called inside a |
| 6815 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6816 | * returned if it is called from another callback. |
| 6817 | */ |
| 6818 | __LJMP static int hlua_http_msg_send(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6819 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6820 | struct http_msg *msg; |
| 6821 | struct filter *filter; |
| 6822 | struct htx *htx; |
| 6823 | const char *str; |
| 6824 | size_t offset, len, sz; |
| 6825 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6826 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6827 | MAY_LJMP(check_args(L, 2, "send")); |
| 6828 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6829 | |
| 6830 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6831 | WILL_LJMP(lua_error(L)); |
| 6832 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6833 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6834 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6835 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6836 | WILL_LJMP(lua_error(L)); |
| 6837 | |
| 6838 | /* Return an error if the channel's output is closed */ |
| 6839 | if (unlikely(channel_output_closed(msg->chn))) { |
| 6840 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6841 | return 1; |
| 6842 | } |
| 6843 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6844 | htx = htx_from_buf(&msg->chn->buf); |
| 6845 | if (sz > htx_free_data_space(htx)) { |
| 6846 | lua_pushinteger(L, -1); |
| 6847 | return 1; |
| 6848 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6849 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6850 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6851 | if (ret > 0) { |
| 6852 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6853 | |
| 6854 | FLT_OFF(filter, msg->chn) += ret; |
| 6855 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6856 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 6857 | } |
| 6858 | |
| 6859 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6860 | return 1; |
| 6861 | } |
| 6862 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6863 | /* Forwards a given amount of bytes. It return -1 if the channel's output is |
| 6864 | * closed. Otherwise, it returns the number of bytes forwarded. Unlike the |
| 6865 | * channel function used to forward data, this one can only be called inside a |
| 6866 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6867 | * returned if it is called from another callback. All other functions deal with |
| 6868 | * DATA block, this one not. |
| 6869 | */ |
| 6870 | __LJMP static int hlua_http_msg_forward(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6871 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6872 | struct http_msg *msg; |
| 6873 | struct filter *filter; |
| 6874 | size_t offset, len; |
| 6875 | int fwd, ret = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6876 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6877 | MAY_LJMP(check_args(L, 2, "forward")); |
| 6878 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6879 | |
| 6880 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6881 | WILL_LJMP(lua_error(L)); |
| 6882 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6883 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6884 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6885 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6886 | WILL_LJMP(lua_error(L)); |
| 6887 | |
| 6888 | /* Nothing to do, just return */ |
| 6889 | if (!fwd) |
| 6890 | goto end; |
| 6891 | |
| 6892 | /* Return an error if the channel's output is closed */ |
| 6893 | if (unlikely(channel_output_closed(msg->chn))) { |
| 6894 | ret = -1; |
| 6895 | goto end; |
| 6896 | } |
| 6897 | |
| 6898 | ret = fwd; |
| 6899 | if (ret > len) |
| 6900 | ret = len; |
| 6901 | |
| 6902 | if (ret) { |
| 6903 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6904 | |
| 6905 | FLT_OFF(filter, msg->chn) += ret; |
| 6906 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 6907 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6908 | } |
| 6909 | |
| 6910 | end: |
| 6911 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6912 | return 1; |
| 6913 | } |
| 6914 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6915 | /* Set EOM flag on the HTX message. |
| 6916 | * |
| 6917 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 6918 | * really know how to do without this feature. |
| 6919 | */ |
| 6920 | __LJMP static int hlua_http_msg_set_eom(lua_State *L) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6921 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6922 | struct http_msg *msg; |
| 6923 | struct htx *htx; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6924 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6925 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 6926 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6927 | htx = htxbuf(&msg->chn->buf); |
| 6928 | htx->flags |= HTX_FL_EOM; |
| 6929 | return 0; |
| 6930 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6931 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6932 | /* Unset EOM flag on the HTX message. |
| 6933 | * |
| 6934 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 6935 | * really know how to do without this feature. |
| 6936 | */ |
| 6937 | __LJMP static int hlua_http_msg_unset_eom(lua_State *L) |
| 6938 | { |
| 6939 | struct http_msg *msg; |
| 6940 | struct htx *htx; |
| 6941 | |
| 6942 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 6943 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6944 | htx = htxbuf(&msg->chn->buf); |
| 6945 | htx->flags &= ~HTX_FL_EOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6946 | return 0; |
| 6947 | } |
| 6948 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6949 | /* |
| 6950 | * |
| 6951 | * |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 6952 | * Class HTTPClient |
| 6953 | * |
| 6954 | * |
| 6955 | */ |
| 6956 | __LJMP static struct hlua_httpclient *hlua_checkhttpclient(lua_State *L, int ud) |
| 6957 | { |
| 6958 | return MAY_LJMP(hlua_checkudata(L, ud, class_httpclient_ref)); |
| 6959 | } |
| 6960 | |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 6961 | |
| 6962 | /* stops the httpclient and ask it to kill itself */ |
| 6963 | __LJMP static int hlua_httpclient_gc(lua_State *L) |
| 6964 | { |
| 6965 | struct hlua_httpclient *hlua_hc; |
| 6966 | |
| 6967 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 6968 | |
| 6969 | hlua_hc = MAY_LJMP(hlua_checkhttpclient(L, 1)); |
| 6970 | |
| 6971 | httpclient_stop_and_destroy(hlua_hc->hc); |
| 6972 | |
| 6973 | hlua_hc->hc = NULL; |
| 6974 | |
| 6975 | |
| 6976 | return 0; |
| 6977 | } |
| 6978 | |
| 6979 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 6980 | __LJMP static int hlua_httpclient_new(lua_State *L) |
| 6981 | { |
| 6982 | struct hlua_httpclient *hlua_hc; |
| 6983 | struct hlua *hlua; |
| 6984 | |
| 6985 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6986 | hlua = hlua_gethlua(L); |
| 6987 | if (!hlua) |
| 6988 | return 0; |
| 6989 | |
| 6990 | /* Check stack size. */ |
| 6991 | if (!lua_checkstack(L, 3)) { |
| 6992 | hlua_pusherror(L, "httpclient: full stack"); |
| 6993 | goto err; |
| 6994 | } |
| 6995 | /* Create the object: obj[0] = userdata. */ |
| 6996 | lua_newtable(L); |
| 6997 | hlua_hc = MAY_LJMP(lua_newuserdata(L, sizeof(*hlua_hc))); |
| 6998 | lua_rawseti(L, -2, 0); |
| 6999 | memset(hlua_hc, 0, sizeof(*hlua_hc)); |
| 7000 | |
| 7001 | hlua_hc->hc = httpclient_new(hlua, 0, IST_NULL); |
| 7002 | if (!hlua_hc->hc) |
| 7003 | goto err; |
| 7004 | |
| 7005 | /* Pop a class stream metatable and affect it to the userdata. */ |
| 7006 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_httpclient_ref); |
| 7007 | lua_setmetatable(L, -2); |
| 7008 | |
| 7009 | return 1; |
| 7010 | |
| 7011 | err: |
| 7012 | WILL_LJMP(lua_error(L)); |
| 7013 | return 0; |
| 7014 | } |
| 7015 | |
| 7016 | |
| 7017 | /* |
| 7018 | * Callback of the httpclient, this callback wakes the lua task up, once the |
| 7019 | * httpclient receives some data |
| 7020 | * |
| 7021 | */ |
| 7022 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7023 | static void hlua_httpclient_cb(struct httpclient *hc) |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7024 | { |
| 7025 | struct hlua *hlua = hc->caller; |
| 7026 | |
| 7027 | if (!hlua || !hlua->task) |
| 7028 | return; |
| 7029 | |
| 7030 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 7031 | } |
| 7032 | |
| 7033 | /* |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 7034 | * Fill the lua stack with headers from the httpclient response |
| 7035 | * This works the same way as the hlua_http_get_headers() function |
| 7036 | */ |
| 7037 | __LJMP static int hlua_httpclient_get_headers(lua_State *L, struct hlua_httpclient *hlua_hc) |
| 7038 | { |
| 7039 | struct http_hdr *hdr; |
| 7040 | |
| 7041 | lua_newtable(L); |
| 7042 | |
William Lallemand | ef574b2 | 2021-10-05 16:19:31 +0200 | [diff] [blame] | 7043 | for (hdr = hlua_hc->hc->res.hdrs; hdr && isttest(hdr->n); hdr++) { |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 7044 | struct ist n, v; |
| 7045 | int len; |
| 7046 | |
| 7047 | n = hdr->n; |
| 7048 | v = hdr->v; |
| 7049 | |
| 7050 | /* Check for existing entry: |
| 7051 | * assume that the table is on the top of the stack, and |
| 7052 | * push the key in the stack, the function lua_gettable() |
| 7053 | * perform the lookup. |
| 7054 | */ |
| 7055 | |
| 7056 | lua_pushlstring(L, n.ptr, n.len); |
| 7057 | lua_gettable(L, -2); |
| 7058 | |
| 7059 | switch (lua_type(L, -1)) { |
| 7060 | case LUA_TNIL: |
| 7061 | /* Table not found, create it. */ |
| 7062 | lua_pop(L, 1); /* remove the nil value. */ |
| 7063 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 7064 | lua_newtable(L); /* create and push empty table. */ |
| 7065 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7066 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 7067 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 7068 | break; |
| 7069 | |
| 7070 | case LUA_TTABLE: |
| 7071 | /* Entry found: push the value in the table. */ |
| 7072 | len = lua_rawlen(L, -1); |
| 7073 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7074 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 7075 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 7076 | break; |
| 7077 | |
| 7078 | default: |
| 7079 | /* Other cases are errors. */ |
| 7080 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 7081 | WILL_LJMP(lua_error(L)); |
| 7082 | } |
| 7083 | } |
| 7084 | return 1; |
| 7085 | } |
| 7086 | |
| 7087 | /* |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7088 | * Allocate and return an array of http_hdr ist extracted from the <headers> lua table |
| 7089 | * |
| 7090 | * Caller must free the result |
| 7091 | */ |
| 7092 | struct http_hdr *hlua_httpclient_table_to_hdrs(lua_State *L) |
| 7093 | { |
| 7094 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
| 7095 | struct http_hdr *result = NULL; |
| 7096 | uint32_t hdr_num = 0; |
| 7097 | |
| 7098 | lua_pushnil(L); |
| 7099 | while (lua_next(L, -2) != 0) { |
| 7100 | struct ist name, value; |
| 7101 | const char *n, *v; |
| 7102 | size_t nlen, vlen; |
| 7103 | |
| 7104 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 7105 | /* Skip element if the key is not a string or if the value is not a table */ |
| 7106 | goto next_hdr; |
| 7107 | } |
| 7108 | |
| 7109 | n = lua_tolstring(L, -2, &nlen); |
| 7110 | name = ist2(n, nlen); |
| 7111 | |
| 7112 | /* Loop on header's values */ |
| 7113 | lua_pushnil(L); |
| 7114 | while (lua_next(L, -2)) { |
| 7115 | if (!lua_isstring(L, -1)) { |
| 7116 | /* Skip the value if it is not a string */ |
| 7117 | goto next_value; |
| 7118 | } |
| 7119 | |
| 7120 | v = lua_tolstring(L, -1, &vlen); |
| 7121 | value = ist2(v, vlen); |
| 7122 | name = ist2(n, nlen); |
| 7123 | |
| 7124 | hdrs[hdr_num].n = istdup(name); |
| 7125 | hdrs[hdr_num].v = istdup(value); |
| 7126 | |
| 7127 | hdr_num++; |
| 7128 | |
| 7129 | next_value: |
| 7130 | lua_pop(L, 1); |
| 7131 | } |
| 7132 | |
| 7133 | next_hdr: |
| 7134 | lua_pop(L, 1); |
| 7135 | |
| 7136 | } |
| 7137 | |
| 7138 | if (hdr_num) { |
| 7139 | /* alloc and copy the headers in the httpclient struct */ |
Tim Duesterhus | 16cc16d | 2021-11-06 15:14:45 +0100 | [diff] [blame] | 7140 | result = calloc((hdr_num + 1), sizeof(*result)); |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7141 | if (!result) |
| 7142 | goto skip_headers; |
| 7143 | memcpy(result, hdrs, sizeof(struct http_hdr) * (hdr_num + 1)); |
| 7144 | |
| 7145 | result[hdr_num].n = IST_NULL; |
| 7146 | result[hdr_num].v = IST_NULL; |
| 7147 | } |
| 7148 | |
| 7149 | skip_headers: |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7150 | |
| 7151 | return result; |
| 7152 | } |
| 7153 | |
| 7154 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7155 | /* |
| 7156 | * For each yield, checks if there is some data in the httpclient and push them |
| 7157 | * in the lua buffer, once the httpclient finished its job, push the result on |
| 7158 | * the stack |
| 7159 | */ |
| 7160 | __LJMP static int hlua_httpclient_rcv_yield(lua_State *L, int status, lua_KContext ctx) |
| 7161 | { |
| 7162 | struct buffer *tr; |
| 7163 | int res; |
| 7164 | struct hlua *hlua = hlua_gethlua(L); |
| 7165 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7166 | |
| 7167 | |
| 7168 | tr = get_trash_chunk(); |
| 7169 | |
| 7170 | res = httpclient_res_xfer(hlua_hc->hc, tr); |
| 7171 | luaL_addlstring(&hlua_hc->b, b_orig(tr), res); |
| 7172 | |
| 7173 | if (!httpclient_data(hlua_hc->hc) && httpclient_ended(hlua_hc->hc)) { |
| 7174 | |
| 7175 | luaL_pushresult(&hlua_hc->b); |
| 7176 | lua_settable(L, -3); |
| 7177 | |
| 7178 | lua_pushstring(L, "status"); |
| 7179 | lua_pushinteger(L, hlua_hc->hc->res.status); |
| 7180 | lua_settable(L, -3); |
| 7181 | |
| 7182 | |
| 7183 | lua_pushstring(L, "reason"); |
| 7184 | lua_pushlstring(L, hlua_hc->hc->res.reason.ptr, hlua_hc->hc->res.reason.len); |
| 7185 | lua_settable(L, -3); |
| 7186 | |
| 7187 | lua_pushstring(L, "headers"); |
| 7188 | hlua_httpclient_get_headers(L, hlua_hc); |
| 7189 | lua_settable(L, -3); |
| 7190 | |
| 7191 | return 1; |
| 7192 | } |
| 7193 | |
| 7194 | if (httpclient_data(hlua_hc->hc)) |
| 7195 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 7196 | |
| 7197 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7198 | |
| 7199 | return 0; |
| 7200 | } |
| 7201 | |
| 7202 | /* |
| 7203 | * Call this when trying to stream a body during a request |
| 7204 | */ |
| 7205 | __LJMP static int hlua_httpclient_snd_yield(lua_State *L, int status, lua_KContext ctx) |
| 7206 | { |
| 7207 | struct hlua *hlua; |
| 7208 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7209 | const char *body_str = NULL; |
| 7210 | int ret; |
| 7211 | int end = 0; |
| 7212 | size_t buf_len; |
| 7213 | size_t to_send = 0; |
| 7214 | |
| 7215 | hlua = hlua_gethlua(L); |
| 7216 | |
| 7217 | if (!hlua || !hlua->task) |
| 7218 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7219 | "'frontend', 'backend' or 'task'")); |
| 7220 | |
| 7221 | ret = lua_getfield(L, -1, "body"); |
| 7222 | if (ret != LUA_TSTRING) |
| 7223 | goto rcv; |
| 7224 | |
| 7225 | body_str = lua_tolstring(L, -1, &buf_len); |
| 7226 | lua_pop(L, 1); |
| 7227 | |
Christopher Faulet | fc59129 | 2022-01-12 14:46:03 +0100 | [diff] [blame] | 7228 | to_send = buf_len - hlua_hc->sent; |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7229 | |
| 7230 | if ((hlua_hc->sent + to_send) >= buf_len) |
| 7231 | end = 1; |
| 7232 | |
| 7233 | /* the end flag is always set since we are using the whole remaining size */ |
| 7234 | hlua_hc->sent += httpclient_req_xfer(hlua_hc->hc, ist2(body_str + hlua_hc->sent, to_send), end); |
| 7235 | |
| 7236 | if (buf_len > hlua_hc->sent) { |
| 7237 | /* still need to process the buffer */ |
| 7238 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_snd_yield, TICK_ETERNITY, 0)); |
| 7239 | } else { |
| 7240 | goto rcv; |
| 7241 | /* we sent the whole request buffer we can recv */ |
| 7242 | } |
| 7243 | return 0; |
| 7244 | |
| 7245 | rcv: |
| 7246 | |
| 7247 | /* we return a "res" object */ |
| 7248 | lua_newtable(L); |
| 7249 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7250 | lua_pushstring(L, "body"); |
William Lallemand | d1187eb | 2021-11-02 10:40:06 +0100 | [diff] [blame] | 7251 | luaL_buffinit(L, &hlua_hc->b); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7252 | |
William Lallemand | 933fe39 | 2021-11-04 09:45:58 +0100 | [diff] [blame] | 7253 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7254 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7255 | |
| 7256 | return 1; |
| 7257 | } |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7258 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7259 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7260 | * Send an HTTP request and wait for a response |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7261 | */ |
| 7262 | |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7263 | __LJMP static int hlua_httpclient_send(lua_State *L, enum http_meth_t meth) |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7264 | { |
| 7265 | struct hlua_httpclient *hlua_hc; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7266 | struct http_hdr *hdrs = NULL; |
| 7267 | struct http_hdr *hdrs_i = NULL; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7268 | struct hlua *hlua; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7269 | const char *url_str = NULL; |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7270 | const char *body_str = NULL; |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7271 | size_t buf_len; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7272 | int ret; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7273 | |
| 7274 | hlua = hlua_gethlua(L); |
| 7275 | |
| 7276 | if (!hlua || !hlua->task) |
| 7277 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7278 | "'frontend', 'backend' or 'task'")); |
| 7279 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7280 | if (lua_gettop(L) != 2 || lua_type(L, -1) != LUA_TTABLE) |
| 7281 | WILL_LJMP(luaL_error(L, "'get' needs a table as argument")); |
| 7282 | |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 7283 | hlua_hc = hlua_checkhttpclient(L, 1); |
| 7284 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7285 | lua_pushnil(L); /* first key */ |
| 7286 | while (lua_next(L, 2)) { |
| 7287 | if (strcmp(lua_tostring(L, -2), "dst") == 0) { |
| 7288 | if (httpclient_set_dst(hlua_hc->hc, lua_tostring(L, -1)) < 0) |
| 7289 | WILL_LJMP(luaL_error(L, "Can't use the 'dst' argument")); |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 7290 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7291 | } else if (strcmp(lua_tostring(L, -2), "url") == 0) { |
| 7292 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7293 | WILL_LJMP(luaL_error(L, "invalid parameter in 'url', must be a string")); |
| 7294 | url_str = lua_tostring(L, -1); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7295 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7296 | } else if (strcmp(lua_tostring(L, -2), "timeout") == 0) { |
| 7297 | if (lua_type(L, -1) != LUA_TNUMBER) |
| 7298 | WILL_LJMP(luaL_error(L, "invalid parameter in 'timeout', must be a number")); |
| 7299 | httpclient_set_timeout(hlua_hc->hc, lua_tointeger(L, -1)); |
William Lallemand | b4a4ef6 | 2022-02-23 14:18:16 +0100 | [diff] [blame] | 7300 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7301 | } else if (strcmp(lua_tostring(L, -2), "headers") == 0) { |
| 7302 | if (lua_type(L, -1) != LUA_TTABLE) |
| 7303 | WILL_LJMP(luaL_error(L, "invalid parameter in 'headers', must be a table")); |
| 7304 | hdrs = hlua_httpclient_table_to_hdrs(L); |
William Lallemand | 79416cb | 2021-09-24 14:51:44 +0200 | [diff] [blame] | 7305 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7306 | } else if (strcmp(lua_tostring(L, -2), "body") == 0) { |
| 7307 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7308 | WILL_LJMP(luaL_error(L, "invalid parameter in 'body', must be a string")); |
| 7309 | body_str = lua_tolstring(L, -1, &buf_len); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7310 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7311 | } else { |
| 7312 | WILL_LJMP(luaL_error(L, "'%s' invalid parameter name", lua_tostring(L, -2))); |
| 7313 | } |
| 7314 | /* removes 'value'; keeps 'key' for next iteration */ |
| 7315 | lua_pop(L, 1); |
| 7316 | } |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7317 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7318 | if (!url_str) { |
| 7319 | WILL_LJMP(luaL_error(L, "'get' need a 'url' argument")); |
| 7320 | return 0; |
| 7321 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7322 | |
William Lallemand | 10a3736 | 2022-03-02 16:18:26 +0100 | [diff] [blame] | 7323 | hlua_hc->sent = 0; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7324 | |
| 7325 | hlua_hc->hc->req.url = istdup(ist(url_str)); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7326 | hlua_hc->hc->req.meth = meth; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7327 | |
| 7328 | /* update the httpclient callbacks */ |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7329 | hlua_hc->hc->ops.res_stline = hlua_httpclient_cb; |
| 7330 | hlua_hc->hc->ops.res_headers = hlua_httpclient_cb; |
| 7331 | hlua_hc->hc->ops.res_payload = hlua_httpclient_cb; |
William Lallemand | 8f170c7 | 2022-03-15 10:52:07 +0100 | [diff] [blame] | 7332 | hlua_hc->hc->ops.res_end = hlua_httpclient_cb; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7333 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7334 | /* a body is available, it will use the request callback */ |
| 7335 | if (body_str) { |
| 7336 | hlua_hc->hc->ops.req_payload = hlua_httpclient_cb; |
| 7337 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7338 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7339 | ret = httpclient_req_gen(hlua_hc->hc, hlua_hc->hc->req.url, meth, hdrs, IST_NULL); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7340 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7341 | /* free the temporary headers array */ |
| 7342 | hdrs_i = hdrs; |
| 7343 | while (hdrs_i && isttest(hdrs_i->n)) { |
| 7344 | istfree(&hdrs_i->n); |
| 7345 | istfree(&hdrs_i->v); |
| 7346 | hdrs_i++; |
| 7347 | } |
| 7348 | ha_free(&hdrs); |
| 7349 | |
| 7350 | |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7351 | if (ret != ERR_NONE) { |
| 7352 | WILL_LJMP(luaL_error(L, "Can't generate the HTTP request")); |
| 7353 | return 0; |
| 7354 | } |
| 7355 | |
William Lallemand | c2d3db4 | 2022-04-26 11:46:13 +0200 | [diff] [blame] | 7356 | if (!httpclient_start(hlua_hc->hc)) |
| 7357 | WILL_LJMP(luaL_error(L, "couldn't start the httpclient")); |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7358 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7359 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_snd_yield, TICK_ETERNITY, 0)); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7360 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7361 | return 0; |
| 7362 | } |
| 7363 | |
| 7364 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7365 | * Sends an HTTP HEAD request and wait for a response |
| 7366 | * |
| 7367 | * httpclient:head(url, headers, payload) |
| 7368 | */ |
| 7369 | __LJMP static int hlua_httpclient_head(lua_State *L) |
| 7370 | { |
| 7371 | return hlua_httpclient_send(L, HTTP_METH_HEAD); |
| 7372 | } |
| 7373 | |
| 7374 | /* |
| 7375 | * Send an HTTP GET request and wait for a response |
| 7376 | * |
| 7377 | * httpclient:get(url, headers, payload) |
| 7378 | */ |
| 7379 | __LJMP static int hlua_httpclient_get(lua_State *L) |
| 7380 | { |
| 7381 | return hlua_httpclient_send(L, HTTP_METH_GET); |
| 7382 | |
| 7383 | } |
| 7384 | |
| 7385 | /* |
| 7386 | * Sends an HTTP PUT request and wait for a response |
| 7387 | * |
| 7388 | * httpclient:put(url, headers, payload) |
| 7389 | */ |
| 7390 | __LJMP static int hlua_httpclient_put(lua_State *L) |
| 7391 | { |
| 7392 | return hlua_httpclient_send(L, HTTP_METH_PUT); |
| 7393 | } |
| 7394 | |
| 7395 | /* |
| 7396 | * Send an HTTP POST request and wait for a response |
| 7397 | * |
| 7398 | * httpclient:post(url, headers, payload) |
| 7399 | */ |
| 7400 | __LJMP static int hlua_httpclient_post(lua_State *L) |
| 7401 | { |
| 7402 | return hlua_httpclient_send(L, HTTP_METH_POST); |
| 7403 | } |
| 7404 | |
| 7405 | |
| 7406 | /* |
| 7407 | * Sends an HTTP DELETE request and wait for a response |
| 7408 | * |
| 7409 | * httpclient:delete(url, headers, payload) |
| 7410 | */ |
| 7411 | __LJMP static int hlua_httpclient_delete(lua_State *L) |
| 7412 | { |
| 7413 | return hlua_httpclient_send(L, HTTP_METH_DELETE); |
| 7414 | } |
| 7415 | |
| 7416 | /* |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7417 | * |
| 7418 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7419 | * Class TXN |
| 7420 | * |
| 7421 | * |
| 7422 | */ |
| 7423 | |
| 7424 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7425 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7426 | */ |
| 7427 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 7428 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7429 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7430 | } |
| 7431 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7432 | __LJMP static int hlua_set_var(lua_State *L) |
| 7433 | { |
| 7434 | struct hlua_txn *htxn; |
| 7435 | const char *name; |
| 7436 | size_t len; |
| 7437 | struct sample smp; |
| 7438 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 7439 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7440 | 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] | 7441 | |
| 7442 | /* It is useles to retrieve the stream, but this function |
| 7443 | * runs only in a stream context. |
| 7444 | */ |
| 7445 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7446 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7447 | |
| 7448 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 7449 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7450 | hlua_lua2smp(L, 3, &smp); |
| 7451 | |
| 7452 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7453 | 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] | 7454 | |
| 7455 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 7456 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 7457 | else |
| 7458 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 7459 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 7460 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7461 | } |
| 7462 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7463 | __LJMP static int hlua_unset_var(lua_State *L) |
| 7464 | { |
| 7465 | struct hlua_txn *htxn; |
| 7466 | const char *name; |
| 7467 | size_t len; |
| 7468 | struct sample smp; |
| 7469 | |
| 7470 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 7471 | |
| 7472 | /* It is useles to retrieve the stream, but this function |
| 7473 | * runs only in a stream context. |
| 7474 | */ |
| 7475 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7476 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7477 | |
| 7478 | /* Unset the variable. */ |
| 7479 | 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] | 7480 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 7481 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7482 | } |
| 7483 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7484 | __LJMP static int hlua_get_var(lua_State *L) |
| 7485 | { |
| 7486 | struct hlua_txn *htxn; |
| 7487 | const char *name; |
| 7488 | size_t len; |
| 7489 | struct sample smp; |
| 7490 | |
| 7491 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 7492 | |
| 7493 | /* It is useles to retrieve the stream, but this function |
| 7494 | * runs only in a stream context. |
| 7495 | */ |
| 7496 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7497 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7498 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7499 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 7500 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7501 | lua_pushnil(L); |
| 7502 | return 1; |
| 7503 | } |
| 7504 | |
| 7505 | return hlua_smp2lua(L, &smp); |
| 7506 | } |
| 7507 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7508 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7509 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7510 | struct hlua *hlua; |
| 7511 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7512 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 7513 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7514 | /* It is useles to retrieve the stream, but this function |
| 7515 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7516 | */ |
| 7517 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7518 | |
| 7519 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7520 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7521 | if (!hlua) |
| 7522 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7523 | |
| 7524 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 7525 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7526 | |
| 7527 | /* Get and store new value. */ |
| 7528 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 7529 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 7530 | |
| 7531 | return 0; |
| 7532 | } |
| 7533 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7534 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7535 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7536 | struct hlua *hlua; |
| 7537 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7538 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 7539 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7540 | /* It is useles to retrieve the stream, but this function |
| 7541 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7542 | */ |
| 7543 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7544 | |
| 7545 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7546 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7547 | if (!hlua) { |
| 7548 | lua_pushnil(L); |
| 7549 | return 1; |
| 7550 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7551 | |
| 7552 | /* Push configuration index in the stack. */ |
| 7553 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 7554 | |
| 7555 | return 1; |
| 7556 | } |
| 7557 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7558 | /* Create stack entry containing a class TXN. This function |
| 7559 | * return 0 if the stack does not contains free slots, |
| 7560 | * otherwise it returns 1. |
| 7561 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7562 | 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] | 7563 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7564 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7565 | |
| 7566 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7567 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7568 | return 0; |
| 7569 | |
| 7570 | /* NOTE: The allocation never fails. The failure |
| 7571 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7572 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7573 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7574 | /* Create the object: obj[0] = userdata. */ |
| 7575 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7576 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7577 | lua_rawseti(L, -2, 0); |
| 7578 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7579 | htxn->s = s; |
| 7580 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 7581 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7582 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7583 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7584 | /* Create the "f" field that contains a list of fetches. */ |
| 7585 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7586 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7587 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7588 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7589 | |
| 7590 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 7591 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7592 | 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] | 7593 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7594 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7595 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7596 | /* Create the "c" field that contains a list of converters. */ |
| 7597 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7598 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7599 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7600 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7601 | |
| 7602 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 7603 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 7604 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7605 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7606 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7607 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7608 | /* Create the "req" field that contains the request channel object. */ |
| 7609 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7610 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7611 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7612 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7613 | |
| 7614 | /* Create the "res" field that contains the response channel object. */ |
| 7615 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7616 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7617 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7618 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7619 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7620 | /* Creates the HTTP object is the current proxy allows http. */ |
| 7621 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 7622 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7623 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7624 | return 0; |
| 7625 | } |
| 7626 | else |
| 7627 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7628 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7629 | |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 7630 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) { |
| 7631 | /* HTTPMessage object are created when a lua TXN is created from |
| 7632 | * a filter context only |
| 7633 | */ |
| 7634 | |
| 7635 | /* Creates the HTTP-Request object is the current proxy allows http. */ |
| 7636 | lua_pushstring(L, "http_req"); |
| 7637 | if (p->mode == PR_MODE_HTTP) { |
| 7638 | if (!hlua_http_msg_new(L, &s->txn->req)) |
| 7639 | return 0; |
| 7640 | } |
| 7641 | else |
| 7642 | lua_pushnil(L); |
| 7643 | lua_rawset(L, -3); |
| 7644 | |
| 7645 | /* Creates the HTTP-Response object is the current proxy allows http. */ |
| 7646 | lua_pushstring(L, "http_res"); |
| 7647 | if (p->mode == PR_MODE_HTTP) { |
| 7648 | if (!hlua_http_msg_new(L, &s->txn->rsp)) |
| 7649 | return 0; |
| 7650 | } |
| 7651 | else |
| 7652 | lua_pushnil(L); |
| 7653 | lua_rawset(L, -3); |
| 7654 | } |
| 7655 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7656 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 7657 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 7658 | lua_setmetatable(L, -2); |
| 7659 | |
| 7660 | return 1; |
| 7661 | } |
| 7662 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7663 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 7664 | { |
| 7665 | const char *msg; |
| 7666 | struct hlua_txn *htxn; |
| 7667 | |
| 7668 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 7669 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7670 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7671 | |
| 7672 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 7673 | return 0; |
| 7674 | } |
| 7675 | |
| 7676 | __LJMP static int hlua_txn_log(lua_State *L) |
| 7677 | { |
| 7678 | int level; |
| 7679 | const char *msg; |
| 7680 | struct hlua_txn *htxn; |
| 7681 | |
| 7682 | MAY_LJMP(check_args(L, 3, "log")); |
| 7683 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7684 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7685 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 7686 | |
| 7687 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 7688 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 7689 | |
| 7690 | hlua_sendlog(htxn->s->be, level, msg); |
| 7691 | return 0; |
| 7692 | } |
| 7693 | |
| 7694 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 7695 | { |
| 7696 | const char *msg; |
| 7697 | struct hlua_txn *htxn; |
| 7698 | |
| 7699 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 7700 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7701 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7702 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 7703 | return 0; |
| 7704 | } |
| 7705 | |
| 7706 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 7707 | { |
| 7708 | const char *msg; |
| 7709 | struct hlua_txn *htxn; |
| 7710 | |
| 7711 | MAY_LJMP(check_args(L, 2, "Info")); |
| 7712 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7713 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7714 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 7715 | return 0; |
| 7716 | } |
| 7717 | |
| 7718 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 7719 | { |
| 7720 | const char *msg; |
| 7721 | struct hlua_txn *htxn; |
| 7722 | |
| 7723 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 7724 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7725 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7726 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 7727 | return 0; |
| 7728 | } |
| 7729 | |
| 7730 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 7731 | { |
| 7732 | const char *msg; |
| 7733 | struct hlua_txn *htxn; |
| 7734 | |
| 7735 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 7736 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7737 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7738 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 7739 | return 0; |
| 7740 | } |
| 7741 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7742 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 7743 | { |
| 7744 | struct hlua_txn *htxn; |
| 7745 | int ll; |
| 7746 | |
| 7747 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 7748 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7749 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7750 | |
| 7751 | if (ll < 0 || ll > 7) |
| 7752 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 7753 | |
| 7754 | htxn->s->logs.level = ll; |
| 7755 | return 0; |
| 7756 | } |
| 7757 | |
| 7758 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 7759 | { |
| 7760 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7761 | int tos; |
| 7762 | |
| 7763 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 7764 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7765 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7766 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 7767 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7768 | return 0; |
| 7769 | } |
| 7770 | |
| 7771 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 7772 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7773 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7774 | int mark; |
| 7775 | |
| 7776 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 7777 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7778 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7779 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 7780 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7781 | return 0; |
| 7782 | } |
| 7783 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 7784 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 7785 | { |
| 7786 | struct hlua_txn *htxn; |
| 7787 | |
| 7788 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 7789 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7790 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 7791 | return 0; |
| 7792 | } |
| 7793 | |
| 7794 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 7795 | { |
| 7796 | struct hlua_txn *htxn; |
| 7797 | |
| 7798 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 7799 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7800 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 7801 | return 0; |
| 7802 | } |
| 7803 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7804 | /* 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] | 7805 | * 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] | 7806 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 7807 | * error. The Reply must be on top of the stack. |
| 7808 | */ |
| 7809 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 7810 | { |
| 7811 | struct htx *htx; |
| 7812 | struct htx_sl *sl; |
| 7813 | struct h1m h1m; |
| 7814 | const char *status, *reason, *body; |
| 7815 | size_t status_len, reason_len, body_len; |
| 7816 | int ret, code, flags; |
| 7817 | |
| 7818 | code = 200; |
| 7819 | status = "200"; |
| 7820 | status_len = 3; |
| 7821 | ret = lua_getfield(L, -1, "status"); |
| 7822 | if (ret == LUA_TNUMBER) { |
| 7823 | code = lua_tointeger(L, -1); |
| 7824 | status = lua_tolstring(L, -1, &status_len); |
| 7825 | } |
| 7826 | lua_pop(L, 1); |
| 7827 | |
| 7828 | reason = http_get_reason(code); |
| 7829 | reason_len = strlen(reason); |
| 7830 | ret = lua_getfield(L, -1, "reason"); |
| 7831 | if (ret == LUA_TSTRING) |
| 7832 | reason = lua_tolstring(L, -1, &reason_len); |
| 7833 | lua_pop(L, 1); |
| 7834 | |
| 7835 | body = NULL; |
| 7836 | body_len = 0; |
| 7837 | ret = lua_getfield(L, -1, "body"); |
| 7838 | if (ret == LUA_TSTRING) |
| 7839 | body = lua_tolstring(L, -1, &body_len); |
| 7840 | lua_pop(L, 1); |
| 7841 | |
| 7842 | /* Prepare the response before inserting the headers */ |
| 7843 | h1m_init_res(&h1m); |
| 7844 | htx = htx_from_buf(&s->res.buf); |
| 7845 | channel_htx_truncate(&s->res, htx); |
| 7846 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 7847 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 7848 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 7849 | ist2(status, status_len), ist2(reason, reason_len)); |
| 7850 | } |
| 7851 | else { |
| 7852 | flags = HTX_SL_F_IS_RESP; |
| 7853 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 7854 | ist2(status, status_len), ist2(reason, reason_len)); |
| 7855 | } |
| 7856 | if (!sl) |
| 7857 | goto fail; |
| 7858 | sl->info.res.status = code; |
| 7859 | |
| 7860 | /* Push in the stack the "headers" entry. */ |
| 7861 | ret = lua_getfield(L, -1, "headers"); |
| 7862 | if (ret != LUA_TTABLE) |
| 7863 | goto skip_headers; |
| 7864 | |
| 7865 | lua_pushnil(L); |
| 7866 | while (lua_next(L, -2) != 0) { |
| 7867 | struct ist name, value; |
| 7868 | const char *n, *v; |
| 7869 | size_t nlen, vlen; |
| 7870 | |
| 7871 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 7872 | /* Skip element if the key is not a string or if the value is not a table */ |
| 7873 | goto next_hdr; |
| 7874 | } |
| 7875 | |
| 7876 | n = lua_tolstring(L, -2, &nlen); |
| 7877 | name = ist2(n, nlen); |
| 7878 | if (isteqi(name, ist("content-length"))) { |
| 7879 | /* Always skip content-length header. It will be added |
| 7880 | * later with the correct len |
| 7881 | */ |
| 7882 | goto next_hdr; |
| 7883 | } |
| 7884 | |
| 7885 | /* Loop on header's values */ |
| 7886 | lua_pushnil(L); |
| 7887 | while (lua_next(L, -2)) { |
| 7888 | if (!lua_isstring(L, -1)) { |
| 7889 | /* Skip the value if it is not a string */ |
| 7890 | goto next_value; |
| 7891 | } |
| 7892 | |
| 7893 | v = lua_tolstring(L, -1, &vlen); |
| 7894 | value = ist2(v, vlen); |
| 7895 | |
| 7896 | if (isteqi(name, ist("transfer-encoding"))) |
| 7897 | h1_parse_xfer_enc_header(&h1m, value); |
| 7898 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 7899 | goto fail; |
| 7900 | |
| 7901 | next_value: |
| 7902 | lua_pop(L, 1); |
| 7903 | } |
| 7904 | |
| 7905 | next_hdr: |
| 7906 | lua_pop(L, 1); |
| 7907 | } |
| 7908 | skip_headers: |
| 7909 | lua_pop(L, 1); |
| 7910 | |
| 7911 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 7912 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 7913 | const char *clen = ultoa(body_len); |
| 7914 | |
| 7915 | h1m.flags |= H1_MF_CLEN; |
| 7916 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 7917 | goto fail; |
| 7918 | } |
| 7919 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 7920 | h1m.flags |= H1_MF_XFER_LEN; |
| 7921 | |
| 7922 | /* Update HTX start-line flags */ |
| 7923 | if (h1m.flags & H1_MF_XFER_ENC) |
| 7924 | flags |= HTX_SL_F_XFER_ENC; |
| 7925 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 7926 | flags |= HTX_SL_F_XFER_LEN; |
| 7927 | if (h1m.flags & H1_MF_CHNK) |
| 7928 | flags |= HTX_SL_F_CHNK; |
| 7929 | else if (h1m.flags & H1_MF_CLEN) |
| 7930 | flags |= HTX_SL_F_CLEN; |
| 7931 | if (h1m.body_len == 0) |
| 7932 | flags |= HTX_SL_F_BODYLESS; |
| 7933 | } |
| 7934 | sl->flags |= flags; |
| 7935 | |
| 7936 | |
| 7937 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7938 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7939 | goto fail; |
| 7940 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7941 | htx->flags |= HTX_FL_EOM; |
| 7942 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7943 | /* Now, forward the response and terminate the transaction */ |
| 7944 | s->txn->status = code; |
| 7945 | htx_to_buf(htx, &s->res.buf); |
| 7946 | if (!http_forward_proxy_resp(s, 1)) |
| 7947 | goto fail; |
| 7948 | |
| 7949 | return 1; |
| 7950 | |
| 7951 | fail: |
| 7952 | channel_htx_truncate(&s->res, htx); |
| 7953 | return 0; |
| 7954 | } |
| 7955 | |
| 7956 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 7957 | * processing is just aborted. Nothing is returned to the client and all |
| 7958 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 7959 | * is forwarded to the client before terminating the transaction. On success, |
| 7960 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 7961 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 7962 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7963 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7964 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7965 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 7966 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7967 | struct stream *s; |
| 7968 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7969 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 7970 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7971 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7972 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 7973 | * just end the execution of the current lua code. */ |
| 7974 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7975 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7976 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7977 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 7978 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7979 | struct channel *req = &s->req; |
| 7980 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 7981 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7982 | channel_auto_read(req); |
| 7983 | channel_abort(req); |
| 7984 | channel_auto_close(req); |
| 7985 | channel_erase(req); |
| 7986 | |
| 7987 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 7988 | channel_auto_read(res); |
| 7989 | channel_auto_close(res); |
| 7990 | channel_shutr_now(res); |
| 7991 | |
| 7992 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 7993 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 7994 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 7995 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7996 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 7997 | /* No reply or invalid reply */ |
| 7998 | s->txn->status = 0; |
| 7999 | http_reply_and_close(s, 0, NULL); |
| 8000 | } |
| 8001 | else { |
| 8002 | /* Remove extra args to have the reply on top of the stack */ |
| 8003 | if (lua_gettop(L) > 2) |
| 8004 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8005 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8006 | if (!hlua_txn_forward_reply(L, s)) { |
| 8007 | if (!(s->flags & SF_ERR_MASK)) |
| 8008 | s->flags |= SF_ERR_PRXCOND; |
| 8009 | lua_pushinteger(L, ACT_RET_ERR); |
| 8010 | WILL_LJMP(hlua_done(L)); |
| 8011 | return 0; /* Never reached */ |
| 8012 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 8013 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8014 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8015 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 8016 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 8017 | /* let's log the request time */ |
| 8018 | s->logs.tv_request = now; |
| 8019 | 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] | 8020 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8021 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8022 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8023 | done: |
| 8024 | if (!(s->flags & SF_ERR_MASK)) |
| 8025 | s->flags |= SF_ERR_LOCAL; |
| 8026 | if (!(s->flags & SF_FINST_MASK)) |
| 8027 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8028 | |
Christopher Faulet | e48d1dc | 2021-08-13 14:11:17 +0200 | [diff] [blame] | 8029 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) |
| 8030 | lua_pushinteger(L, -1); |
| 8031 | else |
| 8032 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8033 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8034 | return 0; |
| 8035 | } |
| 8036 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8037 | /* |
| 8038 | * |
| 8039 | * |
| 8040 | * Class REPLY |
| 8041 | * |
| 8042 | * |
| 8043 | */ |
| 8044 | |
| 8045 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 8046 | * free slots, the function fails and returns 0; |
| 8047 | */ |
| 8048 | static int hlua_txn_reply_new(lua_State *L) |
| 8049 | { |
| 8050 | struct hlua_txn *htxn; |
| 8051 | const char *reason, *body = NULL; |
| 8052 | int ret, status; |
| 8053 | |
| 8054 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8055 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8056 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 8057 | WILL_LJMP(lua_error(L)); |
| 8058 | } |
| 8059 | |
| 8060 | /* Default value */ |
| 8061 | status = 200; |
| 8062 | reason = http_get_reason(status); |
| 8063 | |
| 8064 | if (lua_istable(L, 2)) { |
| 8065 | /* load status and reason from the table argument at index 2 */ |
| 8066 | ret = lua_getfield(L, 2, "status"); |
| 8067 | if (ret == LUA_TNIL) |
| 8068 | goto reason; |
| 8069 | else if (ret != LUA_TNUMBER) { |
| 8070 | /* invalid status: ignore the reason */ |
| 8071 | goto body; |
| 8072 | } |
| 8073 | status = lua_tointeger(L, -1); |
| 8074 | |
| 8075 | reason: |
| 8076 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 8077 | ret = lua_getfield(L, 2, "reason"); |
| 8078 | if (ret == LUA_TSTRING) |
| 8079 | reason = lua_tostring(L, -1); |
| 8080 | |
| 8081 | body: |
| 8082 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 8083 | ret = lua_getfield(L, 2, "body"); |
| 8084 | if (ret == LUA_TSTRING) |
| 8085 | body = lua_tostring(L, -1); |
| 8086 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 8087 | } |
| 8088 | |
| 8089 | /* Create the Reply table */ |
| 8090 | lua_newtable(L); |
| 8091 | |
| 8092 | /* Add status element */ |
| 8093 | lua_pushstring(L, "status"); |
| 8094 | lua_pushinteger(L, status); |
| 8095 | lua_settable(L, -3); |
| 8096 | |
| 8097 | /* Add reason element */ |
| 8098 | reason = http_get_reason(status); |
| 8099 | lua_pushstring(L, "reason"); |
| 8100 | lua_pushstring(L, reason); |
| 8101 | lua_settable(L, -3); |
| 8102 | |
| 8103 | /* Add body element, nil if undefined */ |
| 8104 | lua_pushstring(L, "body"); |
| 8105 | if (body) |
| 8106 | lua_pushstring(L, body); |
| 8107 | else |
| 8108 | lua_pushnil(L); |
| 8109 | lua_settable(L, -3); |
| 8110 | |
| 8111 | /* Add headers element */ |
| 8112 | lua_pushstring(L, "headers"); |
| 8113 | lua_newtable(L); |
| 8114 | |
| 8115 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8116 | if (lua_istable(L, 2)) { |
| 8117 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 8118 | ret = lua_getfield(L, 2, "headers"); |
| 8119 | if (ret == LUA_TTABLE) { |
| 8120 | /* stack: [ ... <headers:table>, <table> ] */ |
| 8121 | lua_pushnil(L); |
| 8122 | while (lua_next(L, -2) != 0) { |
| 8123 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 8124 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 8125 | /* invalid value type, skip it */ |
| 8126 | lua_pop(L, 1); |
| 8127 | continue; |
| 8128 | } |
| 8129 | |
| 8130 | |
| 8131 | /* Duplicate the key and swap it with the value. */ |
| 8132 | lua_pushvalue(L, -2); |
| 8133 | lua_insert(L, -2); |
| 8134 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 8135 | |
| 8136 | lua_newtable(L); |
| 8137 | lua_insert(L, -2); |
| 8138 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 8139 | |
| 8140 | if (lua_isstring(L, -1)) { |
| 8141 | /* push the value in the inner table */ |
| 8142 | lua_rawseti(L, -2, 1); |
| 8143 | } |
| 8144 | else { /* table */ |
| 8145 | lua_pushnil(L); |
| 8146 | while (lua_next(L, -2) != 0) { |
| 8147 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 8148 | if (!lua_isstring(L, -1)) { |
| 8149 | /* invalid value type, skip it*/ |
| 8150 | lua_pop(L, 1); |
| 8151 | continue; |
| 8152 | } |
| 8153 | /* push the value in the inner table */ |
| 8154 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 8155 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 8156 | } |
| 8157 | lua_pop(L, 1); |
| 8158 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 8159 | } |
| 8160 | |
| 8161 | /* push (k,v) on the stack in the headers table: |
| 8162 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 8163 | */ |
| 8164 | lua_settable(L, -5); |
| 8165 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 8166 | } |
| 8167 | } |
| 8168 | lua_pop(L, 1); |
| 8169 | } |
| 8170 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8171 | lua_settable(L, -3); |
| 8172 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 8173 | |
| 8174 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 8175 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 8176 | lua_setmetatable(L, -2); |
| 8177 | return 1; |
| 8178 | } |
| 8179 | |
| 8180 | /* Set the reply status code, and optionally the reason. If no reason is |
| 8181 | * provided, the default one corresponding to the status code is used. |
| 8182 | */ |
| 8183 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 8184 | { |
| 8185 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 8186 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 8187 | |
| 8188 | /* First argument (self) must be a table */ |
| 8189 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8190 | |
| 8191 | if (status < 100 || status > 599) { |
| 8192 | lua_pushboolean(L, 0); |
| 8193 | return 1; |
| 8194 | } |
| 8195 | if (!reason) |
| 8196 | reason = http_get_reason(status); |
| 8197 | |
| 8198 | lua_pushinteger(L, status); |
| 8199 | lua_setfield(L, 1, "status"); |
| 8200 | |
| 8201 | lua_pushstring(L, reason); |
| 8202 | lua_setfield(L, 1, "reason"); |
| 8203 | |
| 8204 | lua_pushboolean(L, 1); |
| 8205 | return 1; |
| 8206 | } |
| 8207 | |
| 8208 | /* Add a header into the reply object. Each header name is associated to an |
| 8209 | * array of values in the "headers" table. If the header name is not found, a |
| 8210 | * new entry is created. |
| 8211 | */ |
| 8212 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 8213 | { |
| 8214 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8215 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 8216 | int ret; |
| 8217 | |
| 8218 | /* First argument (self) must be a table */ |
| 8219 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8220 | |
| 8221 | /* Push in the stack the "headers" entry. */ |
| 8222 | ret = lua_getfield(L, 1, "headers"); |
| 8223 | if (ret != LUA_TTABLE) { |
| 8224 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 8225 | WILL_LJMP(lua_error(L)); |
| 8226 | } |
| 8227 | |
| 8228 | /* check if the header is already registered. If not, register it. */ |
| 8229 | ret = lua_getfield(L, -1, name); |
| 8230 | if (ret == LUA_TNIL) { |
| 8231 | /* Entry not found. */ |
| 8232 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 8233 | |
| 8234 | /* Insert the new header name in the array in the top of the stack. |
| 8235 | * It left the new array in the top of the stack. |
| 8236 | */ |
| 8237 | lua_newtable(L); |
| 8238 | lua_pushstring(L, name); |
| 8239 | lua_pushvalue(L, -2); |
| 8240 | lua_settable(L, -4); |
| 8241 | } |
| 8242 | else if (ret != LUA_TTABLE) { |
| 8243 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 8244 | WILL_LJMP(lua_error(L)); |
| 8245 | } |
| 8246 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8247 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8248 | * the header value as new entry. |
| 8249 | */ |
| 8250 | lua_pushstring(L, value); |
| 8251 | ret = lua_rawlen(L, -2); |
| 8252 | lua_rawseti(L, -2, ret + 1); |
| 8253 | |
| 8254 | lua_pushboolean(L, 1); |
| 8255 | return 1; |
| 8256 | } |
| 8257 | |
| 8258 | /* Remove all occurrences of a given header name. */ |
| 8259 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 8260 | { |
| 8261 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8262 | int ret; |
| 8263 | |
| 8264 | /* First argument (self) must be a table */ |
| 8265 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8266 | |
| 8267 | /* Push in the stack the "headers" entry. */ |
| 8268 | ret = lua_getfield(L, 1, "headers"); |
| 8269 | if (ret != LUA_TTABLE) { |
| 8270 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 8271 | WILL_LJMP(lua_error(L)); |
| 8272 | } |
| 8273 | |
| 8274 | lua_pushstring(L, name); |
| 8275 | lua_pushnil(L); |
| 8276 | lua_settable(L, -3); |
| 8277 | |
| 8278 | lua_pushboolean(L, 1); |
| 8279 | return 1; |
| 8280 | } |
| 8281 | |
| 8282 | /* Set the reply's body. Overwrite any existing entry. */ |
| 8283 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 8284 | { |
| 8285 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8286 | |
| 8287 | /* First argument (self) must be a table */ |
| 8288 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8289 | |
| 8290 | lua_pushstring(L, payload); |
| 8291 | lua_setfield(L, 1, "body"); |
| 8292 | |
| 8293 | lua_pushboolean(L, 1); |
| 8294 | return 1; |
| 8295 | } |
| 8296 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8297 | __LJMP static int hlua_log(lua_State *L) |
| 8298 | { |
| 8299 | int level; |
| 8300 | const char *msg; |
| 8301 | |
| 8302 | MAY_LJMP(check_args(L, 2, "log")); |
| 8303 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 8304 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8305 | |
| 8306 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 8307 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 8308 | |
| 8309 | hlua_sendlog(NULL, level, msg); |
| 8310 | return 0; |
| 8311 | } |
| 8312 | |
| 8313 | __LJMP static int hlua_log_debug(lua_State *L) |
| 8314 | { |
| 8315 | const char *msg; |
| 8316 | |
| 8317 | MAY_LJMP(check_args(L, 1, "debug")); |
| 8318 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8319 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 8320 | return 0; |
| 8321 | } |
| 8322 | |
| 8323 | __LJMP static int hlua_log_info(lua_State *L) |
| 8324 | { |
| 8325 | const char *msg; |
| 8326 | |
| 8327 | MAY_LJMP(check_args(L, 1, "info")); |
| 8328 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8329 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 8330 | return 0; |
| 8331 | } |
| 8332 | |
| 8333 | __LJMP static int hlua_log_warning(lua_State *L) |
| 8334 | { |
| 8335 | const char *msg; |
| 8336 | |
| 8337 | MAY_LJMP(check_args(L, 1, "warning")); |
| 8338 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8339 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 8340 | return 0; |
| 8341 | } |
| 8342 | |
| 8343 | __LJMP static int hlua_log_alert(lua_State *L) |
| 8344 | { |
| 8345 | const char *msg; |
| 8346 | |
| 8347 | MAY_LJMP(check_args(L, 1, "alert")); |
| 8348 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8349 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8350 | return 0; |
| 8351 | } |
| 8352 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8353 | __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] | 8354 | { |
| 8355 | int wakeup_ms = lua_tointeger(L, -1); |
Willy Tarreau | 12c0270 | 2021-09-30 16:12:31 +0200 | [diff] [blame] | 8356 | if (!tick_is_expired(wakeup_ms, now_ms)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8357 | 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] | 8358 | return 0; |
| 8359 | } |
| 8360 | |
| 8361 | __LJMP static int hlua_sleep(lua_State *L) |
| 8362 | { |
| 8363 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8364 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8365 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8366 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8367 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8368 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8369 | wakeup_ms = tick_add(now_ms, delay); |
| 8370 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8371 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8372 | 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] | 8373 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8374 | } |
| 8375 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8376 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8377 | { |
| 8378 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8379 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8380 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8381 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8382 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8383 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8384 | wakeup_ms = tick_add(now_ms, delay); |
| 8385 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8386 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8387 | 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] | 8388 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8389 | } |
| 8390 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8391 | /* This functionis an LUA binding. it permits to give back |
| 8392 | * the hand at the HAProxy scheduler. It is used when the |
| 8393 | * LUA processing consumes a lot of time. |
| 8394 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8395 | __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] | 8396 | { |
| 8397 | return 0; |
| 8398 | } |
| 8399 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8400 | __LJMP static int hlua_yield(lua_State *L) |
| 8401 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8402 | 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] | 8403 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8404 | } |
| 8405 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8406 | /* This function change the nice of the currently executed |
| 8407 | * task. It is used set low or high priority at the current |
| 8408 | * task. |
| 8409 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8410 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8411 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8412 | struct hlua *hlua; |
| 8413 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8414 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8415 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8416 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8417 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8418 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 8419 | hlua = hlua_gethlua(L); |
| 8420 | |
| 8421 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8422 | if (!hlua || !hlua->task) |
| 8423 | return 0; |
| 8424 | |
| 8425 | if (nice < -1024) |
| 8426 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8427 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8428 | nice = 1024; |
| 8429 | |
| 8430 | hlua->task->nice = nice; |
| 8431 | return 0; |
| 8432 | } |
| 8433 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8434 | /* 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] | 8435 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 8436 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 8437 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8438 | * |
| 8439 | * Task wrapper are longjmp safe because the only one Lua code |
| 8440 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8441 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 8442 | 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] | 8443 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 8444 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8445 | enum hlua_exec status; |
| 8446 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 8447 | if (task->thread_mask == MAX_THREADS_MASK) |
| 8448 | task_set_affinity(task, tid_bit); |
| 8449 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8450 | /* If it is the first call to the task, we must initialize the |
| 8451 | * execution timeouts. |
| 8452 | */ |
| 8453 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 8454 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8455 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8456 | /* Execute the Lua code. */ |
| 8457 | status = hlua_ctx_resume(hlua, 1); |
| 8458 | |
| 8459 | switch (status) { |
| 8460 | /* finished or yield */ |
| 8461 | case HLUA_E_OK: |
| 8462 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8463 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 8464 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8465 | break; |
| 8466 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 8467 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 8468 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 8469 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8470 | break; |
| 8471 | |
| 8472 | /* finished with error. */ |
| 8473 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8474 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8475 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8476 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8477 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8478 | break; |
| 8479 | |
| 8480 | case HLUA_E_ERR: |
| 8481 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8482 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8483 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8484 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8485 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8486 | break; |
| 8487 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8488 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8489 | } |
| 8490 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8491 | /* This function is an LUA binding that register LUA function to be |
| 8492 | * executed after the HAProxy configuration parsing and before the |
| 8493 | * HAProxy scheduler starts. This function expect only one LUA |
| 8494 | * argument that is a function. This function returns nothing, but |
| 8495 | * throws if an error is encountered. |
| 8496 | */ |
| 8497 | __LJMP static int hlua_register_init(lua_State *L) |
| 8498 | { |
| 8499 | struct hlua_init_function *init; |
| 8500 | int ref; |
| 8501 | |
| 8502 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 8503 | |
| 8504 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8505 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8506 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8507 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8508 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8509 | |
| 8510 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 8511 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8512 | return 0; |
| 8513 | } |
| 8514 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8515 | /* This functio is an LUA binding. It permits to register a task |
| 8516 | * executed in parallel of the main HAroxy activity. The task is |
| 8517 | * created and it is set in the HAProxy scheduler. It can be called |
| 8518 | * from the "init" section, "post init" or during the runtime. |
| 8519 | * |
| 8520 | * Lua prototype: |
| 8521 | * |
| 8522 | * <none> core.register_task(<function>) |
| 8523 | */ |
| 8524 | static int hlua_register_task(lua_State *L) |
| 8525 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8526 | struct hlua *hlua = NULL; |
| 8527 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8528 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8529 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8530 | |
| 8531 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 8532 | |
| 8533 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8534 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8535 | /* Get the reference state. If the reference is NULL, L is the master |
| 8536 | * state, otherwise hlua->T is. |
| 8537 | */ |
| 8538 | hlua = hlua_gethlua(L); |
| 8539 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8540 | /* we are in runtime processing */ |
| 8541 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8542 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8543 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8544 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8545 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8546 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8547 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8548 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8549 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8550 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8551 | /* We are in the common lua state, execute the task anywhere, |
| 8552 | * otherwise, inherit the current thread identifier |
| 8553 | */ |
| 8554 | if (state_id == 0) |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8555 | task = task_new_anywhere(); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8556 | else |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8557 | task = task_new_here(); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8558 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8559 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8560 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8561 | task->context = hlua; |
| 8562 | task->process = hlua_process_task; |
| 8563 | |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8564 | if (!hlua_ctx_init(hlua, state_id, task, 1)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8565 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8566 | |
| 8567 | /* Restore the function in the stack. */ |
| 8568 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 8569 | hlua->nargs = 0; |
| 8570 | |
| 8571 | /* Schedule task. */ |
Willy Tarreau | e3957f8 | 2021-09-30 16:17:37 +0200 | [diff] [blame] | 8572 | task_wakeup(task, TASK_WOKEN_INIT); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8573 | |
| 8574 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8575 | |
| 8576 | alloc_error: |
| 8577 | task_destroy(task); |
| 8578 | hlua_ctx_destroy(hlua); |
| 8579 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8580 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8581 | } |
| 8582 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8583 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 8584 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 8585 | * resume converters. |
| 8586 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8587 | 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] | 8588 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8589 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8590 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8591 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8592 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8593 | if (!stream) |
| 8594 | return 0; |
| 8595 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8596 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8597 | * Lua context can be not initialized. This behavior |
| 8598 | * permits to save performances because a systematic |
| 8599 | * Lua initialization cause 5% performances loss. |
| 8600 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8601 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8602 | struct hlua *hlua; |
| 8603 | |
| 8604 | hlua = pool_alloc(pool_head_hlua); |
| 8605 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8606 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8607 | return 0; |
| 8608 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8609 | HLUA_INIT(hlua); |
| 8610 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8611 | 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] | 8612 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8613 | return 0; |
| 8614 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8615 | } |
| 8616 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8617 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8618 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8619 | |
| 8620 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8621 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8622 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8623 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8624 | else |
| 8625 | error = "critical error"; |
| 8626 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8627 | return 0; |
| 8628 | } |
| 8629 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8630 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8631 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8632 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8633 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8634 | return 0; |
| 8635 | } |
| 8636 | |
| 8637 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8638 | 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] | 8639 | |
| 8640 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8641 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8642 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8643 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8644 | return 0; |
| 8645 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8646 | hlua_smp2lua(stream->hlua->T, smp); |
| 8647 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8648 | |
| 8649 | /* push keywords in the stack. */ |
| 8650 | if (arg_p) { |
| 8651 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8652 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8653 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8654 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8655 | return 0; |
| 8656 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8657 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8658 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8659 | } |
| 8660 | } |
| 8661 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8662 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8663 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8664 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8665 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8666 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8667 | } |
| 8668 | |
| 8669 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8670 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8671 | /* finished. */ |
| 8672 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8673 | /* If the stack is empty, the function fails. */ |
| 8674 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8675 | return 0; |
| 8676 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8677 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8678 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8679 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8680 | return 1; |
| 8681 | |
| 8682 | /* yield. */ |
| 8683 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8684 | 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] | 8685 | return 0; |
| 8686 | |
| 8687 | /* finished with error. */ |
| 8688 | case HLUA_E_ERRMSG: |
| 8689 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8690 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8691 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8692 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8693 | return 0; |
| 8694 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8695 | case HLUA_E_ETMOUT: |
| 8696 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 8697 | return 0; |
| 8698 | |
| 8699 | case HLUA_E_NOMEM: |
| 8700 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 8701 | return 0; |
| 8702 | |
| 8703 | case HLUA_E_YIELD: |
| 8704 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 8705 | return 0; |
| 8706 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8707 | case HLUA_E_ERR: |
| 8708 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8709 | 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] | 8710 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8711 | |
| 8712 | default: |
| 8713 | return 0; |
| 8714 | } |
| 8715 | } |
| 8716 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8717 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 8718 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8719 | * resume sample-fetches. This function will be called by the sample |
| 8720 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8721 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8722 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 8723 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8724 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8725 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8726 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8727 | const char *error; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 8728 | unsigned int hflags = HLUA_TXN_NOTERM | HLUA_TXN_SMP_CTX; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8729 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8730 | if (!stream) |
| 8731 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8732 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8733 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8734 | * Lua context can be not initialized. This behavior |
| 8735 | * permits to save performances because a systematic |
| 8736 | * Lua initialization cause 5% performances loss. |
| 8737 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8738 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8739 | struct hlua *hlua; |
| 8740 | |
| 8741 | hlua = pool_alloc(pool_head_hlua); |
| 8742 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8743 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8744 | return 0; |
| 8745 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8746 | hlua->T = NULL; |
| 8747 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8748 | 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] | 8749 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8750 | return 0; |
| 8751 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8752 | } |
| 8753 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8754 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8755 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8756 | |
| 8757 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8758 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8759 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8760 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8761 | else |
| 8762 | error = "critical error"; |
| 8763 | 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] | 8764 | return 0; |
| 8765 | } |
| 8766 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8767 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8768 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8769 | 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] | 8770 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8771 | return 0; |
| 8772 | } |
| 8773 | |
| 8774 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8775 | 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] | 8776 | |
| 8777 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 8778 | 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] | 8779 | 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] | 8780 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8781 | return 0; |
| 8782 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8783 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8784 | |
| 8785 | /* push keywords in the stack. */ |
| 8786 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 8787 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8788 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8789 | 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] | 8790 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8791 | return 0; |
| 8792 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8793 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8794 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8795 | } |
| 8796 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8797 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8798 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8799 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8800 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8801 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8802 | } |
| 8803 | |
| 8804 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8805 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8806 | /* finished. */ |
| 8807 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8808 | /* If the stack is empty, the function fails. */ |
| 8809 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8810 | return 0; |
| 8811 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8812 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8813 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8814 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8815 | |
| 8816 | /* Set the end of execution flag. */ |
| 8817 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 8818 | return 1; |
| 8819 | |
| 8820 | /* yield. */ |
| 8821 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8822 | 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] | 8823 | return 0; |
| 8824 | |
| 8825 | /* finished with error. */ |
| 8826 | case HLUA_E_ERRMSG: |
| 8827 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8828 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8829 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8830 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8831 | return 0; |
| 8832 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8833 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8834 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 8835 | return 0; |
| 8836 | |
| 8837 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8838 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 8839 | return 0; |
| 8840 | |
| 8841 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8842 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 8843 | return 0; |
| 8844 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8845 | case HLUA_E_ERR: |
| 8846 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8847 | 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] | 8848 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8849 | |
| 8850 | default: |
| 8851 | return 0; |
| 8852 | } |
| 8853 | } |
| 8854 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8855 | /* This function is an LUA binding used for registering |
| 8856 | * "sample-conv" functions. It expects a converter name used |
| 8857 | * in the haproxy configuration file, and an LUA function. |
| 8858 | */ |
| 8859 | __LJMP static int hlua_register_converters(lua_State *L) |
| 8860 | { |
| 8861 | struct sample_conv_kw_list *sck; |
| 8862 | const char *name; |
| 8863 | int ref; |
| 8864 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8865 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8866 | struct sample_conv *sc; |
| 8867 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8868 | |
| 8869 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 8870 | |
| 8871 | /* First argument : converter name. */ |
| 8872 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8873 | |
| 8874 | /* Second argument : lua function. */ |
| 8875 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 8876 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8877 | /* Check if the converter is already registered */ |
| 8878 | trash = get_trash_chunk(); |
| 8879 | chunk_printf(trash, "lua.%s", name); |
| 8880 | sc = find_sample_conv(trash->area, trash->data); |
| 8881 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8882 | fcn = sc->private; |
| 8883 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8884 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 8885 | "This will become a hard error in version 2.5.\n", name); |
| 8886 | } |
| 8887 | fcn->function_ref[hlua_state_id] = ref; |
| 8888 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8889 | } |
| 8890 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8891 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8892 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8893 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8894 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8895 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8896 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8897 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8898 | |
| 8899 | /* Fill fcn. */ |
| 8900 | fcn->name = strdup(name); |
| 8901 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8902 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8903 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8904 | |
| 8905 | /* List head */ |
| 8906 | sck->list.n = sck->list.p = NULL; |
| 8907 | |
| 8908 | /* converter keyword. */ |
| 8909 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8910 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8911 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8912 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8913 | |
| 8914 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 8915 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 8916 | 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] | 8917 | sck->kw[0].val_args = NULL; |
| 8918 | sck->kw[0].in_type = SMP_T_STR; |
| 8919 | sck->kw[0].out_type = SMP_T_STR; |
| 8920 | sck->kw[0].private = fcn; |
| 8921 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8922 | /* Register this new converter */ |
| 8923 | sample_register_convs(sck); |
| 8924 | |
| 8925 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8926 | |
| 8927 | alloc_error: |
| 8928 | release_hlua_function(fcn); |
| 8929 | ha_free(&sck); |
| 8930 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8931 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8932 | } |
| 8933 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8934 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8935 | * "sample-fetch" functions. It expects a converter name used |
| 8936 | * in the haproxy configuration file, and an LUA function. |
| 8937 | */ |
| 8938 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 8939 | { |
| 8940 | const char *name; |
| 8941 | int ref; |
| 8942 | int len; |
| 8943 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8944 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8945 | struct sample_fetch *sf; |
| 8946 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8947 | |
| 8948 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 8949 | |
| 8950 | /* First argument : sample-fetch name. */ |
| 8951 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8952 | |
| 8953 | /* Second argument : lua function. */ |
| 8954 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 8955 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8956 | /* Check if the sample-fetch is already registered */ |
| 8957 | trash = get_trash_chunk(); |
| 8958 | chunk_printf(trash, "lua.%s", name); |
| 8959 | sf = find_sample_fetch(trash->area, trash->data); |
| 8960 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8961 | fcn = sf->private; |
| 8962 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8963 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 8964 | "This will become a hard error in version 2.5.\n", name); |
| 8965 | } |
| 8966 | fcn->function_ref[hlua_state_id] = ref; |
| 8967 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8968 | } |
| 8969 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8970 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8971 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8972 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8973 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8974 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8975 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8976 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8977 | |
| 8978 | /* Fill fcn. */ |
| 8979 | fcn->name = strdup(name); |
| 8980 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8981 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8982 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8983 | |
| 8984 | /* List head */ |
| 8985 | sfk->list.n = sfk->list.p = NULL; |
| 8986 | |
| 8987 | /* sample-fetch keyword. */ |
| 8988 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8989 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8990 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8991 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8992 | |
| 8993 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 8994 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 8995 | 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] | 8996 | sfk->kw[0].val_args = NULL; |
| 8997 | sfk->kw[0].out_type = SMP_T_STR; |
| 8998 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 8999 | sfk->kw[0].val = 0; |
| 9000 | sfk->kw[0].private = fcn; |
| 9001 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9002 | /* Register this new fetch. */ |
| 9003 | sample_register_fetches(sfk); |
| 9004 | |
| 9005 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9006 | |
| 9007 | alloc_error: |
| 9008 | release_hlua_function(fcn); |
| 9009 | ha_free(&sfk); |
| 9010 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9011 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9012 | } |
| 9013 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 9014 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9015 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 9016 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9017 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9018 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9019 | unsigned int delay; |
| 9020 | unsigned int wakeup_ms; |
| 9021 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9022 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 9023 | hlua = hlua_gethlua(L); |
| 9024 | if (!hlua) { |
| 9025 | return 0; |
| 9026 | } |
| 9027 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9028 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 9029 | |
| 9030 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 9031 | wakeup_ms = tick_add(now_ms, delay); |
| 9032 | hlua->wake_time = wakeup_ms; |
| 9033 | return 0; |
| 9034 | } |
| 9035 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9036 | /* This function is a wrapper to execute each LUA function declared as an action |
| 9037 | * wrapper during the initialisation period. This function may return any |
| 9038 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 9039 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 9040 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9041 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9042 | 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] | 9043 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9044 | { |
| 9045 | char **arg; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 9046 | unsigned int hflags = HLUA_TXN_ACT_CTX; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9047 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9048 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9049 | |
| 9050 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 9051 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 9052 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 9053 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 9054 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9055 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9056 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9057 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9058 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9059 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9060 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9061 | * Lua context can be not initialized. This behavior |
| 9062 | * permits to save performances because a systematic |
| 9063 | * Lua initialization cause 5% performances loss. |
| 9064 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9065 | if (!s->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9066 | struct hlua *hlua; |
| 9067 | |
| 9068 | hlua = pool_alloc(pool_head_hlua); |
| 9069 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9070 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9071 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9072 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9073 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9074 | HLUA_INIT(hlua); |
| 9075 | s->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9076 | 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] | 9077 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9078 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9079 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9080 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9081 | } |
| 9082 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9083 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9084 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9085 | |
| 9086 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9087 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9088 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 9089 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9090 | else |
| 9091 | error = "critical error"; |
| 9092 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9093 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9094 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9095 | } |
| 9096 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9097 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9098 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9099 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9100 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9101 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9102 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9103 | } |
| 9104 | |
| 9105 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9106 | 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] | 9107 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9108 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 9109 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9110 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9111 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9112 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9113 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9114 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9115 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9116 | |
| 9117 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9118 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9119 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9120 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9121 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9122 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9123 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9124 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9125 | lua_pushstring(s->hlua->T, *arg); |
| 9126 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9127 | } |
| 9128 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9129 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9130 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9131 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9132 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9133 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9134 | } |
| 9135 | |
| 9136 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 9137 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9138 | /* finished. */ |
| 9139 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9140 | /* Catch the return value */ |
| 9141 | if (lua_gettop(s->hlua->T) > 0) |
| 9142 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9143 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9144 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 9145 | if (act_ret == ACT_RET_YIELD) { |
| 9146 | if (flags & ACT_OPT_FINAL) |
| 9147 | goto err_yield; |
| 9148 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9149 | if (dir == SMP_OPT_DIR_REQ) |
| 9150 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 9151 | s->hlua->wake_time); |
| 9152 | else |
| 9153 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 9154 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9155 | } |
| 9156 | goto end; |
| 9157 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9158 | /* yield. */ |
| 9159 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 9160 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9161 | if (dir == SMP_OPT_DIR_REQ) |
| 9162 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 9163 | s->hlua->wake_time); |
| 9164 | else |
| 9165 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 9166 | s->hlua->wake_time); |
| 9167 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9168 | /* Some actions can be wake up when a "write" event |
| 9169 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9170 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9171 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 9172 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9173 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9174 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9175 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9176 | act_ret = ACT_RET_YIELD; |
| 9177 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9178 | |
| 9179 | /* finished with error. */ |
| 9180 | case HLUA_E_ERRMSG: |
| 9181 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9182 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9183 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9184 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9185 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9186 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9187 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9188 | 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] | 9189 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9190 | |
| 9191 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9192 | 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] | 9193 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9194 | |
| 9195 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 9196 | err_yield: |
| 9197 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9198 | 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] | 9199 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9200 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9201 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9202 | case HLUA_E_ERR: |
| 9203 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9204 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9205 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9206 | |
| 9207 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9208 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9209 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9210 | |
| 9211 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 9212 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9213 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9214 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9215 | } |
| 9216 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 9217 | 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] | 9218 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 9219 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9220 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9221 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 9222 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 9223 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9224 | } |
| 9225 | |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9226 | static int hlua_applet_tcp_init(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9227 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9228 | struct hlua_tcp_ctx *tcp_ctx = applet_reserve_svcctx(ctx, sizeof(*tcp_ctx)); |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame^] | 9229 | struct conn_stream *cs = appctx_cs(ctx); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 9230 | struct stream *strm = __cs_strm(cs); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9231 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9232 | struct task *task; |
| 9233 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9234 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9235 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9236 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9237 | if (!hlua) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9238 | SEND_ERR(strm->be, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9239 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9240 | return 0; |
| 9241 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9242 | HLUA_INIT(hlua); |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9243 | tcp_ctx->hlua = hlua; |
| 9244 | tcp_ctx->flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9245 | |
| 9246 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 9247 | task = task_new_here(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9248 | if (!task) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9249 | SEND_ERR(strm->be, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9250 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9251 | return 0; |
| 9252 | } |
| 9253 | task->nice = 0; |
| 9254 | task->context = ctx; |
| 9255 | task->process = hlua_applet_wakeup; |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9256 | tcp_ctx->task = task; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9257 | |
| 9258 | /* In the execution wrappers linked with a stream, the |
| 9259 | * Lua context can be not initialized. This behavior |
| 9260 | * permits to save performances because a systematic |
| 9261 | * Lua initialization cause 5% performances loss. |
| 9262 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9263 | if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(ctx->rule->arg.hlua_rule->fcn), task, 0)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9264 | SEND_ERR(strm->be, "Lua applet tcp '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9265 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9266 | return 0; |
| 9267 | } |
| 9268 | |
| 9269 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 9270 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9271 | |
| 9272 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9273 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9274 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9275 | error = lua_tostring(hlua->T, -1); |
| 9276 | else |
| 9277 | error = "critical error"; |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9278 | SEND_ERR(strm->be, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9279 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9280 | return 0; |
| 9281 | } |
| 9282 | |
| 9283 | /* Check stack available size. */ |
| 9284 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9285 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9286 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9287 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9288 | return 0; |
| 9289 | } |
| 9290 | |
| 9291 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9292 | 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] | 9293 | |
| 9294 | /* Create and and push object stream in the stack. */ |
| 9295 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9296 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9297 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9298 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9299 | return 0; |
| 9300 | } |
| 9301 | hlua->nargs = 1; |
| 9302 | |
| 9303 | /* push keywords in the stack. */ |
| 9304 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 9305 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9306 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9307 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9308 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9309 | return 0; |
| 9310 | } |
| 9311 | lua_pushstring(hlua->T, *arg); |
| 9312 | hlua->nargs++; |
| 9313 | } |
| 9314 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9315 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9316 | |
| 9317 | /* Wakeup the applet ASAP. */ |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 9318 | cs_cant_get(cs); |
| 9319 | cs_rx_endp_more(cs); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9320 | |
| 9321 | return 1; |
| 9322 | } |
| 9323 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 9324 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9325 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9326 | struct hlua_tcp_ctx *tcp_ctx = ctx->svcctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame^] | 9327 | struct conn_stream *cs = appctx_cs(ctx); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 9328 | struct stream *strm = __cs_strm(cs); |
| 9329 | struct channel *res = cs_ic(cs); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9330 | struct act_rule *rule = ctx->rule; |
| 9331 | struct proxy *px = strm->be; |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9332 | struct hlua *hlua = tcp_ctx->hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9333 | |
| 9334 | /* The applet execution is already done. */ |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9335 | if (tcp_ctx->flags & APPLET_DONE) { |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 9336 | /* eat the whole request */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 9337 | co_skip(cs_oc(cs), co_data(cs_oc(cs))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9338 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 9339 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9340 | |
| 9341 | /* If the stream is disconnect or closed, ldo nothing. */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 9342 | if (unlikely(cs->state == CS_ST_DIS || cs->state == CS_ST_CLO)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9343 | return; |
| 9344 | |
| 9345 | /* Execute the function. */ |
| 9346 | switch (hlua_ctx_resume(hlua, 1)) { |
| 9347 | /* finished. */ |
| 9348 | case HLUA_E_OK: |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9349 | tcp_ctx->flags |= APPLET_DONE; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9350 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9351 | /* eat the whole request */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 9352 | co_skip(cs_oc(cs), co_data(cs_oc(cs))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9353 | res->flags |= CF_READ_NULL; |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame] | 9354 | cs_shutr(cs); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9355 | return; |
| 9356 | |
| 9357 | /* yield. */ |
| 9358 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 9359 | if (hlua->wake_time != TICK_ETERNITY) |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9360 | task_schedule(tcp_ctx->task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9361 | return; |
| 9362 | |
| 9363 | /* finished with error. */ |
| 9364 | case HLUA_E_ERRMSG: |
| 9365 | /* Display log. */ |
| 9366 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9367 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9368 | lua_pop(hlua->T, 1); |
| 9369 | goto error; |
| 9370 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9371 | case HLUA_E_ETMOUT: |
| 9372 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9373 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9374 | goto error; |
| 9375 | |
| 9376 | case HLUA_E_NOMEM: |
| 9377 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9378 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9379 | goto error; |
| 9380 | |
| 9381 | case HLUA_E_YIELD: /* unexpected */ |
| 9382 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9383 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9384 | goto error; |
| 9385 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9386 | case HLUA_E_ERR: |
| 9387 | /* Display log. */ |
| 9388 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9389 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9390 | goto error; |
| 9391 | |
| 9392 | default: |
| 9393 | goto error; |
| 9394 | } |
| 9395 | |
| 9396 | error: |
| 9397 | |
| 9398 | /* For all other cases, just close the stream. */ |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame] | 9399 | cs_shutw(cs); |
| 9400 | cs_shutr(cs); |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9401 | tcp_ctx->flags |= APPLET_DONE; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9402 | } |
| 9403 | |
| 9404 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 9405 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9406 | struct hlua_tcp_ctx *tcp_ctx = ctx->svcctx; |
| 9407 | |
| 9408 | task_destroy(tcp_ctx->task); |
| 9409 | tcp_ctx->task = NULL; |
| 9410 | hlua_ctx_destroy(tcp_ctx->hlua); |
| 9411 | tcp_ctx->hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9412 | } |
| 9413 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9414 | /* The function returns 1 if the initialisation is complete, 0 if |
| 9415 | * an errors occurs and -1 if more data are required for initializing |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9416 | * the applet. It also reserves the appctx for an hlua_http_ctx. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9417 | */ |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9418 | static int hlua_applet_http_init(struct appctx *ctx) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9419 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9420 | struct hlua_http_ctx *http_ctx = applet_reserve_svcctx(ctx, sizeof(*http_ctx)); |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame^] | 9421 | struct conn_stream *cs = appctx_cs(ctx); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 9422 | struct stream *strm = __cs_strm(cs); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9423 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9424 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9425 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9426 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9427 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9428 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9429 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9430 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9431 | if (!hlua) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9432 | SEND_ERR(strm->be, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9433 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9434 | return 0; |
| 9435 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9436 | HLUA_INIT(hlua); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9437 | http_ctx->hlua = hlua; |
| 9438 | http_ctx->left_bytes = -1; |
| 9439 | http_ctx->flags = 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9440 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 9441 | if (txn->req.flags & HTTP_MSGF_VER_11) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9442 | http_ctx->flags |= APPLET_HTTP11; |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 9443 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9444 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 9445 | task = task_new_here(); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9446 | if (!task) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9447 | SEND_ERR(strm->be, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9448 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9449 | return 0; |
| 9450 | } |
| 9451 | task->nice = 0; |
| 9452 | task->context = ctx; |
| 9453 | task->process = hlua_applet_wakeup; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9454 | http_ctx->task = task; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9455 | |
| 9456 | /* In the execution wrappers linked with a stream, the |
| 9457 | * Lua context can be not initialized. This behavior |
| 9458 | * permits to save performances because a systematic |
| 9459 | * Lua initialization cause 5% performances loss. |
| 9460 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9461 | if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(ctx->rule->arg.hlua_rule->fcn), task, 0)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9462 | SEND_ERR(strm->be, "Lua applet http '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9463 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9464 | return 0; |
| 9465 | } |
| 9466 | |
| 9467 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 9468 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9469 | |
| 9470 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9471 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9472 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9473 | error = lua_tostring(hlua->T, -1); |
| 9474 | else |
| 9475 | error = "critical error"; |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9476 | SEND_ERR(strm->be, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9477 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9478 | return 0; |
| 9479 | } |
| 9480 | |
| 9481 | /* Check stack available size. */ |
| 9482 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9483 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9484 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9485 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9486 | return 0; |
| 9487 | } |
| 9488 | |
| 9489 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9490 | 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] | 9491 | |
| 9492 | /* Create and and push object stream in the stack. */ |
| 9493 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9494 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9495 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9496 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9497 | return 0; |
| 9498 | } |
| 9499 | hlua->nargs = 1; |
| 9500 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9501 | /* push keywords in the stack. */ |
| 9502 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 9503 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9504 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9505 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9506 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9507 | return 0; |
| 9508 | } |
| 9509 | lua_pushstring(hlua->T, *arg); |
| 9510 | hlua->nargs++; |
| 9511 | } |
| 9512 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9513 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9514 | |
| 9515 | /* Wakeup the applet when data is ready for read. */ |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 9516 | cs_cant_get(cs); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9517 | |
| 9518 | return 1; |
| 9519 | } |
| 9520 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 9521 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9522 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9523 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame^] | 9524 | struct conn_stream *cs = appctx_cs(ctx); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 9525 | struct stream *strm = __cs_strm(cs); |
| 9526 | struct channel *req = cs_oc(cs); |
| 9527 | struct channel *res = cs_ic(cs); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9528 | struct act_rule *rule = ctx->rule; |
| 9529 | struct proxy *px = strm->be; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9530 | struct hlua *hlua = http_ctx->hlua; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9531 | struct htx *req_htx, *res_htx; |
| 9532 | |
| 9533 | res_htx = htx_from_buf(&res->buf); |
| 9534 | |
| 9535 | /* If the stream is disconnect or closed, ldo nothing. */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 9536 | if (unlikely(cs->state == CS_ST_DIS || cs->state == CS_ST_CLO)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9537 | goto out; |
| 9538 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9539 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9540 | if (!b_size(&res->buf)) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 9541 | cs_rx_room_blk(cs); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9542 | goto out; |
| 9543 | } |
| 9544 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9545 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9546 | http_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9547 | |
| 9548 | /* Set the currently running flag. */ |
| 9549 | if (!HLUA_IS_RUNNING(hlua) && |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9550 | !(http_ctx->flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 9551 | if (!co_data(req)) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 9552 | cs_cant_get(cs); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9553 | goto out; |
| 9554 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9555 | } |
| 9556 | |
| 9557 | /* Executes The applet if it is not done. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9558 | if (!(http_ctx->flags & APPLET_DONE)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9559 | |
| 9560 | /* Execute the function. */ |
| 9561 | switch (hlua_ctx_resume(hlua, 1)) { |
| 9562 | /* finished. */ |
| 9563 | case HLUA_E_OK: |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9564 | http_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9565 | break; |
| 9566 | |
| 9567 | /* yield. */ |
| 9568 | case HLUA_E_AGAIN: |
| 9569 | if (hlua->wake_time != TICK_ETERNITY) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9570 | task_schedule(http_ctx->task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9571 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9572 | |
| 9573 | /* finished with error. */ |
| 9574 | case HLUA_E_ERRMSG: |
| 9575 | /* Display log. */ |
| 9576 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9577 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9578 | lua_pop(hlua->T, 1); |
| 9579 | goto error; |
| 9580 | |
| 9581 | case HLUA_E_ETMOUT: |
| 9582 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9583 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9584 | goto error; |
| 9585 | |
| 9586 | case HLUA_E_NOMEM: |
| 9587 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9588 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9589 | goto error; |
| 9590 | |
| 9591 | case HLUA_E_YIELD: /* unexpected */ |
| 9592 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9593 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9594 | goto error; |
| 9595 | |
| 9596 | case HLUA_E_ERR: |
| 9597 | /* Display log. */ |
| 9598 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9599 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9600 | goto error; |
| 9601 | |
| 9602 | default: |
| 9603 | goto error; |
| 9604 | } |
| 9605 | } |
| 9606 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9607 | if (http_ctx->flags & APPLET_DONE) { |
| 9608 | if (http_ctx->flags & APPLET_RSP_SENT) |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9609 | goto done; |
| 9610 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9611 | if (!(http_ctx->flags & APPLET_HDR_SENT)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9612 | goto error; |
| 9613 | |
Christopher Faulet | f89af9c | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 9614 | /* no more data are expected. If the response buffer is empty |
| 9615 | * for a chunked message, be sure to add something (EOT block in |
| 9616 | * this case) to have something to send. It is important to be |
| 9617 | * sure the EOM flags will be handled by the endpoint. |
| 9618 | */ |
| 9619 | if (htx_is_empty(res_htx) && (strm->txn->rsp.flags & (HTTP_MSGF_XFER_LEN|HTTP_MSGF_CNT_LEN)) == HTTP_MSGF_XFER_LEN) { |
| 9620 | if (!htx_add_endof(res_htx, HTX_BLK_EOT)) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 9621 | cs_rx_room_blk(cs); |
Christopher Faulet | f89af9c | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 9622 | goto out; |
| 9623 | } |
| 9624 | channel_add_input(res, 1); |
| 9625 | } |
| 9626 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 9627 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | d8d2708 | 2022-03-07 15:50:54 +0100 | [diff] [blame] | 9628 | res->flags |= CF_EOI; |
Willy Tarreau | 66435e5 | 2022-05-10 11:32:31 +0200 | [diff] [blame] | 9629 | ctx->endp->flags |= CS_EP_EOI; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9630 | strm->txn->status = http_ctx->status; |
| 9631 | http_ctx->flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9632 | } |
| 9633 | |
| 9634 | done: |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9635 | if (http_ctx->flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9636 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9637 | res->flags |= CF_READ_NULL; |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame] | 9638 | cs_shutr(cs); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9639 | } |
| 9640 | |
| 9641 | /* eat the whole request */ |
| 9642 | if (co_data(req)) { |
| 9643 | req_htx = htx_from_buf(&req->buf); |
| 9644 | co_htx_skip(req, req_htx, co_data(req)); |
| 9645 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9646 | } |
| 9647 | } |
| 9648 | |
| 9649 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9650 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9651 | return; |
| 9652 | |
| 9653 | error: |
| 9654 | |
| 9655 | /* If we are in HTTP mode, and we are not send any |
| 9656 | * data, return a 500 server error in best effort: |
| 9657 | * if there is no room available in the buffer, |
| 9658 | * just close the connection. |
| 9659 | */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9660 | if (!(http_ctx->flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 9661 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9662 | |
| 9663 | channel_erase(res); |
| 9664 | res->buf.data = b_data(err); |
| 9665 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 9666 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 9667 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9668 | } |
| 9669 | if (!(strm->flags & SF_ERR_MASK)) |
| 9670 | strm->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9671 | http_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9672 | goto done; |
| 9673 | } |
| 9674 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9675 | static void hlua_applet_http_release(struct appctx *ctx) |
| 9676 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9677 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
| 9678 | |
| 9679 | task_destroy(http_ctx->task); |
| 9680 | http_ctx->task = NULL; |
| 9681 | hlua_ctx_destroy(http_ctx->hlua); |
| 9682 | http_ctx->hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9683 | } |
| 9684 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9685 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9686 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9687 | * |
| 9688 | * This function can fail with an abort() due to an Lua critical error. |
| 9689 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 9690 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9691 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9692 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 9693 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9694 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9695 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9696 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9697 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9698 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9699 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9700 | if (!rule->arg.hlua_rule) { |
| 9701 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9702 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9703 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9704 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9705 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 9706 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 9707 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9708 | if (!rule->arg.hlua_rule->args) { |
| 9709 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9710 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9711 | } |
| 9712 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9713 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9714 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9715 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9716 | /* Expect some arguments */ |
| 9717 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9718 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9719 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9720 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9721 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9722 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9723 | if (!rule->arg.hlua_rule->args[i]) { |
| 9724 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9725 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9726 | } |
| 9727 | (*cur_arg)++; |
| 9728 | } |
| 9729 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9730 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 9731 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9732 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9733 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9734 | |
| 9735 | error: |
| 9736 | if (rule->arg.hlua_rule) { |
| 9737 | if (rule->arg.hlua_rule->args) { |
| 9738 | for (i = 0; i < fcn->nargs; i++) |
| 9739 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 9740 | ha_free(&rule->arg.hlua_rule->args); |
| 9741 | } |
| 9742 | ha_free(&rule->arg.hlua_rule); |
| 9743 | } |
| 9744 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9745 | } |
| 9746 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9747 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 9748 | struct act_rule *rule, char **err) |
| 9749 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9750 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9751 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9752 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9753 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9754 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9755 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9756 | * the call of this analyzer. |
| 9757 | */ |
| 9758 | if (rule->from != ACT_F_HTTP_REQ) { |
| 9759 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 9760 | return ACT_RET_PRS_ERR; |
| 9761 | } |
| 9762 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9763 | /* Memory for the rule. */ |
| 9764 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 9765 | if (!rule->arg.hlua_rule) { |
| 9766 | memprintf(err, "out of memory error"); |
| 9767 | return ACT_RET_PRS_ERR; |
| 9768 | } |
| 9769 | |
| 9770 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9771 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9772 | |
| 9773 | /* TODO: later accept arguments. */ |
| 9774 | rule->arg.hlua_rule->args = NULL; |
| 9775 | |
| 9776 | /* Add applet pointer in the rule. */ |
| 9777 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 9778 | rule->applet.name = fcn->name; |
| 9779 | rule->applet.init = hlua_applet_http_init; |
| 9780 | rule->applet.fct = hlua_applet_http_fct; |
| 9781 | rule->applet.release = hlua_applet_http_release; |
| 9782 | rule->applet.timeout = hlua_timeout_applet; |
| 9783 | |
| 9784 | return ACT_RET_PRS_OK; |
| 9785 | } |
| 9786 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9787 | /* This function is an LUA binding used for registering |
| 9788 | * "sample-conv" functions. It expects a converter name used |
| 9789 | * in the haproxy configuration file, and an LUA function. |
| 9790 | */ |
| 9791 | __LJMP static int hlua_register_action(lua_State *L) |
| 9792 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9793 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9794 | const char *name; |
| 9795 | int ref; |
| 9796 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9797 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9798 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9799 | struct buffer *trash; |
| 9800 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9801 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9802 | /* Initialise the number of expected arguments at 0. */ |
| 9803 | nargs = 0; |
| 9804 | |
| 9805 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 9806 | 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] | 9807 | |
| 9808 | /* First argument : converter name. */ |
| 9809 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9810 | |
| 9811 | /* Second argument : environment. */ |
| 9812 | if (lua_type(L, 2) != LUA_TTABLE) |
| 9813 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9814 | |
| 9815 | /* Third argument : lua function. */ |
| 9816 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 9817 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9818 | /* 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] | 9819 | if (lua_gettop(L) >= 4) |
| 9820 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 9821 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9822 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9823 | lua_pushnil(L); |
| 9824 | while (lua_next(L, 2) != 0) { |
| 9825 | if (lua_type(L, -1) != LUA_TSTRING) |
| 9826 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9827 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9828 | /* Check if action exists */ |
| 9829 | trash = get_trash_chunk(); |
| 9830 | chunk_printf(trash, "lua.%s", name); |
| 9831 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 9832 | akw = tcp_req_cont_action(trash->area); |
| 9833 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 9834 | akw = tcp_res_cont_action(trash->area); |
| 9835 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 9836 | akw = action_http_req_custom(trash->area); |
| 9837 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 9838 | akw = action_http_res_custom(trash->area); |
| 9839 | } else { |
| 9840 | akw = NULL; |
| 9841 | } |
| 9842 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9843 | fcn = akw->private; |
| 9844 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9845 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 9846 | "This will become a hard error in version 2.5.\n", name); |
| 9847 | } |
| 9848 | fcn->function_ref[hlua_state_id] = ref; |
| 9849 | |
| 9850 | /* pop the environment string. */ |
| 9851 | lua_pop(L, 1); |
| 9852 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9853 | } |
| 9854 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9855 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 9856 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9857 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9858 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9859 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9860 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9861 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9862 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9863 | |
| 9864 | /* Fill fcn. */ |
| 9865 | fcn->name = strdup(name); |
| 9866 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9867 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9868 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9869 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 9870 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9871 | fcn->nargs = nargs; |
| 9872 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9873 | /* List head */ |
| 9874 | akl->list.n = akl->list.p = NULL; |
| 9875 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9876 | /* action keyword. */ |
| 9877 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9878 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9879 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9880 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9881 | |
| 9882 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 9883 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 9884 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9885 | akl->kw[0].private = fcn; |
| 9886 | akl->kw[0].parse = action_register_lua; |
| 9887 | |
| 9888 | /* select the action registering point. */ |
| 9889 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 9890 | tcp_req_cont_keywords_register(akl); |
| 9891 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 9892 | tcp_res_cont_keywords_register(akl); |
| 9893 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 9894 | http_req_keywords_register(akl); |
| 9895 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 9896 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9897 | else { |
| 9898 | release_hlua_function(fcn); |
| 9899 | if (akl) |
| 9900 | ha_free((char **)&(akl->kw[0].kw)); |
| 9901 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 9902 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9903 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 9904 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9905 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9906 | |
| 9907 | /* pop the environment string. */ |
| 9908 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9909 | |
| 9910 | /* reset for next loop */ |
| 9911 | akl = NULL; |
| 9912 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9913 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9914 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9915 | |
| 9916 | alloc_error: |
| 9917 | release_hlua_function(fcn); |
| 9918 | ha_free(&akl); |
| 9919 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9920 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9921 | } |
| 9922 | |
| 9923 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 9924 | struct act_rule *rule, char **err) |
| 9925 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9926 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9927 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 9928 | if (px->mode == PR_MODE_HTTP) { |
| 9929 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 9930 | return ACT_RET_PRS_ERR; |
| 9931 | } |
| 9932 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9933 | /* Memory for the rule. */ |
| 9934 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 9935 | if (!rule->arg.hlua_rule) { |
| 9936 | memprintf(err, "out of memory error"); |
| 9937 | return ACT_RET_PRS_ERR; |
| 9938 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9939 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9940 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9941 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9942 | |
| 9943 | /* TODO: later accept arguments. */ |
| 9944 | rule->arg.hlua_rule->args = NULL; |
| 9945 | |
| 9946 | /* Add applet pointer in the rule. */ |
| 9947 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 9948 | rule->applet.name = fcn->name; |
| 9949 | rule->applet.init = hlua_applet_tcp_init; |
| 9950 | rule->applet.fct = hlua_applet_tcp_fct; |
| 9951 | rule->applet.release = hlua_applet_tcp_release; |
| 9952 | rule->applet.timeout = hlua_timeout_applet; |
| 9953 | |
| 9954 | return 0; |
| 9955 | } |
| 9956 | |
| 9957 | /* This function is an LUA binding used for registering |
| 9958 | * "sample-conv" functions. It expects a converter name used |
| 9959 | * in the haproxy configuration file, and an LUA function. |
| 9960 | */ |
| 9961 | __LJMP static int hlua_register_service(lua_State *L) |
| 9962 | { |
| 9963 | struct action_kw_list *akl; |
| 9964 | const char *name; |
| 9965 | const char *env; |
| 9966 | int ref; |
| 9967 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9968 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9969 | struct buffer *trash; |
| 9970 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9971 | |
| 9972 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 9973 | |
| 9974 | /* First argument : converter name. */ |
| 9975 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9976 | |
| 9977 | /* Second argument : environment. */ |
| 9978 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 9979 | |
| 9980 | /* Third argument : lua function. */ |
| 9981 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 9982 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9983 | /* Check for service already registered */ |
| 9984 | trash = get_trash_chunk(); |
| 9985 | chunk_printf(trash, "lua.%s", name); |
| 9986 | akw = service_find(trash->area); |
| 9987 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9988 | fcn = akw->private; |
| 9989 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9990 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 9991 | "This will become a hard error in version 2.5.\n", name); |
| 9992 | } |
| 9993 | fcn->function_ref[hlua_state_id] = ref; |
| 9994 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9995 | } |
| 9996 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9997 | /* Allocate and fill the sample fetch keyword struct. */ |
| 9998 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 9999 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10000 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10001 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10002 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10003 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10004 | |
| 10005 | /* Fill fcn. */ |
| 10006 | len = strlen("<lua.>") + strlen(name) + 1; |
| 10007 | fcn->name = calloc(1, len); |
| 10008 | if (!fcn->name) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10009 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10010 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10011 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10012 | |
| 10013 | /* List head */ |
| 10014 | akl->list.n = akl->list.p = NULL; |
| 10015 | |
| 10016 | /* converter keyword. */ |
| 10017 | len = strlen("lua.") + strlen(name) + 1; |
| 10018 | akl->kw[0].kw = calloc(1, len); |
| 10019 | if (!akl->kw[0].kw) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10020 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10021 | |
| 10022 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 10023 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 10024 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10025 | if (strcmp(env, "tcp") == 0) |
| 10026 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10027 | else if (strcmp(env, "http") == 0) |
| 10028 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10029 | else { |
| 10030 | release_hlua_function(fcn); |
| 10031 | if (akl) |
| 10032 | ha_free((char **)&(akl->kw[0].kw)); |
| 10033 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 10034 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 10035 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10036 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10037 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 10038 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10039 | akl->kw[0].private = fcn; |
| 10040 | |
| 10041 | /* End of array. */ |
| 10042 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 10043 | |
| 10044 | /* Register this new converter */ |
| 10045 | service_keywords_register(akl); |
| 10046 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10047 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10048 | |
| 10049 | alloc_error: |
| 10050 | release_hlua_function(fcn); |
| 10051 | ha_free(&akl); |
| 10052 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 10053 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10054 | } |
| 10055 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10056 | /* This function initialises Lua cli handler. It copies the |
| 10057 | * arguments in the Lua stack and create channel IO objects. |
| 10058 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 10059 | 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] | 10060 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10061 | struct hlua_cli_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10062 | struct hlua *hlua; |
| 10063 | struct hlua_function *fcn; |
| 10064 | int i; |
| 10065 | const char *error; |
| 10066 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10067 | fcn = private; |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10068 | ctx->fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10069 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10070 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10071 | if (!hlua) { |
| 10072 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10073 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10074 | } |
| 10075 | HLUA_INIT(hlua); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10076 | ctx->hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10077 | |
| 10078 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 10079 | * 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] | 10080 | * applet_http. It is absolutely compatible. |
| 10081 | */ |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10082 | ctx->task = task_new_here(); |
| 10083 | if (!ctx->task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 10084 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10085 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10086 | } |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10087 | ctx->task->nice = 0; |
| 10088 | ctx->task->context = appctx; |
| 10089 | ctx->task->process = hlua_applet_wakeup; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10090 | |
| 10091 | /* Initialises the Lua context */ |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10092 | if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(fcn), ctx->task, 0)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10093 | 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] | 10094 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10095 | } |
| 10096 | |
| 10097 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10098 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10099 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 10100 | error = lua_tostring(hlua->T, -1); |
| 10101 | else |
| 10102 | error = "critical error"; |
| 10103 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 10104 | goto error; |
| 10105 | } |
| 10106 | |
| 10107 | /* Check stack available size. */ |
| 10108 | if (!lua_checkstack(hlua->T, 2)) { |
| 10109 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10110 | goto error; |
| 10111 | } |
| 10112 | |
| 10113 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10114 | 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] | 10115 | |
| 10116 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 10117 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10118 | */ |
| 10119 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 10120 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10121 | goto error; |
| 10122 | } |
| 10123 | hlua->nargs = 1; |
| 10124 | |
| 10125 | /* push keywords in the stack. */ |
| 10126 | for (i = 0; *args[i]; i++) { |
| 10127 | /* Check stack available size. */ |
| 10128 | if (!lua_checkstack(hlua->T, 1)) { |
| 10129 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10130 | goto error; |
| 10131 | } |
| 10132 | lua_pushstring(hlua->T, args[i]); |
| 10133 | hlua->nargs++; |
| 10134 | } |
| 10135 | |
| 10136 | /* We must initialize the execution timeouts. */ |
| 10137 | hlua->max_time = hlua_timeout_session; |
| 10138 | |
| 10139 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10140 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10141 | |
| 10142 | /* It's ok */ |
| 10143 | return 0; |
| 10144 | |
| 10145 | /* It's not ok. */ |
| 10146 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10147 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10148 | hlua_ctx_destroy(hlua); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10149 | ctx->hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10150 | return 1; |
| 10151 | } |
| 10152 | |
| 10153 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 10154 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10155 | struct hlua_cli_ctx *ctx = appctx->svcctx; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10156 | struct hlua *hlua; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 10157 | struct conn_stream *cs; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10158 | struct hlua_function *fcn; |
| 10159 | |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10160 | hlua = ctx->hlua; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame^] | 10161 | cs = appctx_cs(appctx); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10162 | fcn = ctx->fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10163 | |
| 10164 | /* If the stream is disconnect or closed, ldo nothing. */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 10165 | if (unlikely(cs->state == CS_ST_DIS || cs->state == CS_ST_CLO)) |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10166 | return 1; |
| 10167 | |
| 10168 | /* Execute the function. */ |
| 10169 | switch (hlua_ctx_resume(hlua, 1)) { |
| 10170 | |
| 10171 | /* finished. */ |
| 10172 | case HLUA_E_OK: |
| 10173 | return 1; |
| 10174 | |
| 10175 | /* yield. */ |
| 10176 | case HLUA_E_AGAIN: |
| 10177 | /* We want write. */ |
| 10178 | if (HLUA_IS_WAKERESWR(hlua)) |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 10179 | cs_rx_room_blk(cs); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10180 | /* Set the timeout. */ |
| 10181 | if (hlua->wake_time != TICK_ETERNITY) |
| 10182 | task_schedule(hlua->task, hlua->wake_time); |
| 10183 | return 0; |
| 10184 | |
| 10185 | /* finished with error. */ |
| 10186 | case HLUA_E_ERRMSG: |
| 10187 | /* Display log. */ |
| 10188 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 10189 | fcn->name, lua_tostring(hlua->T, -1)); |
| 10190 | lua_pop(hlua->T, 1); |
| 10191 | return 1; |
| 10192 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10193 | case HLUA_E_ETMOUT: |
| 10194 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 10195 | fcn->name); |
| 10196 | return 1; |
| 10197 | |
| 10198 | case HLUA_E_NOMEM: |
| 10199 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 10200 | fcn->name); |
| 10201 | return 1; |
| 10202 | |
| 10203 | case HLUA_E_YIELD: /* unexpected */ |
| 10204 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 10205 | fcn->name); |
| 10206 | return 1; |
| 10207 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10208 | case HLUA_E_ERR: |
| 10209 | /* Display log. */ |
| 10210 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 10211 | fcn->name); |
| 10212 | return 1; |
| 10213 | |
| 10214 | default: |
| 10215 | return 1; |
| 10216 | } |
| 10217 | |
| 10218 | return 1; |
| 10219 | } |
| 10220 | |
| 10221 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 10222 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10223 | struct hlua_cli_ctx *ctx = appctx->svcctx; |
| 10224 | |
| 10225 | hlua_ctx_destroy(ctx->hlua); |
| 10226 | ctx->hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10227 | } |
| 10228 | |
| 10229 | /* This function is an LUA binding used for registering |
| 10230 | * new keywords in the cli. It expects a list of keywords |
| 10231 | * which are the "path". It is limited to 5 keywords. A |
| 10232 | * description of the command, a function to be executed |
| 10233 | * for the parsing and a function for io handlers. |
| 10234 | */ |
| 10235 | __LJMP static int hlua_register_cli(lua_State *L) |
| 10236 | { |
| 10237 | struct cli_kw_list *cli_kws; |
| 10238 | const char *message; |
| 10239 | int ref_io; |
| 10240 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10241 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10242 | int index; |
| 10243 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10244 | struct buffer *trash; |
| 10245 | const char *kw[5]; |
| 10246 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10247 | const char *errmsg; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10248 | |
| 10249 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 10250 | |
| 10251 | /* First argument : an array of maximum 5 keywords. */ |
| 10252 | if (!lua_istable(L, 1)) |
| 10253 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 10254 | |
| 10255 | /* Second argument : string with contextual message. */ |
| 10256 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 10257 | |
| 10258 | /* Third and fourth argument : lua function. */ |
| 10259 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 10260 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10261 | /* Check for CLI service already registered */ |
| 10262 | trash = get_trash_chunk(); |
| 10263 | index = 0; |
| 10264 | lua_pushnil(L); |
| 10265 | memset(kw, 0, sizeof(kw)); |
| 10266 | while (lua_next(L, 1) != 0) { |
| 10267 | if (index >= CLI_PREFIX_KW_NB) |
| 10268 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 10269 | if (lua_type(L, -1) != LUA_TSTRING) |
| 10270 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 10271 | kw[index] = lua_tostring(L, -1); |
| 10272 | if (index == 0) |
| 10273 | chunk_printf(trash, "%s", kw[index]); |
| 10274 | else |
| 10275 | chunk_appendf(trash, " %s", kw[index]); |
| 10276 | index++; |
| 10277 | lua_pop(L, 1); |
| 10278 | } |
| 10279 | cli_kw = cli_find_kw_exact((char **)kw); |
| 10280 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10281 | fcn = cli_kw->private; |
| 10282 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 10283 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 10284 | "This will become a hard error in version 2.5.\n", trash->area); |
| 10285 | } |
| 10286 | fcn->function_ref[hlua_state_id] = ref_io; |
| 10287 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10288 | } |
| 10289 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10290 | /* Allocate and fill the sample fetch keyword struct. */ |
| 10291 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10292 | if (!cli_kws) { |
| 10293 | errmsg = "Lua out of memory error."; |
| 10294 | goto error; |
| 10295 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10296 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10297 | if (!fcn) { |
| 10298 | errmsg = "Lua out of memory error."; |
| 10299 | goto error; |
| 10300 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10301 | |
| 10302 | /* Fill path. */ |
| 10303 | index = 0; |
| 10304 | lua_pushnil(L); |
| 10305 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10306 | if (index >= 5) { |
| 10307 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 10308 | goto error; |
| 10309 | } |
| 10310 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 10311 | errmsg = "1st argument must be a table filled with strings"; |
| 10312 | goto error; |
| 10313 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10314 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10315 | if (!cli_kws->kw[0].str_kw[index]) { |
| 10316 | errmsg = "Lua out of memory error."; |
| 10317 | goto error; |
| 10318 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10319 | index++; |
| 10320 | lua_pop(L, 1); |
| 10321 | } |
| 10322 | |
| 10323 | /* Copy help message. */ |
| 10324 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10325 | if (!cli_kws->kw[0].usage) { |
| 10326 | errmsg = "Lua out of memory error."; |
| 10327 | goto error; |
| 10328 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10329 | |
| 10330 | /* Fill fcn io handler. */ |
| 10331 | len = strlen("<lua.cli>") + 1; |
| 10332 | for (i = 0; i < index; i++) |
| 10333 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 10334 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10335 | if (!fcn->name) { |
| 10336 | errmsg = "Lua out of memory error."; |
| 10337 | goto error; |
| 10338 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10339 | strncat((char *)fcn->name, "<lua.cli", len); |
| 10340 | for (i = 0; i < index; i++) { |
| 10341 | strncat((char *)fcn->name, ".", len); |
| 10342 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 10343 | } |
| 10344 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10345 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10346 | |
| 10347 | /* Fill last entries. */ |
| 10348 | cli_kws->kw[0].private = fcn; |
| 10349 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 10350 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 10351 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 10352 | |
| 10353 | /* Register this new converter */ |
| 10354 | cli_register_kw(cli_kws); |
| 10355 | |
| 10356 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10357 | |
| 10358 | error: |
| 10359 | release_hlua_function(fcn); |
| 10360 | if (cli_kws) { |
| 10361 | for (i = 0; i < index; i++) |
| 10362 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 10363 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 10364 | } |
| 10365 | ha_free(&cli_kws); |
| 10366 | WILL_LJMP(luaL_error(L, errmsg)); |
| 10367 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10368 | } |
| 10369 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10370 | static int hlua_filter_init_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 10371 | { |
| 10372 | struct hlua_flt_config *conf = fconf->conf; |
| 10373 | lua_State *L; |
| 10374 | int error, pos, state_id, flt_ref; |
| 10375 | |
| 10376 | state_id = reg_flt_to_stack_id(conf->reg); |
| 10377 | L = hlua_states[state_id]; |
| 10378 | pos = lua_gettop(L); |
| 10379 | |
| 10380 | /* The filter parsing function */ |
| 10381 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->fun_ref[state_id]); |
| 10382 | |
| 10383 | /* Push the filter class on the stack and resolve all callbacks */ |
| 10384 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->flt_ref[state_id]); |
| 10385 | |
| 10386 | /* Duplicate the filter class so each filter will have its own copy */ |
| 10387 | lua_newtable(L); |
| 10388 | lua_pushnil(L); |
| 10389 | |
| 10390 | while (lua_next(L, pos+2)) { |
| 10391 | lua_pushvalue(L, -2); |
| 10392 | lua_insert(L, -2); |
| 10393 | lua_settable(L, -4); |
| 10394 | } |
| 10395 | flt_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
| 10396 | |
| 10397 | /* Remove the original lua filter class from the stack */ |
| 10398 | lua_pop(L, 1); |
| 10399 | |
| 10400 | /* Push the copy on the stack */ |
| 10401 | lua_rawgeti(L, LUA_REGISTRYINDEX, flt_ref); |
| 10402 | |
| 10403 | /* extra args are pushed in a table */ |
| 10404 | lua_newtable(L); |
| 10405 | for (pos = 0; conf->args[pos]; pos++) { |
| 10406 | /* Check stack available size. */ |
| 10407 | if (!lua_checkstack(L, 1)) { |
| 10408 | ha_alert("Lua filter '%s' : Lua error : full stack.", conf->reg->name); |
| 10409 | goto error; |
| 10410 | } |
| 10411 | lua_pushstring(L, conf->args[pos]); |
| 10412 | lua_rawseti(L, -2, lua_rawlen(L, -2) + 1); |
| 10413 | } |
| 10414 | |
| 10415 | error = lua_pcall(L, 2, LUA_MULTRET, 0); |
| 10416 | switch (error) { |
| 10417 | case LUA_OK: |
| 10418 | /* replace the filter ref */ |
| 10419 | conf->ref[state_id] = flt_ref; |
| 10420 | break; |
| 10421 | case LUA_ERRRUN: |
| 10422 | ha_alert("Lua filter '%s' : runtime error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10423 | goto error; |
| 10424 | case LUA_ERRMEM: |
| 10425 | ha_alert("Lua filter '%s' : out of memory error", conf->reg->name); |
| 10426 | goto error; |
| 10427 | case LUA_ERRERR: |
| 10428 | ha_alert("Lua filter '%s' : message handler error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10429 | goto error; |
| 10430 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
| 10431 | case LUA_ERRGCMM: |
| 10432 | ha_alert("Lua filter '%s' : garbage collector error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10433 | goto error; |
| 10434 | #endif |
| 10435 | default: |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 10436 | ha_alert("Lua filter '%s' : unknown error : %s", conf->reg->name, lua_tostring(L, -1)); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10437 | goto error; |
| 10438 | } |
| 10439 | |
| 10440 | lua_settop(L, 0); |
| 10441 | return 0; |
| 10442 | |
| 10443 | error: |
| 10444 | lua_settop(L, 0); |
| 10445 | return -1; |
| 10446 | } |
| 10447 | |
| 10448 | static void hlua_filter_deinit_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 10449 | { |
| 10450 | struct hlua_flt_config *conf = fconf->conf; |
| 10451 | lua_State *L; |
| 10452 | int state_id; |
| 10453 | |
| 10454 | if (!conf) |
| 10455 | return; |
| 10456 | |
| 10457 | state_id = reg_flt_to_stack_id(conf->reg); |
| 10458 | L = hlua_states[state_id]; |
| 10459 | luaL_unref(L, LUA_REGISTRYINDEX, conf->ref[state_id]); |
| 10460 | } |
| 10461 | |
| 10462 | static int hlua_filter_init(struct proxy *px, struct flt_conf *fconf) |
| 10463 | { |
| 10464 | struct hlua_flt_config *conf = fconf->conf; |
| 10465 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 10466 | |
| 10467 | /* Rely on per-thread init for global scripts */ |
| 10468 | if (!state_id) |
| 10469 | return hlua_filter_init_per_thread(px, fconf); |
| 10470 | return 0; |
| 10471 | } |
| 10472 | |
| 10473 | static void hlua_filter_deinit(struct proxy *px, struct flt_conf *fconf) |
| 10474 | { |
| 10475 | |
| 10476 | if (fconf->conf) { |
| 10477 | struct hlua_flt_config *conf = fconf->conf; |
| 10478 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 10479 | int pos; |
| 10480 | |
| 10481 | /* Rely on per-thread deinit for global scripts */ |
| 10482 | if (!state_id) |
| 10483 | hlua_filter_deinit_per_thread(px, fconf); |
| 10484 | |
| 10485 | for (pos = 0; conf->args[pos]; pos++) |
| 10486 | free(conf->args[pos]); |
| 10487 | free(conf->args); |
| 10488 | } |
| 10489 | ha_free(&fconf->conf); |
| 10490 | ha_free((char **)&fconf->id); |
| 10491 | ha_free(&fconf->ops); |
| 10492 | } |
| 10493 | |
| 10494 | static int hlua_filter_new(struct stream *s, struct filter *filter) |
| 10495 | { |
| 10496 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 10497 | struct hlua_flt_ctx *flt_ctx = NULL; |
| 10498 | int ret = 1; |
| 10499 | |
| 10500 | /* In the execution wrappers linked with a stream, the |
| 10501 | * Lua context can be not initialized. This behavior |
| 10502 | * permits to save performances because a systematic |
| 10503 | * Lua initialization cause 5% performances loss. |
| 10504 | */ |
| 10505 | if (!s->hlua) { |
| 10506 | struct hlua *hlua; |
| 10507 | |
| 10508 | hlua = pool_alloc(pool_head_hlua); |
| 10509 | if (!hlua) { |
| 10510 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 10511 | conf->reg->name); |
| 10512 | ret = 0; |
| 10513 | goto end; |
| 10514 | } |
| 10515 | HLUA_INIT(hlua); |
| 10516 | s->hlua = hlua; |
| 10517 | if (!hlua_ctx_init(s->hlua, reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 10518 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 10519 | conf->reg->name); |
| 10520 | ret = 0; |
| 10521 | goto end; |
| 10522 | } |
| 10523 | } |
| 10524 | |
| 10525 | flt_ctx = pool_zalloc(pool_head_hlua_flt_ctx); |
| 10526 | if (!flt_ctx) { |
| 10527 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10528 | conf->reg->name); |
| 10529 | ret = 0; |
| 10530 | goto end; |
| 10531 | } |
| 10532 | flt_ctx->hlua[0] = pool_alloc(pool_head_hlua); |
| 10533 | flt_ctx->hlua[1] = pool_alloc(pool_head_hlua); |
| 10534 | if (!flt_ctx->hlua[0] || !flt_ctx->hlua[1]) { |
| 10535 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10536 | conf->reg->name); |
| 10537 | ret = 0; |
| 10538 | goto end; |
| 10539 | } |
| 10540 | HLUA_INIT(flt_ctx->hlua[0]); |
| 10541 | HLUA_INIT(flt_ctx->hlua[1]); |
| 10542 | if (!hlua_ctx_init(flt_ctx->hlua[0], reg_flt_to_stack_id(conf->reg), s->task, 0) || |
| 10543 | !hlua_ctx_init(flt_ctx->hlua[1], reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 10544 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10545 | conf->reg->name); |
| 10546 | ret = 0; |
| 10547 | goto end; |
| 10548 | } |
| 10549 | |
| 10550 | if (!HLUA_IS_RUNNING(s->hlua)) { |
| 10551 | /* The following Lua calls can fail. */ |
| 10552 | if (!SET_SAFE_LJMP(s->hlua)) { |
| 10553 | const char *error; |
| 10554 | |
| 10555 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 10556 | error = lua_tostring(s->hlua->T, -1); |
| 10557 | else |
| 10558 | error = "critical error"; |
| 10559 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 10560 | ret = 0; |
| 10561 | goto end; |
| 10562 | } |
| 10563 | |
| 10564 | /* Check stack size. */ |
| 10565 | if (!lua_checkstack(s->hlua->T, 1)) { |
| 10566 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
Tim Duesterhus | 2281738 | 2021-09-11 23:17:25 +0200 | [diff] [blame] | 10567 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10568 | ret = 0; |
| 10569 | goto end; |
| 10570 | } |
| 10571 | |
| 10572 | lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, conf->ref[s->hlua->state_id]); |
| 10573 | if (lua_getfield(s->hlua->T, -1, "new") != LUA_TFUNCTION) { |
| 10574 | SEND_ERR(s->be, "Lua filter '%s': 'new' field is not a function.\n", |
| 10575 | conf->reg->name); |
| 10576 | RESET_SAFE_LJMP(s->hlua); |
| 10577 | ret = 0; |
| 10578 | goto end; |
| 10579 | } |
| 10580 | lua_insert(s->hlua->T, -2); |
| 10581 | |
| 10582 | /* Push the copy on the stack */ |
| 10583 | s->hlua->nargs = 1; |
| 10584 | |
| 10585 | /* We must initialize the execution timeouts. */ |
| 10586 | s->hlua->max_time = hlua_timeout_session; |
| 10587 | |
| 10588 | /* At this point the execution is safe. */ |
| 10589 | RESET_SAFE_LJMP(s->hlua); |
| 10590 | } |
| 10591 | |
| 10592 | switch (hlua_ctx_resume(s->hlua, 0)) { |
| 10593 | case HLUA_E_OK: |
| 10594 | /* Nothing returned or not a table, ignore the filter for current stream */ |
| 10595 | if (!lua_gettop(s->hlua->T) || !lua_istable(s->hlua->T, 1)) { |
| 10596 | ret = 0; |
| 10597 | goto end; |
| 10598 | } |
| 10599 | |
| 10600 | /* Attached the filter pointer to the ctx */ |
| 10601 | lua_pushstring(s->hlua->T, "__filter"); |
| 10602 | lua_pushlightuserdata(s->hlua->T, filter); |
| 10603 | lua_settable(s->hlua->T, -3); |
| 10604 | |
| 10605 | /* Save a ref on the filter ctx */ |
| 10606 | lua_pushvalue(s->hlua->T, 1); |
| 10607 | flt_ctx->ref = luaL_ref(s->hlua->T, LUA_REGISTRYINDEX); |
| 10608 | filter->ctx = flt_ctx; |
| 10609 | break; |
| 10610 | case HLUA_E_ERRMSG: |
| 10611 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(s->hlua->T, -1)); |
| 10612 | ret = -1; |
| 10613 | goto end; |
| 10614 | case HLUA_E_ETMOUT: |
| 10615 | SEND_ERR(s->be, "Lua filter '%s' : 'new' execution timeout.\n", conf->reg->name); |
| 10616 | ret = 0; |
| 10617 | goto end; |
| 10618 | case HLUA_E_NOMEM: |
| 10619 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10620 | ret = 0; |
| 10621 | goto end; |
| 10622 | case HLUA_E_AGAIN: |
| 10623 | case HLUA_E_YIELD: |
| 10624 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10625 | " are not allowed from 'new' function.\n", conf->reg->name); |
| 10626 | ret = 0; |
| 10627 | goto end; |
| 10628 | case HLUA_E_ERR: |
| 10629 | SEND_ERR(s->be, "Lua filter '%s': 'new' returns an unknown error.\n", conf->reg->name); |
| 10630 | ret = 0; |
| 10631 | goto end; |
| 10632 | default: |
| 10633 | ret = 0; |
| 10634 | goto end; |
| 10635 | } |
| 10636 | |
| 10637 | end: |
| 10638 | if (s->hlua) |
| 10639 | lua_settop(s->hlua->T, 0); |
| 10640 | if (ret <= 0) { |
| 10641 | if (flt_ctx) { |
| 10642 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10643 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10644 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10645 | } |
| 10646 | } |
| 10647 | return ret; |
| 10648 | } |
| 10649 | |
| 10650 | static void hlua_filter_delete(struct stream *s, struct filter *filter) |
| 10651 | { |
| 10652 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10653 | |
| 10654 | luaL_unref(s->hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 10655 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10656 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10657 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10658 | filter->ctx = NULL; |
| 10659 | } |
| 10660 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 10661 | static int hlua_filter_from_payload(struct filter *filter) |
| 10662 | { |
| 10663 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10664 | |
| 10665 | return (flt_ctx && !!(flt_ctx->flags & HLUA_FLT_CTX_FL_PAYLOAD)); |
| 10666 | } |
| 10667 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10668 | static int hlua_filter_callback(struct stream *s, struct filter *filter, const char *fun, |
| 10669 | int dir, unsigned int flags) |
| 10670 | { |
| 10671 | struct hlua *flt_hlua; |
| 10672 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 10673 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10674 | unsigned int hflags = HLUA_TXN_FLT_CTX; |
| 10675 | int ret = 1; |
| 10676 | |
| 10677 | flt_hlua = flt_ctx->hlua[(dir == SMP_OPT_DIR_REQ ? 0 : 1)]; |
| 10678 | if (!flt_hlua) |
| 10679 | goto end; |
| 10680 | |
| 10681 | if (!HLUA_IS_RUNNING(flt_hlua)) { |
| 10682 | int extra_idx = lua_gettop(flt_hlua->T); |
| 10683 | |
| 10684 | /* The following Lua calls can fail. */ |
| 10685 | if (!SET_SAFE_LJMP(flt_hlua)) { |
| 10686 | const char *error; |
| 10687 | |
| 10688 | if (lua_type(flt_hlua->T, -1) == LUA_TSTRING) |
| 10689 | error = lua_tostring(flt_hlua->T, -1); |
| 10690 | else |
| 10691 | error = "critical error"; |
| 10692 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 10693 | goto end; |
| 10694 | } |
| 10695 | |
| 10696 | /* Check stack size. */ |
| 10697 | if (!lua_checkstack(flt_hlua->T, 3)) { |
| 10698 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10699 | RESET_SAFE_LJMP(flt_hlua); |
| 10700 | goto end; |
| 10701 | } |
| 10702 | |
| 10703 | lua_rawgeti(flt_hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 10704 | if (lua_getfield(flt_hlua->T, -1, fun) != LUA_TFUNCTION) { |
| 10705 | RESET_SAFE_LJMP(flt_hlua); |
| 10706 | goto end; |
| 10707 | } |
| 10708 | lua_insert(flt_hlua->T, -2); |
| 10709 | |
| 10710 | if (!hlua_txn_new(flt_hlua->T, s, s->be, dir, hflags)) { |
| 10711 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10712 | RESET_SAFE_LJMP(flt_hlua); |
| 10713 | goto end; |
| 10714 | } |
| 10715 | flt_hlua->nargs = 2; |
| 10716 | |
| 10717 | if (flags & HLUA_FLT_CB_ARG_CHN) { |
| 10718 | if (dir == SMP_OPT_DIR_REQ) |
| 10719 | lua_getfield(flt_hlua->T, -1, "req"); |
| 10720 | else |
| 10721 | lua_getfield(flt_hlua->T, -1, "res"); |
| 10722 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10723 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10724 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10725 | lua_settable(flt_hlua->T, -3); |
| 10726 | } |
| 10727 | flt_hlua->nargs++; |
| 10728 | } |
| 10729 | else if (flags & HLUA_FLT_CB_ARG_HTTP_MSG) { |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10730 | if (dir == SMP_OPT_DIR_REQ) |
| 10731 | lua_getfield(flt_hlua->T, -1, "http_req"); |
| 10732 | else |
| 10733 | lua_getfield(flt_hlua->T, -1, "http_res"); |
| 10734 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10735 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10736 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10737 | lua_settable(flt_hlua->T, -3); |
| 10738 | } |
| 10739 | flt_hlua->nargs++; |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10740 | } |
| 10741 | |
| 10742 | /* Check stack size. */ |
| 10743 | if (!lua_checkstack(flt_hlua->T, 1)) { |
| 10744 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10745 | RESET_SAFE_LJMP(flt_hlua); |
| 10746 | goto end; |
| 10747 | } |
| 10748 | |
| 10749 | while (extra_idx--) { |
| 10750 | lua_pushvalue(flt_hlua->T, 1); |
| 10751 | lua_remove(flt_hlua->T, 1); |
| 10752 | flt_hlua->nargs++; |
| 10753 | } |
| 10754 | |
| 10755 | /* We must initialize the execution timeouts. */ |
| 10756 | flt_hlua->max_time = hlua_timeout_session; |
| 10757 | |
| 10758 | /* At this point the execution is safe. */ |
| 10759 | RESET_SAFE_LJMP(flt_hlua); |
| 10760 | } |
| 10761 | |
| 10762 | switch (hlua_ctx_resume(flt_hlua, !(flags & HLUA_FLT_CB_FINAL))) { |
| 10763 | case HLUA_E_OK: |
| 10764 | /* Catch the return value if it required */ |
| 10765 | if ((flags & HLUA_FLT_CB_RETVAL) && lua_gettop(flt_hlua->T) > 0) { |
| 10766 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10767 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10768 | } |
| 10769 | |
| 10770 | /* Set timeout in the required channel. */ |
| 10771 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10772 | if (dir == SMP_OPT_DIR_REQ) |
| 10773 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10774 | else |
| 10775 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10776 | } |
| 10777 | break; |
| 10778 | case HLUA_E_AGAIN: |
| 10779 | /* Set timeout in the required channel. */ |
| 10780 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10781 | if (dir == SMP_OPT_DIR_REQ) |
| 10782 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10783 | else |
| 10784 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10785 | } |
| 10786 | /* Some actions can be wake up when a "write" event |
| 10787 | * is detected on a response channel. This is useful |
| 10788 | * only for actions targeted on the requests. |
| 10789 | */ |
| 10790 | if (HLUA_IS_WAKERESWR(flt_hlua)) |
| 10791 | s->res.flags |= CF_WAKE_WRITE; |
| 10792 | if (HLUA_IS_WAKEREQWR(flt_hlua)) |
| 10793 | s->req.flags |= CF_WAKE_WRITE; |
| 10794 | ret = 0; |
| 10795 | goto end; |
| 10796 | case HLUA_E_ERRMSG: |
| 10797 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(flt_hlua->T, -1)); |
| 10798 | ret = -1; |
| 10799 | goto end; |
| 10800 | case HLUA_E_ETMOUT: |
| 10801 | SEND_ERR(s->be, "Lua filter '%s' : '%s' callback execution timeout.\n", conf->reg->name, fun); |
| 10802 | goto end; |
| 10803 | case HLUA_E_NOMEM: |
| 10804 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10805 | goto end; |
| 10806 | case HLUA_E_YIELD: |
| 10807 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10808 | " are not allowed from '%s' callback.\n", conf->reg->name, fun); |
| 10809 | goto end; |
| 10810 | case HLUA_E_ERR: |
| 10811 | SEND_ERR(s->be, "Lua filter '%s': '%s' returns an unknown error.\n", conf->reg->name, fun); |
| 10812 | goto end; |
| 10813 | default: |
| 10814 | goto end; |
| 10815 | } |
| 10816 | |
| 10817 | |
| 10818 | end: |
| 10819 | return ret; |
| 10820 | } |
| 10821 | |
| 10822 | static int hlua_filter_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 10823 | { |
| 10824 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10825 | |
| 10826 | flt_ctx->flags = 0; |
| 10827 | return hlua_filter_callback(s, filter, "start_analyze", |
| 10828 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10829 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 10830 | } |
| 10831 | |
| 10832 | static int hlua_filter_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 10833 | { |
| 10834 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10835 | |
| 10836 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10837 | return hlua_filter_callback(s, filter, "end_analyze", |
| 10838 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10839 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 10840 | } |
| 10841 | |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10842 | static int hlua_filter_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 10843 | { |
| 10844 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10845 | |
| 10846 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10847 | return hlua_filter_callback(s, filter, "http_headers", |
| 10848 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10849 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10850 | } |
| 10851 | |
| 10852 | static int hlua_filter_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg, |
| 10853 | unsigned int offset, unsigned int len) |
| 10854 | { |
| 10855 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10856 | struct hlua *flt_hlua; |
| 10857 | int dir = (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 10858 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 10859 | int ret; |
| 10860 | |
| 10861 | flt_hlua = flt_ctx->hlua[idx]; |
| 10862 | flt_ctx->cur_off[idx] = offset; |
| 10863 | flt_ctx->cur_len[idx] = len; |
| 10864 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 10865 | ret = hlua_filter_callback(s, filter, "http_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10866 | if (ret != -1) { |
| 10867 | ret = flt_ctx->cur_len[idx]; |
| 10868 | if (lua_gettop(flt_hlua->T) > 0) { |
| 10869 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10870 | if (ret > flt_ctx->cur_len[idx]) |
| 10871 | ret = flt_ctx->cur_len[idx]; |
| 10872 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10873 | } |
| 10874 | } |
| 10875 | return ret; |
| 10876 | } |
| 10877 | |
| 10878 | static int hlua_filter_http_end(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 10879 | { |
| 10880 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10881 | |
| 10882 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10883 | return hlua_filter_callback(s, filter, "http_end", |
| 10884 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10885 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10886 | } |
| 10887 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10888 | static int hlua_filter_tcp_payload(struct stream *s, struct filter *filter, struct channel *chn, |
| 10889 | unsigned int offset, unsigned int len) |
| 10890 | { |
| 10891 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10892 | struct hlua *flt_hlua; |
| 10893 | int dir = (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 10894 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 10895 | int ret; |
| 10896 | |
| 10897 | flt_hlua = flt_ctx->hlua[idx]; |
| 10898 | flt_ctx->cur_off[idx] = offset; |
| 10899 | flt_ctx->cur_len[idx] = len; |
| 10900 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 10901 | ret = hlua_filter_callback(s, filter, "tcp_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_CHN)); |
| 10902 | if (ret != -1) { |
| 10903 | ret = flt_ctx->cur_len[idx]; |
| 10904 | if (lua_gettop(flt_hlua->T) > 0) { |
| 10905 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10906 | if (ret > flt_ctx->cur_len[idx]) |
| 10907 | ret = flt_ctx->cur_len[idx]; |
| 10908 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10909 | } |
| 10910 | } |
| 10911 | return ret; |
| 10912 | } |
| 10913 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10914 | static int hlua_filter_parse_fct(char **args, int *cur_arg, struct proxy *px, |
| 10915 | struct flt_conf *fconf, char **err, void *private) |
| 10916 | { |
| 10917 | struct hlua_reg_filter *reg_flt = private; |
| 10918 | lua_State *L; |
| 10919 | struct hlua_flt_config *conf = NULL; |
| 10920 | const char *flt_id = NULL; |
| 10921 | int state_id, pos, flt_flags = 0; |
| 10922 | struct flt_ops *hlua_flt_ops = NULL; |
| 10923 | |
| 10924 | state_id = reg_flt_to_stack_id(reg_flt); |
| 10925 | L = hlua_states[state_id]; |
| 10926 | |
| 10927 | /* Initialize the filter ops with default callbacks */ |
| 10928 | hlua_flt_ops = calloc(1, sizeof(*hlua_flt_ops)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10929 | if (!hlua_flt_ops) |
| 10930 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10931 | hlua_flt_ops->init = hlua_filter_init; |
| 10932 | hlua_flt_ops->deinit = hlua_filter_deinit; |
| 10933 | if (state_id) { |
| 10934 | /* Set per-thread callback if script is loaded per-thread */ |
| 10935 | hlua_flt_ops->init_per_thread = hlua_filter_init_per_thread; |
| 10936 | hlua_flt_ops->deinit_per_thread = hlua_filter_deinit_per_thread; |
| 10937 | } |
| 10938 | hlua_flt_ops->attach = hlua_filter_new; |
| 10939 | hlua_flt_ops->detach = hlua_filter_delete; |
| 10940 | |
| 10941 | /* Push the filter class on the stack and resolve all callbacks */ |
| 10942 | lua_rawgeti(L, LUA_REGISTRYINDEX, reg_flt->flt_ref[state_id]); |
| 10943 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10944 | if (lua_getfield(L, -1, "start_analyze") == LUA_TFUNCTION) |
| 10945 | hlua_flt_ops->channel_start_analyze = hlua_filter_start_analyze; |
| 10946 | lua_pop(L, 1); |
| 10947 | if (lua_getfield(L, -1, "end_analyze") == LUA_TFUNCTION) |
| 10948 | hlua_flt_ops->channel_end_analyze = hlua_filter_end_analyze; |
| 10949 | lua_pop(L, 1); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10950 | if (lua_getfield(L, -1, "http_headers") == LUA_TFUNCTION) |
| 10951 | hlua_flt_ops->http_headers = hlua_filter_http_headers; |
| 10952 | lua_pop(L, 1); |
| 10953 | if (lua_getfield(L, -1, "http_payload") == LUA_TFUNCTION) |
| 10954 | hlua_flt_ops->http_payload = hlua_filter_http_payload; |
| 10955 | lua_pop(L, 1); |
| 10956 | if (lua_getfield(L, -1, "http_end") == LUA_TFUNCTION) |
| 10957 | hlua_flt_ops->http_end = hlua_filter_http_end; |
| 10958 | lua_pop(L, 1); |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10959 | if (lua_getfield(L, -1, "tcp_payload") == LUA_TFUNCTION) |
| 10960 | hlua_flt_ops->tcp_payload = hlua_filter_tcp_payload; |
| 10961 | lua_pop(L, 1); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10962 | |
| 10963 | /* Get id and flags of the filter class */ |
| 10964 | if (lua_getfield(L, -1, "id") == LUA_TSTRING) |
| 10965 | flt_id = lua_tostring(L, -1); |
| 10966 | lua_pop(L, 1); |
| 10967 | if (lua_getfield(L, -1, "flags") == LUA_TNUMBER) |
| 10968 | flt_flags = lua_tointeger(L, -1); |
| 10969 | lua_pop(L, 1); |
| 10970 | |
| 10971 | /* Create the filter config */ |
| 10972 | conf = calloc(1, sizeof(*conf)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10973 | if (!conf) |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10974 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10975 | conf->reg = reg_flt; |
| 10976 | |
| 10977 | /* duplicate args */ |
| 10978 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++); |
| 10979 | conf->args = calloc(pos + 1, sizeof(*conf->args)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10980 | if (!conf->args) |
| 10981 | goto error; |
| 10982 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++) { |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10983 | conf->args[pos] = strdup(args[*cur_arg + 1 + pos]); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10984 | if (!conf->args[pos]) |
| 10985 | goto error; |
| 10986 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10987 | conf->args[pos] = NULL; |
| 10988 | *cur_arg += pos + 1; |
| 10989 | |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10990 | if (flt_id) { |
| 10991 | fconf->id = strdup(flt_id); |
| 10992 | if (!fconf->id) |
| 10993 | goto error; |
| 10994 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10995 | fconf->flags = flt_flags; |
| 10996 | fconf->conf = conf; |
| 10997 | fconf->ops = hlua_flt_ops; |
| 10998 | |
| 10999 | lua_settop(L, 0); |
| 11000 | return 0; |
| 11001 | |
| 11002 | error: |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11003 | 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] | 11004 | free(hlua_flt_ops); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11005 | if (conf && conf->args) { |
| 11006 | for (pos = 0; conf->args[pos]; pos++) |
| 11007 | free(conf->args[pos]); |
| 11008 | free(conf->args); |
| 11009 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11010 | free(conf); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11011 | free((char *)fconf->id); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11012 | lua_settop(L, 0); |
| 11013 | return -1; |
| 11014 | } |
| 11015 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11016 | __LJMP static int hlua_register_data_filter(lua_State *L) |
| 11017 | { |
| 11018 | struct filter *filter; |
| 11019 | struct channel *chn; |
| 11020 | |
| 11021 | MAY_LJMP(check_args(L, 2, "register_data_filter")); |
| 11022 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 11023 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 11024 | |
| 11025 | lua_getfield(L, 1, "__filter"); |
| 11026 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 11027 | filter = lua_touserdata (L, -1); |
| 11028 | lua_pop(L, 1); |
| 11029 | |
| 11030 | register_data_filter(chn_strm(chn), chn, filter); |
| 11031 | return 1; |
| 11032 | } |
| 11033 | |
| 11034 | __LJMP static int hlua_unregister_data_filter(lua_State *L) |
| 11035 | { |
| 11036 | struct filter *filter; |
| 11037 | struct channel *chn; |
| 11038 | |
| 11039 | MAY_LJMP(check_args(L, 2, "unregister_data_filter")); |
| 11040 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 11041 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 11042 | |
| 11043 | lua_getfield(L, 1, "__filter"); |
| 11044 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 11045 | filter = lua_touserdata (L, -1); |
| 11046 | lua_pop(L, 1); |
| 11047 | |
| 11048 | unregister_data_filter(chn_strm(chn), chn, filter); |
| 11049 | return 1; |
| 11050 | } |
| 11051 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11052 | /* This function is an LUA binding used for registering a filter. It expects a |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 11053 | * filter name used in the haproxy configuration file and a LUA function to |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11054 | * parse configuration arguments. |
| 11055 | */ |
| 11056 | __LJMP static int hlua_register_filter(lua_State *L) |
| 11057 | { |
| 11058 | struct buffer *trash; |
| 11059 | struct flt_kw_list *fkl; |
| 11060 | struct flt_kw *fkw; |
| 11061 | const char *name; |
| 11062 | struct hlua_reg_filter *reg_flt= NULL; |
| 11063 | int flt_ref, fun_ref; |
| 11064 | int len; |
| 11065 | |
| 11066 | MAY_LJMP(check_args(L, 3, "register_filter")); |
| 11067 | |
| 11068 | /* First argument : filter name. */ |
| 11069 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 11070 | |
| 11071 | /* Second argument : The filter class */ |
| 11072 | flt_ref = MAY_LJMP(hlua_checktable(L, 2)); |
| 11073 | |
| 11074 | /* Third argument : lua function. */ |
| 11075 | fun_ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 11076 | |
| 11077 | trash = get_trash_chunk(); |
| 11078 | chunk_printf(trash, "lua.%s", name); |
| 11079 | fkw = flt_find_kw(trash->area); |
| 11080 | if (fkw != NULL) { |
| 11081 | reg_flt = fkw->private; |
| 11082 | if (reg_flt->flt_ref[hlua_state_id] != -1 || reg_flt->fun_ref[hlua_state_id] != -1) { |
| 11083 | ha_warning("Trying to register filter 'lua.%s' more than once. " |
| 11084 | "This will become a hard error in version 2.5.\n", name); |
| 11085 | } |
| 11086 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 11087 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 11088 | return 0; |
| 11089 | } |
| 11090 | |
| 11091 | fkl = calloc(1, sizeof(*fkl) + sizeof(struct flt_kw) * 2); |
| 11092 | if (!fkl) |
| 11093 | goto alloc_error; |
| 11094 | fkl->scope = "HLUA"; |
| 11095 | |
| 11096 | reg_flt = new_hlua_reg_filter(name); |
| 11097 | if (!reg_flt) |
| 11098 | goto alloc_error; |
| 11099 | |
| 11100 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 11101 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 11102 | |
| 11103 | /* The filter keyword */ |
| 11104 | len = strlen("lua.") + strlen(name) + 1; |
| 11105 | fkl->kw[0].kw = calloc(1, len); |
| 11106 | if (!fkl->kw[0].kw) |
| 11107 | goto alloc_error; |
| 11108 | |
| 11109 | snprintf((char *)fkl->kw[0].kw, len, "lua.%s", name); |
| 11110 | |
| 11111 | fkl->kw[0].parse = hlua_filter_parse_fct; |
| 11112 | fkl->kw[0].private = reg_flt; |
| 11113 | memset(&fkl->kw[1], 0, sizeof(*fkl->kw)); |
| 11114 | |
| 11115 | /* Register this new filter */ |
| 11116 | flt_register_keywords(fkl); |
| 11117 | |
| 11118 | return 0; |
| 11119 | |
| 11120 | alloc_error: |
| 11121 | release_hlua_reg_filter(reg_flt); |
| 11122 | ha_free(&fkl); |
| 11123 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 11124 | return 0; /* Never reached */ |
| 11125 | } |
| 11126 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11127 | 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] | 11128 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11129 | char **err, unsigned int *timeout) |
| 11130 | { |
| 11131 | const char *error; |
| 11132 | |
| 11133 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 11134 | if (error == PARSE_TIME_OVER) { |
| 11135 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 11136 | args[1], args[0]); |
| 11137 | return -1; |
| 11138 | } |
| 11139 | else if (error == PARSE_TIME_UNDER) { |
| 11140 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 11141 | args[1], args[0]); |
| 11142 | return -1; |
| 11143 | } |
| 11144 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11145 | memprintf(err, "%s: invalid timeout", args[0]); |
| 11146 | return -1; |
| 11147 | } |
| 11148 | return 0; |
| 11149 | } |
| 11150 | |
| 11151 | 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] | 11152 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11153 | char **err) |
| 11154 | { |
| 11155 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11156 | file, line, err, &hlua_timeout_session); |
| 11157 | } |
| 11158 | |
| 11159 | 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] | 11160 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11161 | char **err) |
| 11162 | { |
| 11163 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11164 | file, line, err, &hlua_timeout_task); |
| 11165 | } |
| 11166 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11167 | 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] | 11168 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11169 | char **err) |
| 11170 | { |
| 11171 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11172 | file, line, err, &hlua_timeout_applet); |
| 11173 | } |
| 11174 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11175 | 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] | 11176 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11177 | char **err) |
| 11178 | { |
| 11179 | char *error; |
| 11180 | |
| 11181 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 11182 | if (*error != '\0') { |
| 11183 | memprintf(err, "%s: invalid number", args[0]); |
| 11184 | return -1; |
| 11185 | } |
| 11186 | return 0; |
| 11187 | } |
| 11188 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11189 | 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] | 11190 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11191 | char **err) |
| 11192 | { |
| 11193 | char *error; |
| 11194 | |
| 11195 | if (*(args[1]) == 0) { |
| 11196 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 11197 | return -1; |
| 11198 | } |
| 11199 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 11200 | if (*error != '\0') { |
| 11201 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 11202 | return -1; |
| 11203 | } |
| 11204 | return 0; |
| 11205 | } |
| 11206 | |
| 11207 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11208 | /* This function is called by the main configuration key "lua-load". It loads and |
| 11209 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 11210 | * the main lua entry point. |
| 11211 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 11212 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 11213 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11214 | * |
| 11215 | * In some error case, LUA set an error message in top of the stack. This function |
| 11216 | * 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] | 11217 | * |
| 11218 | * This function can fail with an abort() due to an Lua critical error. |
| 11219 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 11220 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11221 | */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11222 | static int hlua_load_state(char *filename, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11223 | { |
| 11224 | int error; |
| 11225 | |
| 11226 | /* Just load and compile the file. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11227 | error = luaL_loadfile(L, filename); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11228 | if (error) { |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11229 | memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1)); |
| 11230 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11231 | return -1; |
| 11232 | } |
| 11233 | |
| 11234 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11235 | error = lua_pcall(L, 0, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11236 | switch (error) { |
| 11237 | case LUA_OK: |
| 11238 | break; |
| 11239 | case LUA_ERRRUN: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11240 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1)); |
| 11241 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11242 | return -1; |
| 11243 | case LUA_ERRMEM: |
Thierry Fournier | de6145f | 2020-11-29 00:55:53 +0100 | [diff] [blame] | 11244 | memprintf(err, "Lua out of memory error\n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11245 | return -1; |
| 11246 | case LUA_ERRERR: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11247 | memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1)); |
| 11248 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11249 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 11250 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11251 | case LUA_ERRGCMM: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11252 | memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1)); |
| 11253 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11254 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 11255 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11256 | default: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11257 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1)); |
| 11258 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11259 | return -1; |
| 11260 | } |
| 11261 | |
| 11262 | return 0; |
| 11263 | } |
| 11264 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11265 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 11266 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11267 | char **err) |
| 11268 | { |
| 11269 | if (*(args[1]) == 0) { |
| 11270 | memprintf(err, "'%s' expects a file name as parameter.\n", args[0]); |
| 11271 | return -1; |
| 11272 | } |
| 11273 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11274 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11275 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11276 | ha_set_thread(NULL); |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 11277 | return hlua_load_state(args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11278 | } |
| 11279 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11280 | 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] | 11281 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11282 | char **err) |
| 11283 | { |
| 11284 | int len; |
| 11285 | |
| 11286 | if (*(args[1]) == 0) { |
| 11287 | memprintf(err, "'%s' expects a file as parameter.\n", args[0]); |
| 11288 | return -1; |
| 11289 | } |
| 11290 | |
| 11291 | if (per_thread_load == NULL) { |
| 11292 | /* allocate the first entry large enough to store the final NULL */ |
| 11293 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 11294 | if (per_thread_load == NULL) { |
| 11295 | memprintf(err, "out of memory error"); |
| 11296 | return -1; |
| 11297 | } |
| 11298 | } |
| 11299 | |
| 11300 | /* count used entries */ |
| 11301 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 11302 | ; |
| 11303 | |
| 11304 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 11305 | if (per_thread_load == NULL) { |
| 11306 | memprintf(err, "out of memory error"); |
| 11307 | return -1; |
| 11308 | } |
| 11309 | |
| 11310 | per_thread_load[len] = strdup(args[1]); |
| 11311 | per_thread_load[len + 1] = NULL; |
| 11312 | |
| 11313 | if (per_thread_load[len] == NULL) { |
| 11314 | memprintf(err, "out of memory error"); |
| 11315 | return -1; |
| 11316 | } |
| 11317 | |
| 11318 | /* loading for thread 1 only */ |
| 11319 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11320 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11321 | return hlua_load_state(args[1], hlua_states[1], err); |
| 11322 | } |
| 11323 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11324 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 11325 | * in the given <ctx>. |
| 11326 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 11327 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11328 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 11329 | lua_getglobal(L, "package"); /* push package variable */ |
| 11330 | lua_pushstring(L, path); /* push given path */ |
| 11331 | lua_pushstring(L, ";"); /* push semicolon */ |
| 11332 | lua_getfield(L, -3, type); /* push old path */ |
| 11333 | lua_concat(L, 3); /* concatenate to new path */ |
| 11334 | lua_setfield(L, -2, type); /* store new path */ |
| 11335 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11336 | |
| 11337 | return 0; |
| 11338 | } |
| 11339 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11340 | 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] | 11341 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11342 | char **err) |
| 11343 | { |
| 11344 | char *path; |
| 11345 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11346 | struct prepend_path *p = NULL; |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 11347 | size_t i; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11348 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11349 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11350 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11351 | } |
| 11352 | |
| 11353 | if (!(*args[1])) { |
| 11354 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11355 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11356 | } |
| 11357 | path = args[1]; |
| 11358 | |
| 11359 | if (*args[2]) { |
| 11360 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 11361 | 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] | 11362 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11363 | } |
| 11364 | type = args[2]; |
| 11365 | } |
| 11366 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11367 | p = calloc(1, sizeof(*p)); |
| 11368 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11369 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11370 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11371 | } |
| 11372 | p->path = strdup(path); |
| 11373 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11374 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11375 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11376 | } |
| 11377 | p->type = strdup(type); |
| 11378 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11379 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11380 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11381 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 11382 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11383 | |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 11384 | /* Handle the global state and the per-thread state for the first |
| 11385 | * thread. The remaining threads will be initialized based on |
| 11386 | * prepend_path_list. |
| 11387 | */ |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 11388 | for (i = 0; i < 2; i++) { |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 11389 | lua_State *L = hlua_states[i]; |
| 11390 | const char *error; |
| 11391 | |
| 11392 | if (setjmp(safe_ljmp_env) != 0) { |
| 11393 | lua_atpanic(L, hlua_panic_safe); |
| 11394 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11395 | error = lua_tostring(L, -1); |
| 11396 | else |
| 11397 | error = "critical error"; |
| 11398 | fprintf(stderr, "lua-prepend-path: %s.\n", error); |
| 11399 | exit(1); |
| 11400 | } else { |
| 11401 | lua_atpanic(L, hlua_panic_ljmp); |
| 11402 | } |
| 11403 | |
| 11404 | hlua_prepend_path(L, type, path); |
| 11405 | |
| 11406 | lua_atpanic(L, hlua_panic_safe); |
| 11407 | } |
| 11408 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11409 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11410 | |
| 11411 | err2: |
| 11412 | free(p->type); |
| 11413 | free(p->path); |
| 11414 | err: |
| 11415 | free(p); |
| 11416 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11417 | } |
| 11418 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11419 | /* configuration keywords declaration */ |
| 11420 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11421 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11422 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11423 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11424 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 11425 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 11426 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11427 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11428 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11429 | { 0, NULL, NULL }, |
| 11430 | }}; |
| 11431 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 11432 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 11433 | |
William Lallemand | 5213918 | 2022-03-30 15:05:42 +0200 | [diff] [blame] | 11434 | #ifdef USE_OPENSSL |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 11435 | |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11436 | /* |
| 11437 | * This function replace a ckch_store by another one, and rebuild the ckch_inst and all its dependencies. |
| 11438 | * It does the sam as "cli_io_handler_commit_cert" but for lua, the major |
| 11439 | * difference is that the yield in lua and for the CLI is not handled the same |
| 11440 | * way. |
| 11441 | */ |
| 11442 | __LJMP static int hlua_ckch_commit_yield(lua_State *L, int status, lua_KContext ctx) |
| 11443 | { |
| 11444 | struct ckch_inst **lua_ckchi = lua_touserdata(L, -1); |
| 11445 | struct ckch_store **lua_ckchs = lua_touserdata(L, -2); |
| 11446 | struct ckch_inst *ckchi = *lua_ckchi; |
| 11447 | struct ckch_store *old_ckchs = lua_ckchs[0]; |
| 11448 | struct ckch_store *new_ckchs = lua_ckchs[1]; |
| 11449 | struct hlua *hlua; |
| 11450 | char *err = NULL; |
| 11451 | int y = 1; |
| 11452 | |
| 11453 | hlua = hlua_gethlua(L); |
| 11454 | |
| 11455 | /* get the first ckchi to copy */ |
| 11456 | if (ckchi == NULL) |
| 11457 | ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs); |
| 11458 | |
| 11459 | /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */ |
| 11460 | list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) { |
| 11461 | struct ckch_inst *new_inst; |
| 11462 | |
| 11463 | /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */ |
| 11464 | if (y % 10 == 0) { |
| 11465 | |
| 11466 | *lua_ckchi = ckchi; |
| 11467 | |
| 11468 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 11469 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_ckch_commit_yield, TICK_ETERNITY, 0)); |
| 11470 | } |
| 11471 | |
| 11472 | if (ckch_inst_rebuild(new_ckchs, ckchi, &new_inst, &err)) |
| 11473 | goto error; |
| 11474 | |
| 11475 | /* link the new ckch_inst to the duplicate */ |
| 11476 | LIST_APPEND(&new_ckchs->ckch_inst, &new_inst->by_ckchs); |
| 11477 | y++; |
| 11478 | } |
| 11479 | |
| 11480 | /* The generation is finished, we can insert everything */ |
| 11481 | ckch_store_replace(old_ckchs, new_ckchs); |
| 11482 | |
| 11483 | lua_pop(L, 2); /* pop the lua_ckchs and ckchi */ |
| 11484 | |
| 11485 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11486 | |
| 11487 | return 0; |
| 11488 | |
| 11489 | error: |
| 11490 | ckch_store_free(new_ckchs); |
| 11491 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11492 | WILL_LJMP(luaL_error(L, "%s", err)); |
| 11493 | free(err); |
| 11494 | |
| 11495 | return 0; |
| 11496 | } |
| 11497 | |
| 11498 | /* |
| 11499 | * Replace a ckch_store <filename> in the ckchs_tree with a ckch_store created |
| 11500 | * from the table in parameter. |
| 11501 | * |
| 11502 | * This is equivalent to "set ssl cert" + "commit ssl cert" over the CLI, which |
| 11503 | * means it does not need to have a transaction since everything is done in the |
| 11504 | * same function. |
| 11505 | * |
| 11506 | * CertCache.set{filename="", crt="", key="", sctl="", ocsp="", issuer=""} |
| 11507 | * |
| 11508 | */ |
| 11509 | __LJMP static int hlua_ckch_set(lua_State *L) |
| 11510 | { |
| 11511 | struct hlua *hlua; |
| 11512 | struct ckch_inst **lua_ckchi; |
| 11513 | struct ckch_store **lua_ckchs; |
| 11514 | struct ckch_store *old_ckchs = NULL; |
| 11515 | struct ckch_store *new_ckchs = NULL; |
| 11516 | int errcode = 0; |
| 11517 | char *err = NULL; |
| 11518 | struct cert_exts *cert_ext = NULL; |
| 11519 | char *filename; |
| 11520 | struct cert_key_and_chain *ckch; |
| 11521 | int ret; |
| 11522 | |
| 11523 | if (lua_type(L, -1) != LUA_TTABLE) |
| 11524 | WILL_LJMP(luaL_error(L, "'CertCache.set' needs a table as argument")); |
| 11525 | |
| 11526 | hlua = hlua_gethlua(L); |
| 11527 | |
| 11528 | /* FIXME: this should not return an error but should come back later */ |
| 11529 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 11530 | WILL_LJMP(luaL_error(L, "CertCache already under lock")); |
| 11531 | |
| 11532 | ret = lua_getfield(L, -1, "filename"); |
| 11533 | if (ret != LUA_TSTRING) { |
| 11534 | memprintf(&err, "%sNo filename specified!\n", err ? err : ""); |
| 11535 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11536 | goto end; |
| 11537 | } |
| 11538 | filename = (char *)lua_tostring(L, -1); |
| 11539 | |
| 11540 | |
| 11541 | /* look for the filename in the tree */ |
| 11542 | old_ckchs = ckchs_lookup(filename); |
| 11543 | if (!old_ckchs) { |
| 11544 | memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n", err ? err : ""); |
| 11545 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11546 | goto end; |
| 11547 | } |
| 11548 | /* TODO: handle extra_files_noext */ |
| 11549 | |
| 11550 | new_ckchs = ckchs_dup(old_ckchs); |
| 11551 | if (!new_ckchs) { |
| 11552 | memprintf(&err, "%sCannot allocate memory!\n", err ? err : ""); |
| 11553 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11554 | goto end; |
| 11555 | } |
| 11556 | |
| 11557 | ckch = new_ckchs->ckch; |
| 11558 | |
| 11559 | /* loop on the field in the table, which have the same name as the |
| 11560 | * possible extensions of files */ |
| 11561 | lua_pushnil(L); |
| 11562 | while (lua_next(L, 1)) { |
| 11563 | int i; |
| 11564 | const char *field = lua_tostring(L, -2); |
| 11565 | char *payload = (char *)lua_tostring(L, -1); |
| 11566 | |
| 11567 | if (!field || strcmp(field, "filename") == 0) { |
| 11568 | lua_pop(L, 1); |
| 11569 | continue; |
| 11570 | } |
| 11571 | |
| 11572 | for (i = 0; field && cert_exts[i].ext != NULL; i++) { |
| 11573 | if (strcmp(field, cert_exts[i].ext) == 0) { |
| 11574 | cert_ext = &cert_exts[i]; |
| 11575 | break; |
| 11576 | } |
| 11577 | } |
| 11578 | |
| 11579 | /* this is the default type, the field is not supported */ |
| 11580 | if (cert_ext == NULL) { |
| 11581 | memprintf(&err, "%sUnsupported field '%s'\n", err ? err : "", field); |
| 11582 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11583 | goto end; |
| 11584 | } |
| 11585 | |
| 11586 | /* appply the change on the duplicate */ |
| 11587 | if (cert_exts->load(filename, payload, ckch, &err) != 0) { |
| 11588 | memprintf(&err, "%sCan't load the payload\n", err ? err : ""); |
| 11589 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11590 | goto end; |
| 11591 | } |
| 11592 | lua_pop(L, 1); |
| 11593 | } |
| 11594 | |
| 11595 | /* store the pointers on the lua stack */ |
| 11596 | lua_ckchs = lua_newuserdata(L, sizeof(struct ckch_store *) * 2); |
| 11597 | lua_ckchs[0] = old_ckchs; |
| 11598 | lua_ckchs[1] = new_ckchs; |
| 11599 | lua_ckchi = lua_newuserdata(L, sizeof(struct ckch_inst *)); |
| 11600 | *lua_ckchi = NULL; |
| 11601 | |
| 11602 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 11603 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_ckch_commit_yield, TICK_ETERNITY, 0)); |
| 11604 | |
| 11605 | end: |
| 11606 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11607 | |
| 11608 | if (errcode & ERR_CODE) { |
| 11609 | ckch_store_free(new_ckchs); |
| 11610 | WILL_LJMP(luaL_error(L, "%s", err)); |
| 11611 | } |
| 11612 | free(err); |
| 11613 | |
| 11614 | return 0; |
| 11615 | } |
| 11616 | |
William Lallemand | 5213918 | 2022-03-30 15:05:42 +0200 | [diff] [blame] | 11617 | #else |
| 11618 | |
| 11619 | __LJMP static int hlua_ckch_set(lua_State *L) |
| 11620 | { |
| 11621 | WILL_LJMP(luaL_error(L, "'CertCache.set' needs an HAProxy built with OpenSSL")); |
| 11622 | |
| 11623 | return 0; |
| 11624 | } |
| 11625 | #endif /* ! USE_OPENSSL */ |
| 11626 | |
| 11627 | |
| 11628 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 11629 | /* This function can fail with an abort() due to an Lua critical error. |
| 11630 | * We are in the initialisation process of HAProxy, this abort() is |
| 11631 | * tolerated. |
| 11632 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11633 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11634 | { |
| 11635 | struct hlua_init_function *init; |
| 11636 | const char *msg; |
| 11637 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 11638 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11639 | const char *kind; |
| 11640 | const char *trace; |
| 11641 | int return_status = 1; |
| 11642 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 11643 | int nres; |
| 11644 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11645 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11646 | /* disable memory limit checks if limit is not set */ |
| 11647 | if (!hlua_global_allocator.limit) |
| 11648 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 11649 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 11650 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11651 | if (setjmp(safe_ljmp_env) != 0) { |
| 11652 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11653 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11654 | error = lua_tostring(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11655 | else |
| 11656 | error = "critical error"; |
| 11657 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 11658 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11659 | } else { |
| 11660 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11661 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 11662 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11663 | hlua_fcn_post_init(L); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11664 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11665 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11666 | lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11667 | |
| 11668 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11669 | ret = lua_resume(L, L, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11670 | #else |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11671 | ret = lua_resume(L, L, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11672 | #endif |
| 11673 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11674 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11675 | |
| 11676 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11677 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 11678 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11679 | |
| 11680 | case LUA_ERRERR: |
| 11681 | kind = "message handler error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11682 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11683 | case LUA_ERRRUN: |
| 11684 | if (!kind) |
| 11685 | kind = "runtime error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11686 | msg = lua_tostring(L, -1); |
| 11687 | lua_settop(L, 0); /* Empty the stack. */ |
| 11688 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 11689 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11690 | if (msg) |
| 11691 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 11692 | else |
| 11693 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
| 11694 | return_status = 0; |
| 11695 | break; |
| 11696 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11697 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11698 | /* Unknown error */ |
| 11699 | kind = "Unknown error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11700 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11701 | case LUA_YIELD: |
| 11702 | /* yield is not configured at this step, this state doesn't happen */ |
| 11703 | if (!kind) |
| 11704 | kind = "yield not allowed"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11705 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11706 | case LUA_ERRMEM: |
| 11707 | if (!kind) |
| 11708 | kind = "out of memory error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11709 | lua_settop(L, 0); |
| 11710 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 11711 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11712 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 11713 | return_status = 0; |
| 11714 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11715 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11716 | if (!return_status) |
| 11717 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11718 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11719 | |
| 11720 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11721 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11722 | } |
| 11723 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11724 | int hlua_post_init() |
| 11725 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11726 | int ret; |
| 11727 | int i; |
| 11728 | int errors; |
| 11729 | char *err = NULL; |
| 11730 | struct hlua_function *fcn; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11731 | struct hlua_reg_filter *reg_flt; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11732 | |
Willy Tarreau | b131049 | 2021-08-30 09:35:18 +0200 | [diff] [blame] | 11733 | #if defined(USE_OPENSSL) |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11734 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11735 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11736 | int saved_used_backed = global.ssl_used_backend; |
| 11737 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11738 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11739 | global.ssl_used_backend = saved_used_backed; |
| 11740 | } |
| 11741 | #endif |
| 11742 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11743 | /* Perform post init of common thread */ |
| 11744 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11745 | ha_set_thread(&ha_thread_info[0]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11746 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 11747 | if (ret == 0) |
| 11748 | return 0; |
| 11749 | |
| 11750 | /* init remaining lua states and load files */ |
| 11751 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 11752 | |
| 11753 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11754 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11755 | |
| 11756 | /* Init lua state */ |
| 11757 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 11758 | |
| 11759 | /* Load lua files */ |
| 11760 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 11761 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 11762 | if (ret != 0) { |
| 11763 | ha_alert("Lua init: %s\n", err); |
| 11764 | return 0; |
| 11765 | } |
| 11766 | } |
| 11767 | } |
| 11768 | |
| 11769 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11770 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11771 | |
| 11772 | /* Execute post init for all states */ |
| 11773 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 11774 | |
| 11775 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11776 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11777 | |
| 11778 | /* run post init */ |
| 11779 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 11780 | if (ret == 0) |
| 11781 | return 0; |
| 11782 | } |
| 11783 | |
| 11784 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11785 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11786 | |
| 11787 | /* control functions registering. Each function must have: |
| 11788 | * - only the function_ref[0] set positive and all other to -1 |
| 11789 | * - only the function_ref[0] set to -1 and all other positive |
| 11790 | * This ensure a same reference is not used both in shared |
| 11791 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11792 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11793 | * complicated to found for the end user. |
| 11794 | */ |
| 11795 | errors = 0; |
| 11796 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 11797 | ret = 0; |
| 11798 | for (i = 1; i < global.nbthread + 1; i++) { |
| 11799 | if (fcn->function_ref[i] == -1) |
| 11800 | ret--; |
| 11801 | else |
| 11802 | ret++; |
| 11803 | } |
| 11804 | if (abs(ret) != global.nbthread) { |
| 11805 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 11806 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 11807 | errors++; |
| 11808 | continue; |
| 11809 | } |
| 11810 | |
| 11811 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11812 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 11813 | "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] | 11814 | "exclusive.\n", fcn->name); |
| 11815 | errors++; |
| 11816 | } |
| 11817 | } |
| 11818 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11819 | /* Do the same with registered filters */ |
| 11820 | list_for_each_entry(reg_flt, &referenced_filters, l) { |
| 11821 | ret = 0; |
| 11822 | for (i = 1; i < global.nbthread + 1; i++) { |
| 11823 | if (reg_flt->flt_ref[i] == -1) |
| 11824 | ret--; |
| 11825 | else |
| 11826 | ret++; |
| 11827 | } |
| 11828 | if (abs(ret) != global.nbthread) { |
| 11829 | ha_alert("Lua filter '%s' is not referenced in all thread. " |
| 11830 | "Expect function in all thread or in none thread.\n", reg_flt->name); |
| 11831 | errors++; |
| 11832 | continue; |
| 11833 | } |
| 11834 | |
| 11835 | if ((reg_flt->flt_ref[0] == -1) == (ret < 0)) { |
| 11836 | ha_alert("Lua filter '%s' is referenced both ins shared Lua context (through lua-load) " |
| 11837 | "and per-thread Lua context (through lua-load-per-thread). these two context " |
| 11838 | "exclusive.\n", fcn->name); |
| 11839 | errors++; |
| 11840 | } |
| 11841 | } |
| 11842 | |
| 11843 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11844 | if (errors > 0) |
| 11845 | return 0; |
| 11846 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11847 | /* 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] | 11848 | * -1 in order to have probably a segfault if someone use it |
| 11849 | */ |
| 11850 | hlua_state_id = -1; |
| 11851 | |
| 11852 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11853 | } |
| 11854 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11855 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 11856 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 11857 | * 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] | 11858 | * allocation. <nsize> is the requested new size. A new allocation is |
| 11859 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11860 | * zero. This one verifies that the limits are respected but is optimized |
| 11861 | * for the fast case where limits are not used, hence stats are not updated. |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 11862 | * |
| 11863 | * Warning: while this API ressembles glibc's realloc() a lot, glibc surpasses |
| 11864 | * POSIX by making realloc(ptr,0) an effective free(), but others do not do |
| 11865 | * that and will simply allocate zero as if it were the result of malloc(0), |
| 11866 | * so mapping this onto realloc() will lead to memory leaks on non-glibc |
| 11867 | * systems. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11868 | */ |
| 11869 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 11870 | { |
| 11871 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11872 | size_t limit, old, new; |
| 11873 | |
| 11874 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 11875 | * for accounting anymore. |
| 11876 | */ |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 11877 | if (likely(~zone->limit == 0)) { |
| 11878 | if (!nsize) |
| 11879 | ha_free(&ptr); |
| 11880 | else |
| 11881 | ptr = realloc(ptr, nsize); |
| 11882 | return ptr; |
| 11883 | } |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11884 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 11885 | if (!ptr) |
| 11886 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11887 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11888 | /* enforce strict limits across all threads */ |
| 11889 | limit = zone->limit; |
| 11890 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 11891 | do { |
| 11892 | new = old + nsize - osize; |
| 11893 | if (unlikely(nsize && limit && new > limit)) |
| 11894 | return NULL; |
| 11895 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11896 | |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 11897 | if (!nsize) |
| 11898 | ha_free(&ptr); |
| 11899 | else |
| 11900 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11901 | |
| 11902 | if (unlikely(!ptr && nsize)) // failed |
| 11903 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 11904 | |
| 11905 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11906 | return ptr; |
| 11907 | } |
| 11908 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11909 | /* 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] | 11910 | * We are in the initialisation process of HAProxy, this abort() is |
| 11911 | * tolerated. |
| 11912 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11913 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 11914 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11915 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11916 | int idx; |
| 11917 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11918 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11919 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 11920 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11921 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11922 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11923 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11924 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11925 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11926 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11927 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11928 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11929 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11930 | *context = NULL; |
| 11931 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 11932 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 11933 | * the Lua function can fail with an abort. We are in the initialisation |
| 11934 | * process of HAProxy, this abort() is tolerated. |
| 11935 | */ |
| 11936 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11937 | /* Call post initialisation function in safe environment. */ |
| 11938 | if (setjmp(safe_ljmp_env) != 0) { |
| 11939 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11940 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11941 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 11942 | else |
| 11943 | error_msg = "critical error"; |
| 11944 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 11945 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11946 | } else { |
| 11947 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 11948 | } |
| 11949 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11950 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11951 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 11952 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 11953 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 11954 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11955 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 11956 | #endif |
| 11957 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11958 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 11959 | #endif |
| 11960 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 11961 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11962 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11963 | /* Apply configured prepend path */ |
| 11964 | list_for_each_entry(pp, &prepend_path_list, l) |
| 11965 | hlua_prepend_path(L, pp->type, pp->path); |
| 11966 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11967 | /* |
| 11968 | * |
| 11969 | * Create "core" object. |
| 11970 | * |
| 11971 | */ |
| 11972 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 11973 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11974 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11975 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11976 | /* set the thread id */ |
| 11977 | hlua_class_const_int(L, "thread", thread_num); |
| 11978 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11979 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 11980 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11981 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11982 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11983 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11984 | hlua_class_function(L, "register_init", hlua_register_init); |
| 11985 | hlua_class_function(L, "register_task", hlua_register_task); |
| 11986 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 11987 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 11988 | hlua_class_function(L, "register_action", hlua_register_action); |
| 11989 | hlua_class_function(L, "register_service", hlua_register_service); |
| 11990 | hlua_class_function(L, "register_cli", hlua_register_cli); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11991 | hlua_class_function(L, "register_filter", hlua_register_filter); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11992 | hlua_class_function(L, "yield", hlua_yield); |
| 11993 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 11994 | hlua_class_function(L, "sleep", hlua_sleep); |
| 11995 | hlua_class_function(L, "msleep", hlua_msleep); |
| 11996 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 11997 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 11998 | hlua_class_function(L, "set_map", hlua_set_map); |
| 11999 | hlua_class_function(L, "del_map", hlua_del_map); |
| 12000 | hlua_class_function(L, "tcp", hlua_socket_new); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12001 | hlua_class_function(L, "httpclient", hlua_httpclient_new); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12002 | hlua_class_function(L, "log", hlua_log); |
| 12003 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 12004 | hlua_class_function(L, "Info", hlua_log_info); |
| 12005 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 12006 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 12007 | hlua_class_function(L, "done", hlua_done); |
| 12008 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12009 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12010 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12011 | |
| 12012 | /* |
| 12013 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12014 | * Create "act" object. |
| 12015 | * |
| 12016 | */ |
| 12017 | |
| 12018 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12019 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12020 | |
| 12021 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12022 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 12023 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 12024 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 12025 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 12026 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 12027 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 12028 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 12029 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12030 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12031 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 12032 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12033 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12034 | |
| 12035 | /* |
| 12036 | * |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12037 | * Create "Filter" object. |
| 12038 | * |
| 12039 | */ |
| 12040 | |
| 12041 | /* This table entry is the object "filter" base. */ |
| 12042 | lua_newtable(L); |
| 12043 | |
| 12044 | /* push flags and constants */ |
| 12045 | hlua_class_const_int(L, "CONTINUE", 1); |
| 12046 | hlua_class_const_int(L, "WAIT", 0); |
| 12047 | hlua_class_const_int(L, "ERROR", -1); |
| 12048 | |
| 12049 | hlua_class_const_int(L, "FLT_CFG_FL_HTX", FLT_CFG_FL_HTX); |
| 12050 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 12051 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
| 12052 | hlua_class_function(L, "register_data_filter", hlua_register_data_filter); |
| 12053 | hlua_class_function(L, "unregister_data_filter", hlua_unregister_data_filter); |
| 12054 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12055 | lua_setglobal(L, "filter"); |
| 12056 | |
| 12057 | /* |
| 12058 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12059 | * Register class Map |
| 12060 | * |
| 12061 | */ |
| 12062 | |
| 12063 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12064 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12065 | |
| 12066 | /* register pattern types. */ |
| 12067 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12068 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 12069 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12070 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12071 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 12072 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12073 | |
| 12074 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12075 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12076 | |
| 12077 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12078 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12079 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12080 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12081 | lua_pushstring(L, "__index"); |
| 12082 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12083 | |
| 12084 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12085 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 12086 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12087 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12088 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12089 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 12090 | /* Register previous table in the registry with reference and named entry. |
| 12091 | * The function hlua_register_metatable() pops the stack, so we |
| 12092 | * previously create a copy of the table. |
| 12093 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12094 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 12095 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12096 | |
| 12097 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12098 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12099 | |
| 12100 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12101 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12102 | |
| 12103 | /* |
| 12104 | * |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12105 | * Register "CertCache" class |
| 12106 | * |
| 12107 | */ |
| 12108 | |
| 12109 | /* Create and fill the metatable. */ |
| 12110 | lua_newtable(L); |
| 12111 | /* Register */ |
| 12112 | hlua_class_function(L, "set", hlua_ckch_set); |
| 12113 | lua_setglobal(L, CLASS_CERTCACHE); /* Create global object called Regex */ |
| 12114 | |
| 12115 | /* |
| 12116 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12117 | * Register class Channel |
| 12118 | * |
| 12119 | */ |
| 12120 | |
| 12121 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12122 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12123 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12124 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12125 | lua_pushstring(L, "__index"); |
| 12126 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12127 | |
| 12128 | /* Register . */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12129 | hlua_class_function(L, "data", hlua_channel_get_data); |
| 12130 | hlua_class_function(L, "line", hlua_channel_get_line); |
| 12131 | hlua_class_function(L, "set", hlua_channel_set_data); |
| 12132 | hlua_class_function(L, "remove", hlua_channel_del_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12133 | hlua_class_function(L, "append", hlua_channel_append); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12134 | hlua_class_function(L, "prepend", hlua_channel_prepend); |
| 12135 | hlua_class_function(L, "insert", hlua_channel_insert_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12136 | hlua_class_function(L, "send", hlua_channel_send); |
| 12137 | hlua_class_function(L, "forward", hlua_channel_forward); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12138 | hlua_class_function(L, "input", hlua_channel_get_in_len); |
| 12139 | hlua_class_function(L, "output", hlua_channel_get_out_len); |
| 12140 | hlua_class_function(L, "may_recv", hlua_channel_may_recv); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12141 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 12142 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12143 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12144 | /* Deprecated API */ |
| 12145 | hlua_class_function(L, "get", hlua_channel_get); |
| 12146 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 12147 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 12148 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 12149 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 12150 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12151 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12152 | |
| 12153 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12154 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12155 | |
| 12156 | /* |
| 12157 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12158 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12159 | * |
| 12160 | */ |
| 12161 | |
| 12162 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12163 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12164 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12165 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12166 | lua_pushstring(L, "__index"); |
| 12167 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12168 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12169 | /* Browse existing fetches and create the associated |
| 12170 | * object method. |
| 12171 | */ |
| 12172 | sf = NULL; |
| 12173 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12174 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 12175 | * by an underscore. |
| 12176 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 12177 | strlcpy2(trash.area, sf->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12178 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12179 | if (*p == '.' || *p == '-' || *p == '+') |
| 12180 | *p = '_'; |
| 12181 | |
| 12182 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12183 | lua_pushstring(L, trash.area); |
| 12184 | lua_pushlightuserdata(L, sf); |
| 12185 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 12186 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12187 | } |
| 12188 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12189 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12190 | |
| 12191 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12192 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12193 | |
| 12194 | /* |
| 12195 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12196 | * Register class Converters |
| 12197 | * |
| 12198 | */ |
| 12199 | |
| 12200 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12201 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12202 | |
| 12203 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12204 | lua_pushstring(L, "__index"); |
| 12205 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12206 | |
| 12207 | /* Browse existing converters and create the associated |
| 12208 | * object method. |
| 12209 | */ |
| 12210 | sc = NULL; |
| 12211 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12212 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 12213 | * by an underscore. |
| 12214 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 12215 | strlcpy2(trash.area, sc->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12216 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12217 | if (*p == '.' || *p == '-' || *p == '+') |
| 12218 | *p = '_'; |
| 12219 | |
| 12220 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12221 | lua_pushstring(L, trash.area); |
| 12222 | lua_pushlightuserdata(L, sc); |
| 12223 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 12224 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12225 | } |
| 12226 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12227 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12228 | |
| 12229 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12230 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12231 | |
| 12232 | /* |
| 12233 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12234 | * Register class HTTP |
| 12235 | * |
| 12236 | */ |
| 12237 | |
| 12238 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12239 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12240 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12241 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12242 | lua_pushstring(L, "__index"); |
| 12243 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12244 | |
| 12245 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12246 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 12247 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 12248 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 12249 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 12250 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 12251 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 12252 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 12253 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 12254 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 12255 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12256 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12257 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 12258 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 12259 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 12260 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 12261 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 12262 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 12263 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12264 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12265 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12266 | |
| 12267 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12268 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12269 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 12270 | /* |
| 12271 | * |
| 12272 | * Register class HTTPMessage |
| 12273 | * |
| 12274 | */ |
| 12275 | |
| 12276 | /* Create and fill the metatable. */ |
| 12277 | lua_newtable(L); |
| 12278 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 12279 | /* Create and fill the __index entry. */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 12280 | lua_pushstring(L, "__index"); |
| 12281 | lua_newtable(L); |
| 12282 | |
| 12283 | /* Register Lua functions. */ |
| 12284 | hlua_class_function(L, "is_resp", hlua_http_msg_is_resp); |
| 12285 | hlua_class_function(L, "get_stline", hlua_http_msg_get_stline); |
| 12286 | hlua_class_function(L, "get_headers", hlua_http_msg_get_headers); |
| 12287 | hlua_class_function(L, "del_header", hlua_http_msg_del_hdr); |
| 12288 | hlua_class_function(L, "rep_header", hlua_http_msg_rep_hdr); |
| 12289 | hlua_class_function(L, "rep_value", hlua_http_msg_rep_val); |
| 12290 | hlua_class_function(L, "add_header", hlua_http_msg_add_hdr); |
| 12291 | hlua_class_function(L, "set_header", hlua_http_msg_set_hdr); |
| 12292 | hlua_class_function(L, "set_method", hlua_http_msg_set_meth); |
| 12293 | hlua_class_function(L, "set_path", hlua_http_msg_set_path); |
| 12294 | hlua_class_function(L, "set_query", hlua_http_msg_set_query); |
| 12295 | hlua_class_function(L, "set_uri", hlua_http_msg_set_uri); |
| 12296 | hlua_class_function(L, "set_status", hlua_http_msg_set_status); |
| 12297 | hlua_class_function(L, "is_full", hlua_http_msg_is_full); |
| 12298 | hlua_class_function(L, "may_recv", hlua_http_msg_may_recv); |
| 12299 | hlua_class_function(L, "eom", hlua_http_msg_is_eom); |
| 12300 | hlua_class_function(L, "input", hlua_http_msg_get_in_len); |
| 12301 | hlua_class_function(L, "output", hlua_http_msg_get_out_len); |
| 12302 | |
| 12303 | hlua_class_function(L, "body", hlua_http_msg_get_body); |
| 12304 | hlua_class_function(L, "set", hlua_http_msg_set_data); |
| 12305 | hlua_class_function(L, "remove", hlua_http_msg_del_data); |
| 12306 | hlua_class_function(L, "append", hlua_http_msg_append); |
| 12307 | hlua_class_function(L, "prepend", hlua_http_msg_prepend); |
| 12308 | hlua_class_function(L, "insert", hlua_http_msg_insert_data); |
| 12309 | hlua_class_function(L, "set_eom", hlua_http_msg_set_eom); |
| 12310 | hlua_class_function(L, "unset_eom", hlua_http_msg_unset_eom); |
| 12311 | |
| 12312 | hlua_class_function(L, "send", hlua_http_msg_send); |
| 12313 | hlua_class_function(L, "forward", hlua_http_msg_forward); |
| 12314 | |
| 12315 | lua_rawset(L, -3); |
| 12316 | |
| 12317 | /* Register previous table in the registry with reference and named entry. */ |
| 12318 | class_http_msg_ref = hlua_register_metatable(L, CLASS_HTTP_MSG); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12319 | |
| 12320 | /* |
| 12321 | * |
| 12322 | * Register class HTTPClient |
| 12323 | * |
| 12324 | */ |
| 12325 | |
| 12326 | /* Create and fill the metatable. */ |
| 12327 | lua_newtable(L); |
| 12328 | lua_pushstring(L, "__index"); |
| 12329 | lua_newtable(L); |
| 12330 | hlua_class_function(L, "get", hlua_httpclient_get); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 12331 | hlua_class_function(L, "head", hlua_httpclient_head); |
| 12332 | hlua_class_function(L, "put", hlua_httpclient_put); |
| 12333 | hlua_class_function(L, "post", hlua_httpclient_post); |
| 12334 | hlua_class_function(L, "delete", hlua_httpclient_delete); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12335 | lua_settable(L, -3); /* Sets the __index entry. */ |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 12336 | /* Register the garbage collector entry. */ |
| 12337 | lua_pushstring(L, "__gc"); |
| 12338 | lua_pushcclosure(L, hlua_httpclient_gc, 0); |
| 12339 | lua_settable(L, -3); /* Push the last 2 entries in the table at index -3 */ |
| 12340 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12341 | |
| 12342 | |
| 12343 | class_httpclient_ref = hlua_register_metatable(L, CLASS_HTTPCLIENT); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12344 | /* |
| 12345 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12346 | * Register class AppletTCP |
| 12347 | * |
| 12348 | */ |
| 12349 | |
| 12350 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12351 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12352 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12353 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12354 | lua_pushstring(L, "__index"); |
| 12355 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12356 | |
| 12357 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12358 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 12359 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 12360 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 12361 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 12362 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 12363 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 12364 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 12365 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12366 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12367 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12368 | |
| 12369 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12370 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12371 | |
| 12372 | /* |
| 12373 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12374 | * Register class AppletHTTP |
| 12375 | * |
| 12376 | */ |
| 12377 | |
| 12378 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12379 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12380 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12381 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12382 | lua_pushstring(L, "__index"); |
| 12383 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12384 | |
| 12385 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12386 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 12387 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 12388 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 12389 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 12390 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 12391 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 12392 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 12393 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 12394 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 12395 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 12396 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12397 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12398 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12399 | |
| 12400 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12401 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12402 | |
| 12403 | /* |
| 12404 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12405 | * Register class TXN |
| 12406 | * |
| 12407 | */ |
| 12408 | |
| 12409 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12410 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12411 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12412 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12413 | lua_pushstring(L, "__index"); |
| 12414 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12415 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 12416 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12417 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 12418 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 12419 | hlua_class_function(L, "set_var", hlua_set_var); |
| 12420 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 12421 | hlua_class_function(L, "get_var", hlua_get_var); |
| 12422 | hlua_class_function(L, "done", hlua_txn_done); |
| 12423 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 12424 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 12425 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 12426 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 12427 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 12428 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 12429 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 12430 | hlua_class_function(L, "log", hlua_txn_log); |
| 12431 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 12432 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 12433 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 12434 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 12435 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12436 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12437 | |
| 12438 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12439 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12440 | |
| 12441 | /* |
| 12442 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 12443 | * Register class reply |
| 12444 | * |
| 12445 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12446 | lua_newtable(L); |
| 12447 | lua_pushstring(L, "__index"); |
| 12448 | lua_newtable(L); |
| 12449 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 12450 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 12451 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 12452 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 12453 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 12454 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 12455 | |
| 12456 | |
| 12457 | /* |
| 12458 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12459 | * Register class Socket |
| 12460 | * |
| 12461 | */ |
| 12462 | |
| 12463 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12464 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12465 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12466 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12467 | lua_pushstring(L, "__index"); |
| 12468 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12469 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 12470 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12471 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 12472 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12473 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 12474 | hlua_class_function(L, "send", hlua_socket_send); |
| 12475 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 12476 | hlua_class_function(L, "close", hlua_socket_close); |
| 12477 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 12478 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 12479 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 12480 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12481 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12482 | 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] | 12483 | |
| 12484 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12485 | lua_pushstring(L, "__gc"); |
| 12486 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 12487 | 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] | 12488 | |
| 12489 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12490 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12491 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12492 | lua_atpanic(L, hlua_panic_safe); |
| 12493 | |
| 12494 | return L; |
| 12495 | } |
| 12496 | |
| 12497 | void hlua_init(void) { |
| 12498 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 12499 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12500 | #ifdef USE_OPENSSL |
| 12501 | struct srv_kw *kw; |
| 12502 | int tmp_error; |
| 12503 | char *error; |
| 12504 | char *args[] = { /* SSL client configuration. */ |
| 12505 | "ssl", |
| 12506 | "verify", |
| 12507 | "none", |
| 12508 | NULL |
| 12509 | }; |
| 12510 | #endif |
| 12511 | |
| 12512 | /* Init post init function list head */ |
| 12513 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 12514 | LIST_INIT(&hlua_init_functions[i]); |
| 12515 | |
| 12516 | /* Init state for common/shared lua parts */ |
| 12517 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12518 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12519 | hlua_states[0] = hlua_init_state(0); |
| 12520 | |
| 12521 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 12522 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12523 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12524 | hlua_states[1] = hlua_init_state(1); |
| 12525 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12526 | /* Proxy and server configuration initialisation. */ |
William Lallemand | 6bb77b9 | 2021-07-28 15:48:16 +0200 | [diff] [blame] | 12527 | 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] | 12528 | if (!socket_proxy) { |
| 12529 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 12530 | exit(1); |
| 12531 | } |
| 12532 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12533 | |
| 12534 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12535 | socket_tcp = new_server(socket_proxy); |
| 12536 | if (!socket_tcp) { |
| 12537 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 12538 | exit(1); |
| 12539 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12540 | |
| 12541 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12542 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12543 | socket_ssl = new_server(socket_proxy); |
| 12544 | if (!socket_ssl) { |
| 12545 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 12546 | exit(1); |
| 12547 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12548 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12549 | socket_ssl->use_ssl = 1; |
| 12550 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12551 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 12552 | for (i = 0; args[i] != NULL; i++) { |
| 12553 | 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] | 12554 | /* |
| 12555 | * |
| 12556 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 12557 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12558 | * features like client certificates and ssl_verify. |
| 12559 | * |
| 12560 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12561 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12562 | if (tmp_error != 0) { |
| 12563 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 12564 | abort(); /* This must be never arrives because the command line |
| 12565 | not editable by the user. */ |
| 12566 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 12567 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12568 | } |
| 12569 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12570 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 12571 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 12572 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12573 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 12574 | static void hlua_deinit() |
| 12575 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 12576 | int thr; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12577 | struct hlua_reg_filter *reg_flt, *reg_flt_bck; |
| 12578 | |
| 12579 | list_for_each_entry_safe(reg_flt, reg_flt_bck, &referenced_filters, l) |
| 12580 | release_hlua_reg_filter(reg_flt); |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 12581 | |
| 12582 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 12583 | if (hlua_states[thr]) |
| 12584 | lua_close(hlua_states[thr]); |
| 12585 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 12586 | |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 12587 | srv_drop(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 12588 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 12589 | #ifdef USE_OPENSSL |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 12590 | srv_drop(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 12591 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 12592 | |
| 12593 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 12594 | } |
| 12595 | |
| 12596 | REGISTER_POST_DEINIT(hlua_deinit); |
| 12597 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12598 | static void hlua_register_build_options(void) |
| 12599 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12600 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12601 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12602 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 12603 | hap_register_build_opts(ptr, 1); |
| 12604 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12605 | |
| 12606 | INITCALL0(STG_REGISTER, hlua_register_build_options); |