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 | |
| 245 | DECLARE_STATIC_POOL(pool_head_hlua_flt_ctx, "hlua_flt_ctx", sizeof(struct hlua_flt_ctx)); |
| 246 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 247 | static int hlua_filter_from_payload(struct filter *filter); |
| 248 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 249 | /* This is the chained list of struct hlua_flt referenced |
| 250 | * for haproxy filters. It is used for a post-initialisation control. |
| 251 | */ |
| 252 | static struct list referenced_filters = LIST_HEAD_INIT(referenced_filters); |
| 253 | |
| 254 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 255 | /* This is the memory pool containing struct lua for applets |
| 256 | * (including cli). |
| 257 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 258 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 259 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 260 | /* Used for Socket connection. */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 261 | static struct proxy *socket_proxy; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 262 | static struct server *socket_tcp; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 263 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 264 | static struct server *socket_ssl; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 265 | #endif |
| 266 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 267 | /* List head of the function called at the initialisation time. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 268 | struct list hlua_init_functions[MAX_THREADS + 1]; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 269 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 270 | /* The following variables contains the reference of the different |
| 271 | * Lua classes. These references are useful for identify metadata |
| 272 | * associated with an object. |
| 273 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 274 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 275 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 276 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 277 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 278 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 279 | static int class_http_ref; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 280 | static int class_http_msg_ref; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 281 | static int class_httpclient_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 282 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 283 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 284 | static int class_applet_http_ref; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 285 | static int class_txn_reply_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 286 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 287 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 288 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 289 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 290 | * because a task may remain alive during all the haproxy execution. |
| 291 | */ |
| 292 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 293 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 294 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 295 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 296 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 297 | * it is used for preventing infinite loops. |
| 298 | * |
| 299 | * I test the scheer with an infinite loop containing one incrementation |
| 300 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 301 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 302 | * to one interrupt each 10 000 instructions. |
| 303 | * |
| 304 | * configured | Number of |
| 305 | * instructions | loops executed |
| 306 | * between two | in milions |
| 307 | * forced yields | |
| 308 | * ---------------+--------------- |
| 309 | * 10 | 160 |
| 310 | * 500 | 670 |
| 311 | * 1000 | 680 |
| 312 | * 5000 | 700 |
| 313 | * 7000 | 700 |
| 314 | * 8000 | 700 |
| 315 | * 9000 | 710 <- ceil |
| 316 | * 10000 | 710 |
| 317 | * 100000 | 710 |
| 318 | * 1000000 | 710 |
| 319 | * |
| 320 | */ |
| 321 | static unsigned int hlua_nb_instruction = 10000; |
| 322 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 323 | /* Descriptor for the memory allocation state. The limit is pre-initialised to |
| 324 | * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it |
| 325 | * is replaced with ~0 during post_init after everything was loaded. This way |
| 326 | * it is guaranteed that if limit is ~0 the boot is complete and that if it's |
| 327 | * zero it's not yet limited and proper accounting is required. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 328 | */ |
| 329 | struct hlua_mem_allocator { |
| 330 | size_t allocated; |
| 331 | size_t limit; |
| 332 | }; |
| 333 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 334 | static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 335 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 336 | /* These functions converts types between HAProxy internal args or |
| 337 | * sample and LUA types. Another function permits to check if the |
| 338 | * LUA stack contains arguments according with an required ARG_T |
| 339 | * format. |
| 340 | */ |
| 341 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 342 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 343 | __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] | 344 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 345 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 346 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 347 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 348 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 349 | __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] | 350 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 351 | struct prepend_path { |
| 352 | struct list l; |
| 353 | char *type; |
| 354 | char *path; |
| 355 | }; |
| 356 | |
| 357 | static struct list prepend_path_list = LIST_HEAD_INIT(prepend_path_list); |
| 358 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 359 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 360 | do { \ |
| 361 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 362 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 363 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 364 | } while (0) |
| 365 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 366 | static inline struct hlua_function *new_hlua_function() |
| 367 | { |
| 368 | struct hlua_function *fcn; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 369 | int i; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 370 | |
| 371 | fcn = calloc(1, sizeof(*fcn)); |
| 372 | if (!fcn) |
| 373 | return NULL; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 374 | LIST_APPEND(&referenced_functions, &fcn->l); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 375 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 376 | fcn->function_ref[i] = -1; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 377 | return fcn; |
| 378 | } |
| 379 | |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 380 | static inline void release_hlua_function(struct hlua_function *fcn) |
| 381 | { |
| 382 | if (!fcn) |
| 383 | return; |
| 384 | if (fcn->name) |
| 385 | ha_free(&fcn->name); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 386 | LIST_DELETE(&fcn->l); |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 387 | ha_free(&fcn); |
| 388 | } |
| 389 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 390 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 391 | static inline int fcn_ref_to_stack_id(struct hlua_function *fcn) |
| 392 | { |
| 393 | if (fcn->function_ref[0] == -1) |
| 394 | return tid + 1; |
| 395 | return 0; |
| 396 | } |
| 397 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 398 | /* Create a new registered filter. Only its name is filled */ |
| 399 | static inline struct hlua_reg_filter *new_hlua_reg_filter(const char *name) |
| 400 | { |
| 401 | struct hlua_reg_filter *reg_flt; |
| 402 | int i; |
| 403 | |
| 404 | reg_flt = calloc(1, sizeof(*reg_flt)); |
| 405 | if (!reg_flt) |
| 406 | return NULL; |
| 407 | reg_flt->name = strdup(name); |
| 408 | if (!reg_flt->name) { |
| 409 | free(reg_flt); |
| 410 | return NULL; |
| 411 | } |
| 412 | LIST_APPEND(&referenced_filters, ®_flt->l); |
| 413 | for (i = 0; i < MAX_THREADS + 1; i++) { |
| 414 | reg_flt->flt_ref[i] = -1; |
| 415 | reg_flt->fun_ref[i] = -1; |
| 416 | } |
| 417 | return reg_flt; |
| 418 | } |
| 419 | |
| 420 | /* Release a registered filter */ |
| 421 | static inline void release_hlua_reg_filter(struct hlua_reg_filter *reg_flt) |
| 422 | { |
| 423 | if (!reg_flt) |
| 424 | return; |
| 425 | if (reg_flt->name) |
| 426 | ha_free(®_flt->name); |
| 427 | LIST_DELETE(®_flt->l); |
| 428 | ha_free(®_flt); |
| 429 | } |
| 430 | |
| 431 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 432 | static inline int reg_flt_to_stack_id(struct hlua_reg_filter *reg_flt) |
| 433 | { |
| 434 | if (reg_flt->fun_ref[0] == -1) |
| 435 | return tid + 1; |
| 436 | return 0; |
| 437 | } |
| 438 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 439 | /* Used to check an Lua function type in the stack. It creates and |
| 440 | * returns a reference of the function. This function throws an |
| 441 | * error if the rgument is not a "function". |
| 442 | */ |
| 443 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 444 | { |
| 445 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 446 | 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] | 447 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 448 | } |
| 449 | lua_pushvalue(L, argno); |
| 450 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 451 | } |
| 452 | |
Christopher Faulet | ba9e21d | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 453 | /* Used to check an Lua table type in the stack. It creates and |
| 454 | * returns a reference of the table. This function throws an |
| 455 | * error if the rgument is not a "table". |
| 456 | */ |
| 457 | __LJMP unsigned int hlua_checktable(lua_State *L, int argno) |
| 458 | { |
| 459 | if (!lua_istable(L, argno)) { |
| 460 | const char *msg = lua_pushfstring(L, "table expected, got %s", luaL_typename(L, argno)); |
| 461 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 462 | } |
| 463 | lua_pushvalue(L, argno); |
| 464 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 465 | } |
| 466 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 467 | /* Return the string that is of the top of the stack. */ |
| 468 | const char *hlua_get_top_error_string(lua_State *L) |
| 469 | { |
| 470 | if (lua_gettop(L) < 1) |
| 471 | return "unknown error"; |
| 472 | if (lua_type(L, -1) != LUA_TSTRING) |
| 473 | return "unknown error"; |
| 474 | return lua_tostring(L, -1); |
| 475 | } |
| 476 | |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 477 | __LJMP const char *hlua_traceback(lua_State *L, const char* sep) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 478 | { |
| 479 | lua_Debug ar; |
| 480 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 481 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 482 | |
| 483 | while (lua_getstack(L, level++, &ar)) { |
| 484 | |
| 485 | /* Add separator */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 486 | if (b_data(msg)) |
| 487 | chunk_appendf(msg, "%s", sep); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 488 | |
| 489 | /* Fill fields: |
| 490 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 491 | * 'l': fills in the field currentline; |
| 492 | * 'n': fills in the field name and namewhat; |
| 493 | * 't': fills in the field istailcall; |
| 494 | */ |
| 495 | lua_getinfo(L, "Slnt", &ar); |
| 496 | |
| 497 | /* Append code localisation */ |
| 498 | if (ar.currentline > 0) |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 499 | chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 500 | else |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 501 | chunk_appendf(msg, "%s: ", ar.short_src); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 502 | |
| 503 | /* |
| 504 | * Get function name |
| 505 | * |
| 506 | * if namewhat is no empty, name is defined. |
| 507 | * what contains "Lua" for Lua function, "C" for C function, |
| 508 | * or "main" for main code. |
| 509 | */ |
| 510 | 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] | 511 | chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */ |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 512 | |
| 513 | 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] | 514 | chunk_appendf(msg, "in main chunk"); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 515 | |
| 516 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 517 | chunk_appendf(msg, "in function line %d", ar.linedefined); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 518 | |
| 519 | else /* nothing left... */ |
| 520 | chunk_appendf(msg, "?"); |
| 521 | |
| 522 | |
| 523 | /* Display tailed call */ |
| 524 | if (ar.istailcall) |
| 525 | chunk_appendf(msg, " ..."); |
| 526 | } |
| 527 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 528 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 532 | /* This function check the number of arguments available in the |
| 533 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 534 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 535 | */ |
| 536 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 537 | { |
| 538 | if (lua_gettop(L) == nb) |
| 539 | return; |
| 540 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 541 | } |
| 542 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 543 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 544 | * and the line number where the error is encountered. |
| 545 | */ |
| 546 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 547 | { |
| 548 | va_list argp; |
| 549 | va_start(argp, fmt); |
| 550 | luaL_where(L, 1); |
| 551 | lua_pushvfstring(L, fmt, argp); |
| 552 | va_end(argp); |
| 553 | lua_concat(L, 2); |
| 554 | return 1; |
| 555 | } |
| 556 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 557 | /* This functions is used with sample fetch and converters. It |
| 558 | * converts the HAProxy configuration argument in a lua stack |
| 559 | * values. |
| 560 | * |
| 561 | * It takes an array of "arg", and each entry of the array is |
| 562 | * converted and pushed in the LUA stack. |
| 563 | */ |
| 564 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 565 | { |
| 566 | switch (arg->type) { |
| 567 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 568 | case ARGT_TIME: |
| 569 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 570 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 571 | break; |
| 572 | |
| 573 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 574 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 575 | break; |
| 576 | |
| 577 | case ARGT_IPV4: |
| 578 | case ARGT_IPV6: |
| 579 | case ARGT_MSK4: |
| 580 | case ARGT_MSK6: |
| 581 | case ARGT_FE: |
| 582 | case ARGT_BE: |
| 583 | case ARGT_TAB: |
| 584 | case ARGT_SRV: |
| 585 | case ARGT_USR: |
| 586 | case ARGT_MAP: |
| 587 | default: |
| 588 | lua_pushnil(L); |
| 589 | break; |
| 590 | } |
| 591 | return 1; |
| 592 | } |
| 593 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 594 | /* 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] | 595 | * 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] | 596 | * with sample fetch wrappers. The input arguments are given to the |
| 597 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 598 | */ |
| 599 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 600 | { |
| 601 | switch (lua_type(L, ud)) { |
| 602 | |
| 603 | case LUA_TNUMBER: |
| 604 | case LUA_TBOOLEAN: |
| 605 | arg->type = ARGT_SINT; |
| 606 | arg->data.sint = lua_tointeger(L, ud); |
| 607 | break; |
| 608 | |
| 609 | case LUA_TSTRING: |
| 610 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 611 | 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] | 612 | /* 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] | 613 | 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] | 614 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 615 | break; |
| 616 | |
| 617 | case LUA_TUSERDATA: |
| 618 | case LUA_TNIL: |
| 619 | case LUA_TTABLE: |
| 620 | case LUA_TFUNCTION: |
| 621 | case LUA_TTHREAD: |
| 622 | case LUA_TLIGHTUSERDATA: |
| 623 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 624 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 625 | break; |
| 626 | } |
| 627 | return 1; |
| 628 | } |
| 629 | |
| 630 | /* the following functions are used to convert a struct sample |
| 631 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 632 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 633 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 634 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 635 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 636 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 637 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 638 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 639 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 640 | break; |
| 641 | |
| 642 | case SMP_T_BIN: |
| 643 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 644 | 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] | 645 | break; |
| 646 | |
| 647 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 648 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 649 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 650 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 651 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 652 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 653 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 654 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 655 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 656 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 657 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 658 | 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] | 659 | break; |
| 660 | default: |
| 661 | lua_pushnil(L); |
| 662 | break; |
| 663 | } |
| 664 | break; |
| 665 | |
| 666 | case SMP_T_IPV4: |
| 667 | case SMP_T_IPV6: |
| 668 | 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] | 669 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 670 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 671 | 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] | 672 | else |
| 673 | lua_pushnil(L); |
| 674 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 675 | default: |
| 676 | lua_pushnil(L); |
| 677 | break; |
| 678 | } |
| 679 | return 1; |
| 680 | } |
| 681 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 682 | /* the following functions are used to convert a struct sample |
| 683 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 684 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 685 | */ |
| 686 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 687 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 688 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 689 | |
| 690 | case SMP_T_BIN: |
| 691 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 692 | 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] | 693 | break; |
| 694 | |
| 695 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 696 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 697 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 698 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 699 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 700 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 701 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 702 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 703 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 704 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 705 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 706 | 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] | 707 | break; |
| 708 | default: |
| 709 | lua_pushstring(L, ""); |
| 710 | break; |
| 711 | } |
| 712 | break; |
| 713 | |
| 714 | case SMP_T_SINT: |
| 715 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 716 | case SMP_T_IPV4: |
| 717 | case SMP_T_IPV6: |
| 718 | 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] | 719 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 720 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 721 | 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] | 722 | else |
| 723 | lua_pushstring(L, ""); |
| 724 | break; |
| 725 | default: |
| 726 | lua_pushstring(L, ""); |
| 727 | break; |
| 728 | } |
| 729 | return 1; |
| 730 | } |
| 731 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 732 | /* the following functions are used to convert an Lua type in a |
| 733 | * struct sample. This is useful to provide data from a converter |
| 734 | * to the LUA code. |
| 735 | */ |
| 736 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 737 | { |
| 738 | switch (lua_type(L, ud)) { |
| 739 | |
| 740 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 741 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 742 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 743 | break; |
| 744 | |
| 745 | |
| 746 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 747 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 748 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 749 | break; |
| 750 | |
| 751 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 752 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 753 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 754 | 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] | 755 | /* 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] | 756 | 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] | 757 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 758 | break; |
| 759 | |
| 760 | case LUA_TUSERDATA: |
| 761 | case LUA_TNIL: |
| 762 | case LUA_TTABLE: |
| 763 | case LUA_TFUNCTION: |
| 764 | case LUA_TTHREAD: |
| 765 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 766 | case LUA_TNONE: |
| 767 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 768 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 769 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 770 | break; |
| 771 | } |
| 772 | return 1; |
| 773 | } |
| 774 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 775 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 776 | * 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] | 777 | * 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] | 778 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 779 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 780 | * 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] | 781 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 782 | __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] | 783 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 784 | { |
| 785 | int min_arg; |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 786 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 787 | struct proxy *px; |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 788 | struct userlist *ul; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 789 | struct my_regex *reg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 790 | const char *msg = NULL; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 791 | char *sname, *pname, *err = NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 792 | |
| 793 | idx = 0; |
| 794 | min_arg = ARGM(mask); |
| 795 | mask >>= ARGM_BITS; |
| 796 | |
| 797 | while (1) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 798 | struct buffer tmp = BUF_NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 799 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 800 | /* Check for mandatory arguments. */ |
| 801 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 802 | if (idx < min_arg) { |
| 803 | |
| 804 | /* If miss other argument than the first one, we return an error. */ |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 805 | if (idx > 0) { |
| 806 | msg = "Mandatory argument expected"; |
| 807 | goto error; |
| 808 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 809 | |
| 810 | /* If first argument have a certain type, some default values |
| 811 | * may be used. See the function smp_resolve_args(). |
| 812 | */ |
| 813 | switch (mask & ARGT_MASK) { |
| 814 | |
| 815 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 816 | if (!(p->cap & PR_CAP_FE)) { |
| 817 | msg = "Mandatory argument expected"; |
| 818 | goto error; |
| 819 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 820 | argp[idx].data.prx = p; |
| 821 | argp[idx].type = ARGT_FE; |
| 822 | argp[idx+1].type = ARGT_STOP; |
| 823 | break; |
| 824 | |
| 825 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 826 | if (!(p->cap & PR_CAP_BE)) { |
| 827 | msg = "Mandatory argument expected"; |
| 828 | goto error; |
| 829 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 830 | argp[idx].data.prx = p; |
| 831 | argp[idx].type = ARGT_BE; |
| 832 | argp[idx+1].type = ARGT_STOP; |
| 833 | break; |
| 834 | |
| 835 | case ARGT_TAB: |
| 836 | argp[idx].data.prx = p; |
| 837 | argp[idx].type = ARGT_TAB; |
| 838 | argp[idx+1].type = ARGT_STOP; |
| 839 | break; |
| 840 | |
| 841 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 842 | msg = "Mandatory argument expected"; |
| 843 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 844 | break; |
| 845 | } |
| 846 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 847 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 848 | } |
| 849 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 850 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 851 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 852 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 853 | msg = "Last argument expected"; |
| 854 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 858 | argp[idx].type == ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 859 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 860 | } |
| 861 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 862 | /* Convert some argument types. All string in argp[] are for not |
| 863 | * duplicated yet. |
| 864 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 865 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 866 | case ARGT_SINT: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 867 | if (argp[idx].type != ARGT_SINT) { |
| 868 | msg = "integer expected"; |
| 869 | goto error; |
| 870 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 871 | argp[idx].type = ARGT_SINT; |
| 872 | break; |
| 873 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 874 | case ARGT_TIME: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 875 | if (argp[idx].type != ARGT_SINT) { |
| 876 | msg = "integer expected"; |
| 877 | goto error; |
| 878 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 879 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 880 | break; |
| 881 | |
| 882 | case ARGT_SIZE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 883 | if (argp[idx].type != ARGT_SINT) { |
| 884 | msg = "integer expected"; |
| 885 | goto error; |
| 886 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 887 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 888 | break; |
| 889 | |
| 890 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 891 | if (argp[idx].type != ARGT_STR) { |
| 892 | msg = "string expected"; |
| 893 | goto error; |
| 894 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 895 | 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] | 896 | if (!argp[idx].data.prx) { |
| 897 | msg = "frontend doesn't exist"; |
| 898 | goto error; |
| 899 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 900 | argp[idx].type = ARGT_FE; |
| 901 | break; |
| 902 | |
| 903 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 904 | if (argp[idx].type != ARGT_STR) { |
| 905 | msg = "string expected"; |
| 906 | goto error; |
| 907 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 908 | 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] | 909 | if (!argp[idx].data.prx) { |
| 910 | msg = "backend doesn't exist"; |
| 911 | goto error; |
| 912 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 913 | argp[idx].type = ARGT_BE; |
| 914 | break; |
| 915 | |
| 916 | case ARGT_TAB: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 917 | if (argp[idx].type != ARGT_STR) { |
| 918 | msg = "string expected"; |
| 919 | goto error; |
| 920 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 921 | 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] | 922 | if (!argp[idx].data.t) { |
| 923 | msg = "table doesn't exist"; |
| 924 | goto error; |
| 925 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 926 | argp[idx].type = ARGT_TAB; |
| 927 | break; |
| 928 | |
| 929 | case ARGT_SRV: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 930 | if (argp[idx].type != ARGT_STR) { |
| 931 | msg = "string expected"; |
| 932 | goto error; |
| 933 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 934 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 935 | if (sname) { |
| 936 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 937 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 938 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 939 | if (!px) { |
| 940 | msg = "backend doesn't exist"; |
| 941 | goto error; |
| 942 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 943 | } |
| 944 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 945 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 946 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 947 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 948 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 949 | if (!argp[idx].data.srv) { |
| 950 | msg = "server doesn't exist"; |
| 951 | goto error; |
| 952 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 953 | argp[idx].type = ARGT_SRV; |
| 954 | break; |
| 955 | |
| 956 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 957 | if (argp[idx].type != ARGT_STR) { |
| 958 | msg = "string expected"; |
| 959 | goto error; |
| 960 | } |
| 961 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 962 | msg = "invalid IPv4 address"; |
| 963 | goto error; |
| 964 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 965 | argp[idx].type = ARGT_IPV4; |
| 966 | break; |
| 967 | |
| 968 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 969 | if (argp[idx].type == ARGT_SINT) |
| 970 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 971 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 972 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 973 | msg = "invalid IPv4 mask"; |
| 974 | goto error; |
| 975 | } |
| 976 | } |
| 977 | else { |
| 978 | msg = "integer or string expected"; |
| 979 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 980 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 981 | argp[idx].type = ARGT_MSK4; |
| 982 | break; |
| 983 | |
| 984 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 985 | if (argp[idx].type != ARGT_STR) { |
| 986 | msg = "string expected"; |
| 987 | goto error; |
| 988 | } |
| 989 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 990 | msg = "invalid IPv6 address"; |
| 991 | goto error; |
| 992 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 993 | argp[idx].type = ARGT_IPV6; |
| 994 | break; |
| 995 | |
| 996 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 997 | if (argp[idx].type == ARGT_SINT) |
| 998 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 999 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1000 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 1001 | msg = "invalid IPv6 mask"; |
| 1002 | goto error; |
| 1003 | } |
| 1004 | } |
| 1005 | else { |
| 1006 | msg = "integer or string expected"; |
| 1007 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1008 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 1009 | argp[idx].type = ARGT_MSK6; |
| 1010 | break; |
| 1011 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 1012 | case ARGT_REG: |
| 1013 | if (argp[idx].type != ARGT_STR) { |
| 1014 | msg = "string expected"; |
| 1015 | goto error; |
| 1016 | } |
| 1017 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 1018 | if (!reg) { |
| 1019 | msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'", |
| 1020 | argp[idx].data.str.area, err); |
| 1021 | free(err); |
| 1022 | goto error; |
| 1023 | } |
| 1024 | argp[idx].type = ARGT_REG; |
| 1025 | argp[idx].data.reg = reg; |
| 1026 | break; |
| 1027 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1028 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1029 | if (argp[idx].type != ARGT_STR) { |
| 1030 | msg = "string expected"; |
| 1031 | goto error; |
| 1032 | } |
| 1033 | if (p->uri_auth && p->uri_auth->userlist && |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1034 | strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0) |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1035 | ul = p->uri_auth->userlist; |
| 1036 | else |
| 1037 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 1038 | |
| 1039 | if (!ul) { |
| 1040 | msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area); |
| 1041 | goto error; |
| 1042 | } |
| 1043 | argp[idx].type = ARGT_USR; |
| 1044 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1045 | break; |
| 1046 | |
| 1047 | case ARGT_STR: |
| 1048 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 1049 | msg = "unable to duplicate string arg"; |
| 1050 | goto error; |
| 1051 | } |
| 1052 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1053 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1054 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1055 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1056 | msg = "type not yet supported"; |
| 1057 | goto error; |
| 1058 | break; |
| 1059 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | /* Check for type of argument. */ |
| 1063 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1064 | msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 1065 | arg_type_names[(mask & ARGT_MASK)], |
| 1066 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 1067 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | /* Next argument. */ |
| 1071 | mask >>= ARGT_BITS; |
| 1072 | idx++; |
| 1073 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1074 | return 0; |
| 1075 | |
| 1076 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 1077 | free_args(argp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1078 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 1079 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1080 | } |
| 1081 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1082 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1083 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1084 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1085 | * |
| 1086 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1087 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 1088 | */ |
| 1089 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 1090 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1091 | struct hlua **hlua = lua_getextraspace(L); |
| 1092 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1093 | } |
| 1094 | static inline void hlua_sethlua(struct hlua *hlua) |
| 1095 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1096 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 1097 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1098 | } |
| 1099 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1100 | /* This function is used to send logs. It try to send on screen (stderr) |
| 1101 | * and on the default syslog server. |
| 1102 | */ |
| 1103 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 1104 | { |
| 1105 | struct tm tm; |
| 1106 | char *p; |
| 1107 | |
| 1108 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1109 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1110 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1111 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 1112 | /* Break the message if exceed the buffer size. */ |
| 1113 | *(p-4) = ' '; |
| 1114 | *(p-3) = '.'; |
| 1115 | *(p-2) = '.'; |
| 1116 | *(p-1) = '.'; |
| 1117 | break; |
| 1118 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1119 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1120 | *p = *msg; |
| 1121 | else |
| 1122 | *p = '.'; |
| 1123 | } |
| 1124 | *p = '\0'; |
| 1125 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1126 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1127 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 1128 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 1129 | return; |
| 1130 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 1131 | get_localtime(date.tv_sec, &tm); |
| 1132 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1133 | 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] | 1134 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1135 | fflush(stderr); |
| 1136 | } |
| 1137 | } |
| 1138 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1139 | /* This function just ensure that the yield will be always |
| 1140 | * returned with a timeout and permit to set some flags |
| 1141 | */ |
| 1142 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1143 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1144 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1145 | struct hlua *hlua; |
| 1146 | |
| 1147 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1148 | hlua = hlua_gethlua(L); |
| 1149 | if (!hlua) { |
| 1150 | return; |
| 1151 | } |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1152 | |
| 1153 | /* Set the wake timeout. If timeout is required, we set |
| 1154 | * the expiration time. |
| 1155 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1156 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1157 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1158 | hlua->flags |= flags; |
| 1159 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1160 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1161 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1162 | } |
| 1163 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1164 | /* This function initialises the Lua environment stored in the stream. |
| 1165 | * 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] | 1166 | * 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] | 1167 | * |
| 1168 | * This function is particular. it initialises a new Lua thread. If the |
| 1169 | * initialisation fails (example: out of memory error), the lua function |
| 1170 | * throws an error (longjmp). |
| 1171 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1172 | * 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] | 1173 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1174 | * threads appear, the safe environment set a lock to ensure only one |
| 1175 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1176 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1177 | * |
| 1178 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1179 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1180 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1181 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1182 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1183 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1184 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1185 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1186 | 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] | 1187 | { |
| 1188 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1189 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1190 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1191 | lua->wake_time = TICK_ETERNITY; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1192 | lua->state_id = state_id; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1193 | LIST_INIT(&lua->com); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1194 | if (!already_safe) { |
| 1195 | if (!SET_SAFE_LJMP_PARENT(lua)) { |
| 1196 | lua->Tref = LUA_REFNIL; |
| 1197 | return 0; |
| 1198 | } |
| 1199 | } |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1200 | lua->T = lua_newthread(hlua_states[state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1201 | if (!lua->T) { |
| 1202 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1203 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1204 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1205 | return 0; |
| 1206 | } |
| 1207 | hlua_sethlua(lua); |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1208 | lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1209 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1210 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1211 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1212 | return 1; |
| 1213 | } |
| 1214 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1215 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1216 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 1217 | * is not freed. |
| 1218 | */ |
| 1219 | void hlua_ctx_destroy(struct hlua *lua) |
| 1220 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1221 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1222 | return; |
| 1223 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1224 | if (!lua->T) |
| 1225 | goto end; |
| 1226 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1227 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1228 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1229 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1230 | if (!SET_SAFE_LJMP(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1231 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1232 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1233 | RESET_SAFE_LJMP(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1234 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1235 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1236 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1237 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1238 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1239 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1240 | * without error, we run the GC on the thread pointer. Its freed all |
| 1241 | * the unused memory. |
| 1242 | * If the thread is finnish with an error or is currently yielded, |
| 1243 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1244 | * so e run the GC on the main thread. |
| 1245 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1246 | * the garbage collection. |
| 1247 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1248 | if (lua->gc_count) { |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1249 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1250 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1251 | lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1252 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1253 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1254 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1255 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1256 | |
| 1257 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1258 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | /* This function is used to restore the Lua context when a coroutine |
| 1262 | * fails. This function copy the common memory between old coroutine |
| 1263 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1264 | * replaced by the new coroutine. |
| 1265 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1266 | * as string error message and it is copied in the new stack. |
| 1267 | */ |
| 1268 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1269 | { |
| 1270 | lua_State *T; |
| 1271 | int new_ref; |
| 1272 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1273 | /* New Lua coroutine. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1274 | T = lua_newthread(hlua_states[lua->state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1275 | if (!T) |
| 1276 | return 0; |
| 1277 | |
| 1278 | /* Copy last error message. */ |
| 1279 | if (keep_msg) |
| 1280 | lua_xmove(lua->T, T, 1); |
| 1281 | |
| 1282 | /* Copy data between the coroutines. */ |
| 1283 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1284 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1285 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1286 | |
| 1287 | /* Destroy old data. */ |
| 1288 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1289 | |
| 1290 | /* The thread is garbage collected by Lua. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1291 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1292 | |
| 1293 | /* Fill the struct with the new coroutine values. */ |
| 1294 | lua->Mref = new_ref; |
| 1295 | lua->T = T; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1296 | lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1297 | |
| 1298 | /* Set context. */ |
| 1299 | hlua_sethlua(lua); |
| 1300 | |
| 1301 | return 1; |
| 1302 | } |
| 1303 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1304 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1305 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1306 | struct hlua *hlua; |
| 1307 | |
| 1308 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1309 | hlua = hlua_gethlua(L); |
| 1310 | if (!hlua) |
| 1311 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1312 | |
| 1313 | /* Lua cannot yield when its returning from a function, |
| 1314 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1315 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1316 | */ |
| 1317 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1318 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1319 | return; |
| 1320 | } |
| 1321 | |
| 1322 | /* restore the interrupt condition. */ |
| 1323 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1324 | |
| 1325 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1326 | * If the state is not yieldable, trying yield causes an error. |
| 1327 | */ |
| 1328 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1329 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1330 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1331 | /* If we cannot yield, update the clock and check the timeout. */ |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 1332 | clock_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1333 | hlua->run_time += now_ms - hlua->start_time; |
| 1334 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1335 | lua_pushfstring(L, "execution timeout"); |
| 1336 | WILL_LJMP(lua_error(L)); |
| 1337 | } |
| 1338 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1339 | /* Update the start time. */ |
| 1340 | hlua->start_time = now_ms; |
| 1341 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1342 | /* Try to interrupt the process at the end of the current |
| 1343 | * unyieldable function. |
| 1344 | */ |
| 1345 | 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] | 1346 | } |
| 1347 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1348 | /* This function start or resumes the Lua stack execution. If the flag |
| 1349 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1350 | * The function return an error. |
| 1351 | * |
| 1352 | * The function can returns 4 values: |
| 1353 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1354 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1355 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1356 | * - 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] | 1357 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1358 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1359 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1360 | * 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] | 1361 | * LUA code. |
| 1362 | */ |
| 1363 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1364 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1365 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1366 | int nres; |
| 1367 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1368 | int ret; |
| 1369 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1370 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1371 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1372 | /* Initialise run time counter. */ |
| 1373 | if (!HLUA_IS_RUNNING(lua)) |
| 1374 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1375 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1376 | /* Lock the whole Lua execution. This lock must be before the |
| 1377 | * label "resume_execution". |
| 1378 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1379 | if (lua->state_id == 0) |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 1380 | lua_take_global_lock(); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1381 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1382 | resume_execution: |
| 1383 | |
| 1384 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1385 | * instructions. it is used for preventing infinite loops. |
| 1386 | */ |
| 1387 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1388 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1389 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1390 | HLUA_SET_RUN(lua); |
| 1391 | HLUA_CLR_CTRLYIELD(lua); |
| 1392 | HLUA_CLR_WAKERESWR(lua); |
| 1393 | HLUA_CLR_WAKEREQWR(lua); |
Christopher Faulet | 1f43a34 | 2021-08-04 17:58:21 +0200 | [diff] [blame] | 1394 | HLUA_CLR_NOYIELD(lua); |
| 1395 | if (!yield_allowed) |
| 1396 | HLUA_SET_NOYIELD(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1397 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1398 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1399 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1400 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1401 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1402 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1403 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1404 | 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] | 1405 | #else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1406 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1407 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1408 | switch (ret) { |
| 1409 | |
| 1410 | case LUA_OK: |
| 1411 | ret = HLUA_E_OK; |
| 1412 | break; |
| 1413 | |
| 1414 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1415 | /* Check if the execution timeout is expired. It it is the case, we |
| 1416 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1417 | */ |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 1418 | clock_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1419 | lua->run_time += now_ms - lua->start_time; |
| 1420 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1421 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1422 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1423 | break; |
| 1424 | } |
| 1425 | /* Process the forced yield. if the general yield is not allowed or |
| 1426 | * if no task were associated this the current Lua execution |
| 1427 | * coroutine, we resume the execution. Else we want to return in the |
| 1428 | * scheduler and we want to be waked up again, to continue the |
| 1429 | * current Lua execution. So we schedule our own task. |
| 1430 | */ |
| 1431 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1432 | if (!yield_allowed || !lua->task) |
| 1433 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1434 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1435 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1436 | if (!yield_allowed) { |
| 1437 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1438 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1439 | break; |
| 1440 | } |
| 1441 | ret = HLUA_E_AGAIN; |
| 1442 | break; |
| 1443 | |
| 1444 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1445 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1446 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1447 | * because the errors ares the only one mean to return immediately |
| 1448 | * from and lua execution. |
| 1449 | */ |
| 1450 | if (lua->flags & HLUA_EXIT) { |
| 1451 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1452 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1453 | break; |
| 1454 | } |
| 1455 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1456 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1457 | if (!lua_checkstack(lua->T, 1)) { |
| 1458 | ret = HLUA_E_ERR; |
| 1459 | break; |
| 1460 | } |
| 1461 | msg = lua_tostring(lua->T, -1); |
| 1462 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1463 | lua_pop(lua->T, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 1464 | trace = hlua_traceback(lua->T, ", "); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1465 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1466 | 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] | 1467 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1468 | 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] | 1469 | ret = HLUA_E_ERRMSG; |
| 1470 | break; |
| 1471 | |
| 1472 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1473 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1474 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1475 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1476 | break; |
| 1477 | |
| 1478 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1479 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1480 | if (!lua_checkstack(lua->T, 1)) { |
| 1481 | ret = HLUA_E_ERR; |
| 1482 | break; |
| 1483 | } |
| 1484 | msg = lua_tostring(lua->T, -1); |
| 1485 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1486 | lua_pop(lua->T, 1); |
| 1487 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1488 | 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] | 1489 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1490 | 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] | 1491 | ret = HLUA_E_ERRMSG; |
| 1492 | break; |
| 1493 | |
| 1494 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1495 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1496 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1497 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1498 | break; |
| 1499 | } |
| 1500 | |
| 1501 | switch (ret) { |
| 1502 | case HLUA_E_AGAIN: |
| 1503 | break; |
| 1504 | |
| 1505 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1506 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1507 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1508 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1509 | break; |
| 1510 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1511 | case HLUA_E_ETMOUT: |
| 1512 | case HLUA_E_NOMEM: |
| 1513 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1514 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1515 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1516 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1517 | hlua_ctx_renew(lua, 0); |
| 1518 | break; |
| 1519 | |
| 1520 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1521 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1522 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1523 | break; |
| 1524 | } |
| 1525 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1526 | /* This is the main exit point, remove the Lua lock. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1527 | if (lua->state_id == 0) |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 1528 | lua_drop_global_lock(); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1529 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1530 | return ret; |
| 1531 | } |
| 1532 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1533 | /* This function exit the current code. */ |
| 1534 | __LJMP static int hlua_done(lua_State *L) |
| 1535 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1536 | struct hlua *hlua; |
| 1537 | |
| 1538 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1539 | hlua = hlua_gethlua(L); |
| 1540 | if (!hlua) |
| 1541 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1542 | |
| 1543 | hlua->flags |= HLUA_EXIT; |
| 1544 | WILL_LJMP(lua_error(L)); |
| 1545 | |
| 1546 | return 0; |
| 1547 | } |
| 1548 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1549 | /* This function is an LUA binding. It provides a function |
| 1550 | * for deleting ACL from a referenced ACL file. |
| 1551 | */ |
| 1552 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1553 | { |
| 1554 | const char *name; |
| 1555 | const char *key; |
| 1556 | struct pat_ref *ref; |
| 1557 | |
| 1558 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1559 | |
| 1560 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1561 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1562 | |
| 1563 | ref = pat_ref_lookup(name); |
| 1564 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1565 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1566 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1567 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1568 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1569 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1570 | return 0; |
| 1571 | } |
| 1572 | |
| 1573 | /* This function is an LUA binding. It provides a function |
| 1574 | * for deleting map entry from a referenced map file. |
| 1575 | */ |
| 1576 | static int hlua_del_map(lua_State *L) |
| 1577 | { |
| 1578 | const char *name; |
| 1579 | const char *key; |
| 1580 | struct pat_ref *ref; |
| 1581 | |
| 1582 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1583 | |
| 1584 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1585 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1586 | |
| 1587 | ref = pat_ref_lookup(name); |
| 1588 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1589 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1590 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1591 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1592 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1593 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1594 | return 0; |
| 1595 | } |
| 1596 | |
| 1597 | /* This function is an LUA binding. It provides a function |
| 1598 | * for adding ACL pattern from a referenced ACL file. |
| 1599 | */ |
| 1600 | static int hlua_add_acl(lua_State *L) |
| 1601 | { |
| 1602 | const char *name; |
| 1603 | const char *key; |
| 1604 | struct pat_ref *ref; |
| 1605 | |
| 1606 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1607 | |
| 1608 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1609 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1610 | |
| 1611 | ref = pat_ref_lookup(name); |
| 1612 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1613 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1614 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1615 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1616 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1617 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1618 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1619 | return 0; |
| 1620 | } |
| 1621 | |
| 1622 | /* This function is an LUA binding. It provides a function |
| 1623 | * for setting map pattern and sample from a referenced map |
| 1624 | * file. |
| 1625 | */ |
| 1626 | static int hlua_set_map(lua_State *L) |
| 1627 | { |
| 1628 | const char *name; |
| 1629 | const char *key; |
| 1630 | const char *value; |
| 1631 | struct pat_ref *ref; |
| 1632 | |
| 1633 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1634 | |
| 1635 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1636 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1637 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1638 | |
| 1639 | ref = pat_ref_lookup(name); |
| 1640 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1641 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1642 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1643 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1644 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1645 | pat_ref_set(ref, key, value, NULL); |
| 1646 | else |
| 1647 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1648 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1649 | return 0; |
| 1650 | } |
| 1651 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1652 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1653 | * 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] | 1654 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1655 | * the name of the object (_G[<name>] = <metable> ). |
| 1656 | * |
| 1657 | * A metable is a table that modify the standard behavior of a standard |
| 1658 | * access to the associated data. The entries of this new metatable are |
| 1659 | * defined as is: |
| 1660 | * |
| 1661 | * http://lua-users.org/wiki/MetatableEvents |
| 1662 | * |
| 1663 | * __index |
| 1664 | * |
| 1665 | * we access an absent field in a table, the result is nil. This is |
| 1666 | * true, but it is not the whole truth. Actually, such access triggers |
| 1667 | * the interpreter to look for an __index metamethod: If there is no |
| 1668 | * such method, as usually happens, then the access results in nil; |
| 1669 | * otherwise, the metamethod will provide the result. |
| 1670 | * |
| 1671 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1672 | * the key does not appear in the table, but the metatable has an __index |
| 1673 | * property: |
| 1674 | * |
| 1675 | * - if the value is a function, the function is called, passing in the |
| 1676 | * table and the key; the return value of that function is returned as |
| 1677 | * the result. |
| 1678 | * |
| 1679 | * - if the value is another table, the value of the key in that table is |
| 1680 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1681 | * table's metatable has an __index property, then it continues on up) |
| 1682 | * |
| 1683 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1684 | * |
| 1685 | * http://www.lua.org/pil/13.4.1.html |
| 1686 | * |
| 1687 | * __newindex |
| 1688 | * |
| 1689 | * Like __index, but control property assignment. |
| 1690 | * |
| 1691 | * __mode - Control weak references. A string value with one or both |
| 1692 | * of the characters 'k' and 'v' which specifies that the the |
| 1693 | * keys and/or values in the table are weak references. |
| 1694 | * |
| 1695 | * __call - Treat a table like a function. When a table is followed by |
| 1696 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1697 | * a __call key pointing to a function, that function is invoked |
| 1698 | * (passing any specified arguments) and the return value is |
| 1699 | * returned. |
| 1700 | * |
| 1701 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1702 | * called, if the metatable for myTable has a __metatable |
| 1703 | * key, the value of that key is returned instead of the |
| 1704 | * actual metatable. |
| 1705 | * |
| 1706 | * __tostring - Control string representation. When the builtin |
| 1707 | * "tostring( myTable )" function is called, if the metatable |
| 1708 | * for myTable has a __tostring property set to a function, |
| 1709 | * that function is invoked (passing myTable to it) and the |
| 1710 | * return value is used as the string representation. |
| 1711 | * |
| 1712 | * __len - Control table length. When the table length is requested using |
| 1713 | * the length operator ( '#' ), if the metatable for myTable has |
| 1714 | * a __len key pointing to a function, that function is invoked |
| 1715 | * (passing myTable to it) and the return value used as the value |
| 1716 | * of "#myTable". |
| 1717 | * |
| 1718 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1719 | * collected, if the metatable has a __gc field pointing to a |
| 1720 | * function, that function is first invoked, passing the userdata |
| 1721 | * to it. The __gc metamethod is not called for tables. |
| 1722 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1723 | * |
| 1724 | * Special metamethods for redefining standard operators: |
| 1725 | * http://www.lua.org/pil/13.1.html |
| 1726 | * |
| 1727 | * __add "+" |
| 1728 | * __sub "-" |
| 1729 | * __mul "*" |
| 1730 | * __div "/" |
| 1731 | * __unm "!" |
| 1732 | * __pow "^" |
| 1733 | * __concat ".." |
| 1734 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1735 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1736 | * http://www.lua.org/pil/13.2.html |
| 1737 | * |
| 1738 | * __eq "==" |
| 1739 | * __lt "<" |
| 1740 | * __le "<=" |
| 1741 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1742 | |
| 1743 | /* |
| 1744 | * |
| 1745 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1746 | * Class Map |
| 1747 | * |
| 1748 | * |
| 1749 | */ |
| 1750 | |
| 1751 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1752 | * a class session, otherwise it throws an error. |
| 1753 | */ |
| 1754 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1755 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1756 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1757 | } |
| 1758 | |
| 1759 | /* This function is the map constructor. It don't need |
| 1760 | * the class Map object. It creates and return a new Map |
| 1761 | * object. It must be called only during "body" or "init" |
| 1762 | * context because it process some filesystem accesses. |
| 1763 | */ |
| 1764 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1765 | { |
| 1766 | const char *fn; |
| 1767 | int match = PAT_MATCH_STR; |
| 1768 | struct sample_conv conv; |
| 1769 | const char *file = ""; |
| 1770 | int line = 0; |
| 1771 | lua_Debug ar; |
| 1772 | char *err = NULL; |
| 1773 | struct arg args[2]; |
| 1774 | |
| 1775 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1776 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1777 | |
| 1778 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1779 | |
| 1780 | if (lua_gettop(L) >= 2) { |
| 1781 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1782 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1783 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1784 | } |
| 1785 | |
| 1786 | /* Get Lua filename and line number. */ |
| 1787 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1788 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1789 | if (ar.currentline > 0) { /* is there info? */ |
| 1790 | file = ar.short_src; |
| 1791 | line = ar.currentline; |
| 1792 | } |
| 1793 | } |
| 1794 | |
| 1795 | /* fill fake sample_conv struct. */ |
| 1796 | conv.kw = ""; /* unused. */ |
| 1797 | conv.process = NULL; /* unused. */ |
| 1798 | conv.arg_mask = 0; /* unused. */ |
| 1799 | conv.val_args = NULL; /* unused. */ |
| 1800 | conv.out_type = SMP_T_STR; |
| 1801 | conv.private = (void *)(long)match; |
| 1802 | switch (match) { |
| 1803 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1804 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1805 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1806 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1807 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1808 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1809 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1810 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1811 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1812 | default: |
| 1813 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1814 | } |
| 1815 | |
| 1816 | /* fill fake args. */ |
| 1817 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1818 | args[0].data.str.area = strdup(fn); |
| 1819 | args[0].data.str.data = strlen(fn); |
| 1820 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1821 | args[1].type = ARGT_STOP; |
| 1822 | |
| 1823 | /* load the map. */ |
| 1824 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1825 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1826 | * free the err variable. |
| 1827 | */ |
| 1828 | luaL_where(L, 1); |
| 1829 | lua_pushfstring(L, "'new': %s.", err); |
| 1830 | lua_concat(L, 2); |
| 1831 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1832 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1833 | WILL_LJMP(lua_error(L)); |
| 1834 | } |
| 1835 | |
| 1836 | /* create the lua object. */ |
| 1837 | lua_newtable(L); |
| 1838 | lua_pushlightuserdata(L, args[0].data.map); |
| 1839 | lua_rawseti(L, -2, 0); |
| 1840 | |
| 1841 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1842 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1843 | lua_setmetatable(L, -2); |
| 1844 | |
| 1845 | |
| 1846 | return 1; |
| 1847 | } |
| 1848 | |
| 1849 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1850 | { |
| 1851 | struct map_descriptor *desc; |
| 1852 | struct pattern *pat; |
| 1853 | struct sample smp; |
| 1854 | |
| 1855 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1856 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1857 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1858 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1859 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1860 | } |
| 1861 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1862 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1863 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1864 | 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] | 1865 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1866 | } |
| 1867 | |
| 1868 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1869 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1870 | if (str) |
| 1871 | lua_pushstring(L, ""); |
| 1872 | else |
| 1873 | lua_pushnil(L); |
| 1874 | return 1; |
| 1875 | } |
| 1876 | |
| 1877 | /* 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] | 1878 | 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] | 1879 | return 1; |
| 1880 | } |
| 1881 | |
| 1882 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1883 | { |
| 1884 | return _hlua_map_lookup(L, 0); |
| 1885 | } |
| 1886 | |
| 1887 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1888 | { |
| 1889 | return _hlua_map_lookup(L, 1); |
| 1890 | } |
| 1891 | |
| 1892 | /* |
| 1893 | * |
| 1894 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1895 | * Class Socket |
| 1896 | * |
| 1897 | * |
| 1898 | */ |
| 1899 | |
| 1900 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1901 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1902 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1903 | } |
| 1904 | |
| 1905 | /* 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] | 1906 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1907 | * received. |
| 1908 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1909 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1910 | { |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1911 | struct conn_stream *cs = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1912 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1913 | if (appctx->ctx.hlua_cosocket.die) { |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame] | 1914 | cs_shutw(cs); |
| 1915 | cs_shutr(cs); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1916 | cs_ic(cs)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1917 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1918 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1919 | stream_shutdown(__cs_strm(cs), SF_ERR_KILLED); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1920 | } |
| 1921 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1922 | /* If we can't write, wakeup the pending write signals. */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1923 | if (channel_output_closed(cs_ic(cs))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1924 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1925 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1926 | /* If we can't read, wakeup the pending read signals. */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1927 | if (channel_input_closed(cs_oc(cs))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1928 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1929 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1930 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1931 | * to be notified whenever the connection completes. |
| 1932 | */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1933 | if (cs_opposite(cs)->state < CS_ST_EST) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 1934 | cs_cant_get(cs); |
| 1935 | cs_rx_conn_blk(cs); |
| 1936 | cs_rx_endp_more(cs); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1937 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1938 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1939 | |
| 1940 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1941 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1942 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1943 | /* 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] | 1944 | if (channel_may_recv(cs_ic(cs))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1945 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1946 | |
| 1947 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1948 | if (!channel_is_empty(cs_oc(cs))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1949 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1950 | |
| 1951 | /* Some data were injected in the buffer, notify the stream |
| 1952 | * interface. |
| 1953 | */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1954 | if (!channel_is_empty(cs_ic(cs))) |
Christopher Faulet | 13045f0 | 2022-04-01 14:23:38 +0200 | [diff] [blame] | 1955 | cs_update(cs); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1956 | |
| 1957 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1958 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1959 | */ |
| 1960 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 1961 | cs_rx_endp_more(cs); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1962 | } |
| 1963 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1964 | /* This function is called when the "struct stream" is destroyed. |
| 1965 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1966 | * Wake all the pending signals. |
| 1967 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1968 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1969 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1970 | struct xref *peer; |
| 1971 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1972 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1973 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1974 | if (peer) |
| 1975 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1976 | |
| 1977 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1978 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1979 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1980 | } |
| 1981 | |
| 1982 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1983 | * uses this object. If the stream does not exists, just quit. |
| 1984 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1985 | * pending signal can rest in the read and write lists. destroy |
| 1986 | * it. |
| 1987 | */ |
| 1988 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1989 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1990 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1991 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1992 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1993 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1994 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1995 | |
| 1996 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1997 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1998 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1999 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2000 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2001 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2002 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2003 | appctx->ctx.hlua_cosocket.die = 1; |
| 2004 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2005 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2006 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2007 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2008 | return 0; |
| 2009 | } |
| 2010 | |
| 2011 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2012 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2013 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2014 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2015 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2016 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2017 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2018 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2019 | struct hlua *hlua; |
| 2020 | |
| 2021 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2022 | hlua = hlua_gethlua(L); |
| 2023 | if (!hlua) |
| 2024 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2025 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2026 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2027 | |
| 2028 | /* Check if we run on the same thread than the xreator thread. |
| 2029 | * We cannot access to the socket if the thread is different. |
| 2030 | */ |
| 2031 | if (socket->tid != tid) |
| 2032 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2033 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2034 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2035 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2036 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2037 | |
| 2038 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2039 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2040 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2041 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2042 | appctx->ctx.hlua_cosocket.die = 1; |
| 2043 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2044 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2045 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2046 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2047 | return 0; |
| 2048 | } |
| 2049 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2050 | /* The close function calls close_helper. |
| 2051 | */ |
| 2052 | __LJMP static int hlua_socket_close(lua_State *L) |
| 2053 | { |
| 2054 | MAY_LJMP(check_args(L, 1, "close")); |
| 2055 | return hlua_socket_close_helper(L); |
| 2056 | } |
| 2057 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2058 | /* This Lua function assumes that the stack contain three parameters. |
| 2059 | * 1 - USERDATA containing a struct socket |
| 2060 | * 2 - INTEGER with values of the macro defined below |
| 2061 | * If the integer is -1, we must read at most one line. |
| 2062 | * If the integer is -2, we ust read all the data until the |
| 2063 | * end of the stream. |
| 2064 | * If the integer is positive value, we must read a number of |
| 2065 | * bytes corresponding to this value. |
| 2066 | */ |
| 2067 | #define HLSR_READ_LINE (-1) |
| 2068 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2069 | __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] | 2070 | { |
| 2071 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2072 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2073 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2074 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2075 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2076 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2077 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2078 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2079 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2080 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2081 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 2082 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2083 | struct stream *s; |
| 2084 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2085 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2086 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2087 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2088 | hlua = hlua_gethlua(L); |
| 2089 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2090 | /* Check if this lua stack is schedulable. */ |
| 2091 | if (!hlua || !hlua->task) |
| 2092 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 2093 | "'frontend', 'backend' or 'task'")); |
| 2094 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2095 | /* Check if we run on the same thread than the xreator thread. |
| 2096 | * We cannot access to the socket if the thread is different. |
| 2097 | */ |
| 2098 | if (socket->tid != tid) |
| 2099 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2100 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2101 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2102 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2103 | if (!peer) |
| 2104 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2105 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2106 | s = __cs_strm(appctx->owner); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2107 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2108 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2109 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2110 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2111 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2112 | if (nblk < 0) /* Connection close. */ |
| 2113 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2114 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2115 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2116 | |
| 2117 | /* remove final \r\n. */ |
| 2118 | if (nblk == 1) { |
| 2119 | if (blk1[len1-1] == '\n') { |
| 2120 | len1--; |
| 2121 | skip_at_end++; |
| 2122 | if (blk1[len1-1] == '\r') { |
| 2123 | len1--; |
| 2124 | skip_at_end++; |
| 2125 | } |
| 2126 | } |
| 2127 | } |
| 2128 | else { |
| 2129 | if (blk2[len2-1] == '\n') { |
| 2130 | len2--; |
| 2131 | skip_at_end++; |
| 2132 | if (blk2[len2-1] == '\r') { |
| 2133 | len2--; |
| 2134 | skip_at_end++; |
| 2135 | } |
| 2136 | } |
| 2137 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2138 | } |
| 2139 | |
| 2140 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2141 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2142 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2143 | if (nblk < 0) /* Connection close. */ |
| 2144 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2145 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2146 | goto connection_empty; |
| 2147 | } |
| 2148 | |
| 2149 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2150 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2151 | nblk = co_getblk_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; |
| 2156 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2157 | missing_bytes = wanted - socket->b.n; |
| 2158 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2159 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2160 | len1 = missing_bytes; |
| 2161 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2162 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2163 | } |
| 2164 | |
| 2165 | len = len1; |
| 2166 | |
| 2167 | luaL_addlstring(&socket->b, blk1, len1); |
| 2168 | if (nblk == 2) { |
| 2169 | len += len2; |
| 2170 | luaL_addlstring(&socket->b, blk2, len2); |
| 2171 | } |
| 2172 | |
| 2173 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2174 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2175 | |
| 2176 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2177 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2178 | |
| 2179 | /* If the pattern reclaim to read all the data |
| 2180 | * in the connection, got out. |
| 2181 | */ |
| 2182 | if (wanted == HLSR_READ_ALL) |
| 2183 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2184 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2185 | goto connection_empty; |
| 2186 | |
| 2187 | /* Return result. */ |
| 2188 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2189 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2190 | return 1; |
| 2191 | |
| 2192 | connection_closed: |
| 2193 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2194 | xref_unlock(&socket->xref, peer); |
| 2195 | |
| 2196 | no_peer: |
| 2197 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2198 | /* If the buffer containds data. */ |
| 2199 | if (socket->b.n > 0) { |
| 2200 | luaL_pushresult(&socket->b); |
| 2201 | return 1; |
| 2202 | } |
| 2203 | lua_pushnil(L); |
| 2204 | lua_pushstring(L, "connection closed."); |
| 2205 | return 2; |
| 2206 | |
| 2207 | connection_empty: |
| 2208 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2209 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 2210 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2211 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2212 | } |
| 2213 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2214 | 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] | 2215 | return 0; |
| 2216 | } |
| 2217 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2218 | /* This Lua function gets two parameters. The first one can be string |
| 2219 | * or a number. If the string is "*l", the user requires one line. If |
| 2220 | * 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] | 2221 | * If the value is a number, the user require a number of bytes equal |
| 2222 | * to the value. The default value is "*l" (a line). |
| 2223 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2224 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2225 | * integer takes this values: |
| 2226 | * -1 : read a line |
| 2227 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2228 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2229 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2230 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2231 | * concatenated with the read data. |
| 2232 | */ |
| 2233 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2234 | { |
| 2235 | int wanted = HLSR_READ_LINE; |
| 2236 | const char *pattern; |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2237 | int lastarg, type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2238 | char *error; |
| 2239 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2240 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2241 | |
| 2242 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2243 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2244 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2245 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2246 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2247 | /* Check if we run on the same thread than the xreator thread. |
| 2248 | * We cannot access to the socket if the thread is different. |
| 2249 | */ |
| 2250 | if (socket->tid != tid) |
| 2251 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2252 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2253 | /* check for pattern. */ |
| 2254 | if (lua_gettop(L) >= 2) { |
| 2255 | type = lua_type(L, 2); |
| 2256 | if (type == LUA_TSTRING) { |
| 2257 | pattern = lua_tostring(L, 2); |
| 2258 | if (strcmp(pattern, "*a") == 0) |
| 2259 | wanted = HLSR_READ_ALL; |
| 2260 | else if (strcmp(pattern, "*l") == 0) |
| 2261 | wanted = HLSR_READ_LINE; |
| 2262 | else { |
| 2263 | wanted = strtoll(pattern, &error, 10); |
| 2264 | if (*error != '\0') |
| 2265 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2266 | } |
| 2267 | } |
| 2268 | else if (type == LUA_TNUMBER) { |
| 2269 | wanted = lua_tointeger(L, 2); |
| 2270 | if (wanted < 0) |
| 2271 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2272 | } |
| 2273 | } |
| 2274 | |
| 2275 | /* Set pattern. */ |
| 2276 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2277 | |
| 2278 | /* Check if we would replace the top by itself. */ |
| 2279 | if (lua_gettop(L) != 2) |
| 2280 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2281 | |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2282 | /* Save index of the top of the stack because since buffers are used, it |
| 2283 | * may change |
| 2284 | */ |
| 2285 | lastarg = lua_gettop(L); |
| 2286 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2287 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2288 | luaL_buffinit(L, &socket->b); |
| 2289 | |
| 2290 | /* Check prefix. */ |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2291 | if (lastarg >= 3) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2292 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2293 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2294 | pattern = lua_tolstring(L, 3, &len); |
| 2295 | luaL_addlstring(&socket->b, pattern, len); |
| 2296 | } |
| 2297 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2298 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2299 | } |
| 2300 | |
| 2301 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2302 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2303 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2304 | 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] | 2305 | { |
| 2306 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2307 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2308 | struct appctx *appctx; |
| 2309 | size_t buf_len; |
| 2310 | const char *buf; |
| 2311 | int len; |
| 2312 | int send_len; |
| 2313 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2314 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2315 | struct stream *s; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2316 | struct conn_stream *cs; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2317 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2318 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2319 | hlua = hlua_gethlua(L); |
| 2320 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2321 | /* Check if this lua stack is schedulable. */ |
| 2322 | if (!hlua || !hlua->task) |
| 2323 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2324 | "'frontend', 'backend' or 'task'")); |
| 2325 | |
| 2326 | /* Get object */ |
| 2327 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2328 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2329 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2330 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2331 | /* Check if we run on the same thread than the xreator thread. |
| 2332 | * We cannot access to the socket if the thread is different. |
| 2333 | */ |
| 2334 | if (socket->tid != tid) |
| 2335 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2336 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2337 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2338 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2339 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2340 | lua_pushinteger(L, -1); |
| 2341 | return 1; |
| 2342 | } |
| 2343 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2344 | cs = appctx->owner; |
| 2345 | s = __cs_strm(cs); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2346 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2347 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2348 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2349 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2350 | lua_pushinteger(L, -1); |
| 2351 | return 1; |
| 2352 | } |
| 2353 | |
| 2354 | /* Update the input buffer data. */ |
| 2355 | buf += sent; |
| 2356 | send_len = buf_len - sent; |
| 2357 | |
| 2358 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2359 | if (sent >= buf_len) { |
| 2360 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2361 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2362 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2363 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2364 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2365 | * the request buffer if its not required. |
| 2366 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2367 | if (s->req.buf.size == 0) { |
Christopher Faulet | 8f45eec | 2022-04-01 17:19:36 +0200 | [diff] [blame] | 2368 | if (!cs_alloc_ibuf(cs, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2369 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2370 | } |
| 2371 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2372 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2373 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2374 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2375 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2376 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2377 | |
| 2378 | /* send data */ |
| 2379 | if (len < send_len) |
| 2380 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2381 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2382 | |
| 2383 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2384 | * the data" (-2) are not expected because the available length |
| 2385 | * is tested. |
| 2386 | * Other unknown error are also not expected. |
| 2387 | */ |
| 2388 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2389 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2390 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2391 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2392 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2393 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2394 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2395 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2396 | return 1; |
| 2397 | } |
| 2398 | |
| 2399 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2400 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2401 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2402 | s->req.rex = TICK_ETERNITY; |
| 2403 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2404 | |
| 2405 | /* Update length sent. */ |
| 2406 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2407 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2408 | |
| 2409 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2410 | if (sent + len >= buf_len) { |
| 2411 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2412 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2413 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2414 | |
| 2415 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2416 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2417 | xref_unlock(&socket->xref, peer); |
| 2418 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2419 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2420 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2421 | 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] | 2422 | return 0; |
| 2423 | } |
| 2424 | |
| 2425 | /* This function initiate the send of data. It just check the input |
| 2426 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2427 | * 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] | 2428 | * "hlua_socket_write_yield" that can yield. |
| 2429 | * |
| 2430 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2431 | * the associated object. The second is a string buffer. The third is |
| 2432 | * a facultative integer that represents where is the buffer position |
| 2433 | * of the start of the data that can send. The first byte is the |
| 2434 | * position "1". The default value is "1". The fourth argument is a |
| 2435 | * facultative integer that represents where is the buffer position |
| 2436 | * of the end of the data that can send. The default is the last byte. |
| 2437 | */ |
| 2438 | static int hlua_socket_send(struct lua_State *L) |
| 2439 | { |
| 2440 | int i; |
| 2441 | int j; |
| 2442 | const char *buf; |
| 2443 | size_t buf_len; |
| 2444 | |
| 2445 | /* Check number of arguments. */ |
| 2446 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2447 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2448 | |
| 2449 | /* Get the string. */ |
| 2450 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2451 | |
| 2452 | /* Get and check j. */ |
| 2453 | if (lua_gettop(L) == 4) { |
| 2454 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2455 | if (j < 0) |
| 2456 | j = buf_len + j + 1; |
| 2457 | if (j > buf_len) |
| 2458 | j = buf_len + 1; |
| 2459 | lua_pop(L, 1); |
| 2460 | } |
| 2461 | else |
| 2462 | j = buf_len; |
| 2463 | |
| 2464 | /* Get and check i. */ |
| 2465 | if (lua_gettop(L) == 3) { |
| 2466 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2467 | if (i < 0) |
| 2468 | i = buf_len + i + 1; |
| 2469 | if (i > buf_len) |
| 2470 | i = buf_len + 1; |
| 2471 | lua_pop(L, 1); |
| 2472 | } else |
| 2473 | i = 1; |
| 2474 | |
| 2475 | /* Check bth i and j. */ |
| 2476 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2477 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2478 | return 1; |
| 2479 | } |
| 2480 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2481 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2482 | return 1; |
| 2483 | } |
| 2484 | if (i == 0) |
| 2485 | i = 1; |
| 2486 | if (j == 0) |
| 2487 | j = 1; |
| 2488 | |
| 2489 | /* Pop the string. */ |
| 2490 | lua_pop(L, 1); |
| 2491 | |
| 2492 | /* Update the buffer length. */ |
| 2493 | buf += i - 1; |
| 2494 | buf_len = j - i + 1; |
| 2495 | lua_pushlstring(L, buf, buf_len); |
| 2496 | |
| 2497 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2498 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2499 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2500 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2501 | } |
| 2502 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2503 | #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] | 2504 | __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] | 2505 | { |
| 2506 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2507 | int ret; |
| 2508 | int len; |
| 2509 | char *p; |
| 2510 | |
| 2511 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2512 | if (ret <= 0) { |
| 2513 | lua_pushnil(L); |
| 2514 | return 1; |
| 2515 | } |
| 2516 | |
| 2517 | if (ret == AF_UNIX) { |
| 2518 | lua_pushstring(L, buffer+1); |
| 2519 | return 1; |
| 2520 | } |
| 2521 | else if (ret == AF_INET6) { |
| 2522 | buffer[0] = '['; |
| 2523 | len = strlen(buffer); |
| 2524 | buffer[len] = ']'; |
| 2525 | len++; |
| 2526 | buffer[len] = ':'; |
| 2527 | len++; |
| 2528 | p = buffer; |
| 2529 | } |
| 2530 | else if (ret == AF_INET) { |
| 2531 | p = buffer + 1; |
| 2532 | len = strlen(p); |
| 2533 | p[len] = ':'; |
| 2534 | len++; |
| 2535 | } |
| 2536 | else { |
| 2537 | lua_pushnil(L); |
| 2538 | return 1; |
| 2539 | } |
| 2540 | |
| 2541 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2542 | lua_pushnil(L); |
| 2543 | return 1; |
| 2544 | } |
| 2545 | |
| 2546 | lua_pushstring(L, p); |
| 2547 | return 1; |
| 2548 | } |
| 2549 | |
| 2550 | /* Returns information about the peer of the connection. */ |
| 2551 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2552 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2553 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2554 | struct xref *peer; |
| 2555 | struct appctx *appctx; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2556 | struct conn_stream *cs; |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2557 | const struct sockaddr_storage *dst; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2558 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2559 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2560 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2561 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2562 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2563 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2564 | /* Check if we run on the same thread than the xreator thread. |
| 2565 | * We cannot access to the socket if the thread is different. |
| 2566 | */ |
| 2567 | if (socket->tid != tid) |
| 2568 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2569 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2570 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2571 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2572 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2573 | lua_pushnil(L); |
| 2574 | return 1; |
| 2575 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2576 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2577 | cs = appctx->owner; |
Christopher Faulet | 8da67aa | 2022-03-29 17:53:09 +0200 | [diff] [blame] | 2578 | dst = cs_dst(cs_opposite(cs)); |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2579 | if (!dst) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2580 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2581 | lua_pushnil(L); |
| 2582 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2583 | } |
| 2584 | |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2585 | ret = MAY_LJMP(hlua_socket_info(L, dst)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2586 | xref_unlock(&socket->xref, peer); |
| 2587 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2588 | } |
| 2589 | |
| 2590 | /* Returns information about my connection side. */ |
| 2591 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2592 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2593 | struct hlua_socket *socket; |
| 2594 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2595 | struct appctx *appctx; |
| 2596 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2597 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2598 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2599 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2600 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2601 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2602 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2603 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2604 | /* Check if we run on the same thread than the xreator thread. |
| 2605 | * We cannot access to the socket if the thread is different. |
| 2606 | */ |
| 2607 | if (socket->tid != tid) |
| 2608 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2609 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2610 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2611 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2612 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2613 | lua_pushnil(L); |
| 2614 | return 1; |
| 2615 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2616 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2617 | s = __cs_strm(appctx->owner); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2618 | |
Christopher Faulet | 95a61e8 | 2021-12-22 14:22:03 +0100 | [diff] [blame] | 2619 | conn = cs_conn(s->csb); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2620 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2621 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2622 | lua_pushnil(L); |
| 2623 | return 1; |
| 2624 | } |
| 2625 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2626 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2627 | xref_unlock(&socket->xref, peer); |
| 2628 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2629 | } |
| 2630 | |
| 2631 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2632 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2633 | .obj_type = OBJ_TYPE_APPLET, |
| 2634 | .name = "<LUA_TCP>", |
| 2635 | .fct = hlua_socket_handler, |
| 2636 | .release = hlua_socket_release, |
| 2637 | }; |
| 2638 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2639 | __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] | 2640 | { |
| 2641 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2642 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2643 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2644 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2645 | struct stream *s; |
| 2646 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2647 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2648 | hlua = hlua_gethlua(L); |
| 2649 | if (!hlua) |
| 2650 | return 0; |
| 2651 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2652 | /* Check if we run on the same thread than the xreator thread. |
| 2653 | * We cannot access to the socket if the thread is different. |
| 2654 | */ |
| 2655 | if (socket->tid != tid) |
| 2656 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2657 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2658 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2659 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2660 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2661 | lua_pushnil(L); |
| 2662 | lua_pushstring(L, "Can't connect"); |
| 2663 | return 2; |
| 2664 | } |
| 2665 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2666 | s = __cs_strm(appctx->owner); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2667 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2668 | /* Check if we run on the same thread than the xreator thread. |
| 2669 | * We cannot access to the socket if the thread is different. |
| 2670 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2671 | if (socket->tid != tid) { |
| 2672 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2673 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2674 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2675 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2676 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2677 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2678 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2679 | lua_pushnil(L); |
| 2680 | lua_pushstring(L, "Can't connect"); |
| 2681 | return 2; |
| 2682 | } |
| 2683 | |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 2684 | appctx = __cs_appctx(s->csf); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2685 | |
| 2686 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2687 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2688 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2689 | lua_pushinteger(L, 1); |
| 2690 | return 1; |
| 2691 | } |
| 2692 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2693 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2694 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2695 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2696 | } |
| 2697 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2698 | 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] | 2699 | return 0; |
| 2700 | } |
| 2701 | |
| 2702 | /* This function fail or initite the connection. */ |
| 2703 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2704 | { |
| 2705 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2706 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2707 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2708 | struct hlua *hlua; |
| 2709 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2710 | int low, high; |
| 2711 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2712 | struct xref *peer; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2713 | struct conn_stream *cs; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2714 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2715 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2716 | if (lua_gettop(L) < 2) |
| 2717 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2718 | |
| 2719 | /* Get args. */ |
| 2720 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2721 | |
| 2722 | /* Check if we run on the same thread than the xreator thread. |
| 2723 | * We cannot access to the socket if the thread is different. |
| 2724 | */ |
| 2725 | if (socket->tid != tid) |
| 2726 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2727 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2728 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2729 | if (lua_gettop(L) >= 3) { |
| 2730 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2731 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2732 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2733 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2734 | * that are not enclosed within square brackets. |
| 2735 | */ |
| 2736 | if (port > 0) { |
| 2737 | luaL_buffinit(L, &b); |
| 2738 | luaL_addstring(&b, ip); |
| 2739 | luaL_addchar(&b, ':'); |
| 2740 | luaL_pushresult(&b); |
| 2741 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2742 | } |
| 2743 | } |
| 2744 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2745 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2746 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2747 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2748 | lua_pushnil(L); |
| 2749 | return 1; |
| 2750 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2751 | |
| 2752 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2753 | 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] | 2754 | if (!addr) { |
| 2755 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2756 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2757 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2758 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2759 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2760 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2761 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2762 | if (port == -1) { |
| 2763 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2764 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2765 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2766 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2767 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2768 | if (port == -1) { |
| 2769 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2770 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2771 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2772 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2773 | } |
| 2774 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2775 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2776 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2777 | cs = appctx->owner; |
| 2778 | s = __cs_strm(cs); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2779 | |
Christopher Faulet | 8da67aa | 2022-03-29 17:53:09 +0200 | [diff] [blame] | 2780 | if (!sockaddr_alloc(&cs_opposite(cs)->dst, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2781 | xref_unlock(&socket->xref, peer); |
| 2782 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2783 | } |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2784 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2785 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2786 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2787 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2788 | if (!hlua) |
| 2789 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2790 | |
| 2791 | /* inform the stream that we want to be notified whenever the |
| 2792 | * connection completes. |
| 2793 | */ |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 2794 | cs_cant_get(s->csf); |
| 2795 | cs_rx_endp_more(s->csf); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2796 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2797 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2798 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2799 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2800 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2801 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2802 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2803 | } |
| 2804 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2805 | |
| 2806 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2807 | /* Return yield waiting for connection. */ |
| 2808 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2809 | 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] | 2810 | |
| 2811 | return 0; |
| 2812 | } |
| 2813 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2814 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2815 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2816 | { |
| 2817 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2818 | struct xref *peer; |
| 2819 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2820 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2821 | |
| 2822 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2823 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2824 | |
| 2825 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2826 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2827 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2828 | lua_pushnil(L); |
| 2829 | return 1; |
| 2830 | } |
| 2831 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2832 | s = __cs_strm(appctx->owner); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2833 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2834 | s->target = &socket_ssl->obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2835 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2836 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2837 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2838 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2839 | |
| 2840 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2841 | { |
| 2842 | return 0; |
| 2843 | } |
| 2844 | |
| 2845 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2846 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2847 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2848 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2849 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2850 | struct xref *peer; |
| 2851 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2852 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2853 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2854 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2855 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2856 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2857 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2858 | /* convert the timeout to millis */ |
| 2859 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2860 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2861 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2862 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2863 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2864 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2865 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2866 | 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] | 2867 | |
| 2868 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2869 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2870 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2871 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2872 | /* Check if we run on the same thread than the xreator thread. |
| 2873 | * We cannot access to the socket if the thread is different. |
| 2874 | */ |
| 2875 | if (socket->tid != tid) |
| 2876 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2877 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2878 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2879 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2880 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2881 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2882 | WILL_LJMP(lua_error(L)); |
| 2883 | return 0; |
| 2884 | } |
| 2885 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2886 | s = __cs_strm(appctx->owner); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2887 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2888 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2889 | s->req.rto = tmout; |
| 2890 | s->req.wto = tmout; |
| 2891 | s->res.rto = tmout; |
| 2892 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2893 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2894 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2895 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2896 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2897 | |
| 2898 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2899 | task_queue(s->task); |
| 2900 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2901 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2902 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2903 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2904 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2905 | } |
| 2906 | |
| 2907 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2908 | { |
| 2909 | struct hlua_socket *socket; |
| 2910 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2911 | struct session *sess; |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 2912 | struct conn_stream *cs; |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 2913 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2914 | |
| 2915 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2916 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2917 | hlua_pusherror(L, "socket: full stack"); |
| 2918 | goto out_fail_conf; |
| 2919 | } |
| 2920 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2921 | /* Create the object: obj[0] = userdata. */ |
| 2922 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2923 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2924 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2925 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2926 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2927 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2928 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2929 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2930 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2931 | goto out_fail_conf; |
| 2932 | } |
| 2933 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2934 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2935 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2936 | lua_setmetatable(L, -2); |
| 2937 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2938 | /* Create the applet context */ |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 2939 | appctx = appctx_new(&update_applet, NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2940 | if (!appctx) { |
| 2941 | hlua_pusherror(L, "socket: out of memory"); |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 2942 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2943 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2944 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2945 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2946 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2947 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2948 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2949 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2950 | /* Now create a session, task and stream for this applet */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 2951 | sess = session_new(socket_proxy, NULL, &appctx->obj_type); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2952 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2953 | hlua_pusherror(L, "socket: out of memory"); |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 2954 | goto out_fail_appctx; |
| 2955 | } |
| 2956 | |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 2957 | cs = cs_new_from_applet(appctx->endp, sess, &BUF_NULL); |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 2958 | if (!cs) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2959 | hlua_pusherror(L, "socket: out of memory"); |
Christopher Faulet | 2479e5f | 2022-01-19 14:50:11 +0100 | [diff] [blame] | 2960 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2961 | } |
| 2962 | |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 2963 | s = DISGUISE(cs_strm(cs)); |
Christopher Faulet | 2479e5f | 2022-01-19 14:50:11 +0100 | [diff] [blame] | 2964 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2965 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2966 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2967 | |
Christopher Faulet | 6b0a0fb | 2022-04-04 11:29:28 +0200 | [diff] [blame^] | 2968 | /* Configure "right" conn-stream. this "si" is used to connect |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2969 | * and retrieve data from the server. The connection is initialized |
| 2970 | * with the "struct server". |
| 2971 | */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 2972 | cs_set_state(s->csb, CS_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2973 | |
| 2974 | /* Force destination server. */ |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 2975 | s->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
| 2976 | s->target = &socket_tcp->obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2977 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2978 | return 1; |
| 2979 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2980 | out_fail_sess: |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 2981 | session_free(sess); |
| 2982 | out_fail_appctx: |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2983 | appctx_free(appctx); |
| 2984 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2985 | WILL_LJMP(lua_error(L)); |
| 2986 | return 0; |
| 2987 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2988 | |
| 2989 | /* |
| 2990 | * |
| 2991 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2992 | * Class Channel |
| 2993 | * |
| 2994 | * |
| 2995 | */ |
| 2996 | |
| 2997 | /* Returns the struct hlua_channel join to the class channel in the |
| 2998 | * stack entry "ud" or throws an argument error. |
| 2999 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3000 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3001 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3002 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3003 | } |
| 3004 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3005 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 3006 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3007 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 3008 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3009 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3010 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3011 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3012 | return 0; |
| 3013 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3014 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3015 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3016 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3017 | |
| 3018 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3019 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 3020 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3021 | return 1; |
| 3022 | } |
| 3023 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3024 | /* Helper function returning a filter attached to a channel at the position <ud> |
| 3025 | * 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] | 3026 | * filter is attached, NULL is returned and <offset> and <len> are not |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3027 | * initialized. |
| 3028 | */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3029 | 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] | 3030 | { |
| 3031 | struct filter *filter = NULL; |
| 3032 | |
| 3033 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 3034 | struct hlua_flt_ctx *flt_ctx; |
| 3035 | |
| 3036 | filter = lua_touserdata (L, -1); |
| 3037 | flt_ctx = filter->ctx; |
| 3038 | if (hlua_filter_from_payload(filter)) { |
| 3039 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 3040 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 3041 | } |
| 3042 | } |
| 3043 | |
| 3044 | lua_pop(L, 1); |
| 3045 | return filter; |
| 3046 | } |
| 3047 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3048 | /* Copies <len> bytes of data present in the channel's buffer, starting at the |
| 3049 | * 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] | 3050 | * responsibility to ensure <len> and <offset> are valid. It always return the |
| 3051 | * length of the built string. <len> may be 0, in this case, an empty string is |
| 3052 | * created and 0 is returned. |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3053 | */ |
| 3054 | 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] | 3055 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3056 | size_t block1, block2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3057 | luaL_Buffer b; |
| 3058 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3059 | block1 = len; |
| 3060 | if (block1 > b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset))) |
| 3061 | block1 = b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset)); |
| 3062 | block2 = len - block1; |
| 3063 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3064 | luaL_buffinit(L, &b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3065 | luaL_addlstring(&b, b_peek(&chn->buf, offset), block1); |
| 3066 | if (block2) |
| 3067 | luaL_addlstring(&b, b_orig(&chn->buf), block2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3068 | luaL_pushresult(&b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3069 | return len; |
| 3070 | } |
| 3071 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3072 | /* Inserts the string <str> to the channel's buffer at the offset <offset>. This |
| 3073 | * function returns -1 if data cannot be copied. Otherwise, it returns the |
| 3074 | * number of bytes copied. |
| 3075 | */ |
| 3076 | static int _hlua_channel_insert(struct channel *chn, lua_State *L, struct ist str, size_t offset) |
| 3077 | { |
| 3078 | int ret = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3079 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3080 | /* Nothing to do, just return */ |
| 3081 | if (unlikely(istlen(str) == 0)) |
| 3082 | goto end; |
| 3083 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3084 | if (istlen(str) > c_room(chn)) { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3085 | ret = -1; |
| 3086 | goto end; |
| 3087 | } |
| 3088 | ret = b_insert_blk(&chn->buf, offset, istptr(str), istlen(str)); |
| 3089 | |
| 3090 | end: |
| 3091 | return ret; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3092 | } |
| 3093 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3094 | /* Removes <len> bytes of data at the absolute position <offset>. |
| 3095 | */ |
| 3096 | static void _hlua_channel_delete(struct channel *chn, size_t offset, size_t len) |
| 3097 | { |
| 3098 | size_t end = offset + len; |
| 3099 | |
| 3100 | if (b_peek(&chn->buf, end) != b_tail(&chn->buf)) |
| 3101 | b_move(&chn->buf, b_peek_ofs(&chn->buf, end), |
| 3102 | b_data(&chn->buf) - end, -len); |
| 3103 | b_sub(&chn->buf, len); |
| 3104 | } |
| 3105 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3106 | /* Copies input data in the channel's buffer. It is possible to set a specific |
| 3107 | * offset (0 by default) and a length (all remaining input data starting for the |
| 3108 | * offset by default). If there is not enough input data and more data can be |
| 3109 | * received, this function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3110 | * |
| 3111 | * From an action, All input data are considered. For a filter, the offset and |
| 3112 | * 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] | 3113 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3114 | __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] | 3115 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3116 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3117 | struct filter *filter; |
| 3118 | size_t input, output; |
| 3119 | int offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3120 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3121 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3122 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3123 | output = co_data(chn); |
| 3124 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3125 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3126 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3127 | if (filter && !hlua_filter_from_payload(filter)) |
| 3128 | WILL_LJMP(lua_error(L)); |
| 3129 | |
| 3130 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3131 | if (lua_gettop(L) > 1) { |
| 3132 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3133 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3134 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3135 | offset += output; |
| 3136 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3137 | lua_pushfstring(L, "offset out of range."); |
| 3138 | WILL_LJMP(lua_error(L)); |
| 3139 | } |
| 3140 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3141 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3142 | if (lua_gettop(L) == 3) { |
| 3143 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3144 | if (!len) |
| 3145 | goto dup; |
| 3146 | if (len == -1) |
| 3147 | len = global.tune.bufsize; |
| 3148 | if (len < 0) { |
| 3149 | lua_pushfstring(L, "length out of range."); |
| 3150 | WILL_LJMP(lua_error(L)); |
| 3151 | } |
| 3152 | } |
| 3153 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3154 | if (offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3155 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3156 | /* Yield waiting for more data, as requested */ |
| 3157 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_data_yield, TICK_ETERNITY, 0)); |
| 3158 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3159 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3160 | } |
| 3161 | |
| 3162 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3163 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3164 | return 1; |
| 3165 | } |
| 3166 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3167 | /* Copies the first line (including the trailing LF) of input data in the |
| 3168 | * channel's buffer. It is possible to set a specific offset (0 by default) and |
| 3169 | * a length (all remaining input data starting for the offset by default). If |
| 3170 | * there is not enough input data and more data can be received, the function |
| 3171 | * yields. If a length is explicitly specified, no more data are |
| 3172 | * copied. Otherwise, if no LF is found and more data can be received, this |
| 3173 | * function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3174 | * |
| 3175 | * From an action, All input data are considered. For a filter, the offset and |
| 3176 | * 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] | 3177 | */ |
| 3178 | __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] | 3179 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3180 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3181 | struct filter *filter; |
| 3182 | size_t l, input, output; |
| 3183 | int offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3184 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3185 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3186 | output = co_data(chn); |
| 3187 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3188 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3189 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3190 | if (filter && !hlua_filter_from_payload(filter)) |
| 3191 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3192 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3193 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3194 | if (lua_gettop(L) > 1) { |
| 3195 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3196 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3197 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3198 | offset += output; |
| 3199 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3200 | lua_pushfstring(L, "offset out of range."); |
| 3201 | WILL_LJMP(lua_error(L)); |
| 3202 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3203 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3204 | |
| 3205 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3206 | if (lua_gettop(L) == 3) { |
| 3207 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3208 | if (!len) |
| 3209 | goto dup; |
| 3210 | if (len == -1) |
| 3211 | len = global.tune.bufsize; |
| 3212 | if (len < 0) { |
| 3213 | lua_pushfstring(L, "length out of range."); |
| 3214 | WILL_LJMP(lua_error(L)); |
| 3215 | } |
| 3216 | } |
| 3217 | |
| 3218 | for (l = 0; l < len; l++) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3219 | if (l + offset >= output + input) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3220 | break; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3221 | if (*(b_peek(&chn->buf, offset + l)) == '\n') { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3222 | len = l+1; |
| 3223 | goto dup; |
| 3224 | } |
| 3225 | } |
| 3226 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3227 | if (offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3228 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3229 | /* Yield waiting for more data */ |
| 3230 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_line_yield, TICK_ETERNITY, 0)); |
| 3231 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3232 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3233 | } |
| 3234 | |
| 3235 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3236 | _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3237 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3238 | } |
| 3239 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3240 | /* [ DEPRECATED ] |
| 3241 | * |
| 3242 | * Duplicate all input data foud in the channel's buffer. The data are not |
| 3243 | * removed from the buffer. This function relies on _hlua_channel_dup(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3244 | * |
| 3245 | * From an action, All input data are considered. For a filter, the offset and |
| 3246 | * 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] | 3247 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3248 | __LJMP static int hlua_channel_dup(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3249 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3250 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3251 | struct filter *filter; |
| 3252 | size_t offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3253 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3254 | MAY_LJMP(check_args(L, 1, "dup")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3255 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3256 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3257 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3258 | WILL_LJMP(lua_error(L)); |
| 3259 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3260 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3261 | offset = co_data(chn); |
| 3262 | len = ci_data(chn); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3263 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3264 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3265 | if (filter && !hlua_filter_from_payload(filter)) |
| 3266 | WILL_LJMP(lua_error(L)); |
| 3267 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3268 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3269 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3270 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3271 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3272 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3273 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3274 | return 1; |
| 3275 | } |
| 3276 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3277 | /* [ DEPRECATED ] |
| 3278 | * |
| 3279 | * Get all input data foud in the channel's buffer. The data are removed from |
| 3280 | * the buffer after the copy. This function relies on _hlua_channel_dup() and |
| 3281 | * _hlua_channel_delete(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3282 | * |
| 3283 | * From an action, All input data are considered. For a filter, the offset and |
| 3284 | * 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] | 3285 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3286 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3287 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3288 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3289 | struct filter *filter; |
| 3290 | size_t offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3291 | int ret; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3292 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3293 | MAY_LJMP(check_args(L, 1, "get")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3294 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3295 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3296 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3297 | WILL_LJMP(lua_error(L)); |
| 3298 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3299 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3300 | offset = co_data(chn); |
| 3301 | len = ci_data(chn); |
| 3302 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3303 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3304 | if (filter && !hlua_filter_from_payload(filter)) |
| 3305 | WILL_LJMP(lua_error(L)); |
| 3306 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3307 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3308 | lua_pushnil(L); |
| 3309 | return 1; |
| 3310 | } |
| 3311 | |
| 3312 | ret = _hlua_channel_dup(chn, L, offset, len); |
| 3313 | _hlua_channel_delete(chn, offset, ret); |
| 3314 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3315 | } |
| 3316 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3317 | /* This functions consumes and returns one line. If the channel is closed, |
| 3318 | * 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] | 3319 | * 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] | 3320 | * value. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3321 | * |
| 3322 | * From an action, All input data are considered. For a filter, the offset and |
| 3323 | * 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] | 3324 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3325 | __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] | 3326 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3327 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3328 | struct filter *filter; |
| 3329 | size_t l, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3330 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3331 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3332 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3333 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3334 | offset = co_data(chn); |
| 3335 | len = ci_data(chn); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3336 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3337 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3338 | if (filter && !hlua_filter_from_payload(filter)) |
| 3339 | WILL_LJMP(lua_error(L)); |
| 3340 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3341 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3342 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3343 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3344 | } |
| 3345 | |
| 3346 | for (l = 0; l < len; l++) { |
| 3347 | if (*(b_peek(&chn->buf, offset+l)) == '\n') { |
| 3348 | len = l+1; |
| 3349 | goto dup; |
| 3350 | } |
| 3351 | } |
| 3352 | |
| 3353 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3354 | /* Yield waiting for more data */ |
| 3355 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0)); |
| 3356 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3357 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3358 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3359 | ret = _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3360 | _hlua_channel_delete(chn, offset, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3361 | return 1; |
| 3362 | } |
| 3363 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3364 | /* [ DEPRECATED ] |
| 3365 | * |
| 3366 | * Check arguments for the function "hlua_channel_getline_yield". |
| 3367 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3368 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3369 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3370 | struct channel *chn; |
| 3371 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3372 | MAY_LJMP(check_args(L, 1, "getline")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3373 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3374 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3375 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3376 | WILL_LJMP(lua_error(L)); |
| 3377 | } |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3378 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3379 | } |
| 3380 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3381 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3382 | * available input data are returned. The offset may be negactive to start from |
| 3383 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3384 | * size. |
| 3385 | */ |
| 3386 | __LJMP static int hlua_channel_get_data(lua_State *L) |
| 3387 | { |
| 3388 | struct channel *chn; |
| 3389 | |
| 3390 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3391 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 3392 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3393 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3394 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3395 | WILL_LJMP(lua_error(L)); |
| 3396 | } |
| 3397 | return MAY_LJMP(hlua_channel_get_data_yield(L, 0, 0)); |
| 3398 | } |
| 3399 | |
| 3400 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3401 | * available input data are returned. The offset may be negactive to start from |
| 3402 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3403 | * size. |
| 3404 | */ |
| 3405 | __LJMP static int hlua_channel_get_line(lua_State *L) |
| 3406 | { |
| 3407 | struct channel *chn; |
| 3408 | |
| 3409 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3410 | WILL_LJMP(luaL_error(L, "'line' expects at most 2 arguments")); |
| 3411 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3412 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3413 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3414 | WILL_LJMP(lua_error(L)); |
| 3415 | } |
| 3416 | return MAY_LJMP(hlua_channel_get_line_yield(L, 0, 0)); |
| 3417 | } |
| 3418 | |
| 3419 | /* Appends a string into the input side of channel. It returns the length of the |
| 3420 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3421 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3422 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3423 | * |
| 3424 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3425 | */ |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3426 | __LJMP static int hlua_channel_append(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3427 | { |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3428 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3429 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3430 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3431 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3432 | int ret; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3433 | |
| 3434 | MAY_LJMP(check_args(L, 2, "append")); |
| 3435 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3436 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3437 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3438 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3439 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3440 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3441 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3442 | offset = co_data(chn); |
| 3443 | len = ci_data(chn); |
| 3444 | |
| 3445 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3446 | if (filter && !hlua_filter_from_payload(filter)) |
| 3447 | WILL_LJMP(lua_error(L)); |
| 3448 | |
| 3449 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3450 | if (ret > 0 && filter) { |
| 3451 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3452 | |
| 3453 | flt_update_offsets(filter, chn, ret); |
| 3454 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3455 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3456 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3457 | return 1; |
| 3458 | } |
| 3459 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3460 | /* Prepends a string into the input side of channel. It returns the length of the |
| 3461 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3462 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3463 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3464 | * |
| 3465 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3466 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3467 | __LJMP static int hlua_channel_prepend(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3468 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3469 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3470 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3471 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3472 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3473 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3474 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3475 | MAY_LJMP(check_args(L, 2, "prepend")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3476 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3477 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3478 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3479 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3480 | WILL_LJMP(lua_error(L)); |
| 3481 | } |
| 3482 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3483 | offset = co_data(chn); |
| 3484 | len = ci_data(chn); |
| 3485 | |
| 3486 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3487 | if (filter && !hlua_filter_from_payload(filter)) |
| 3488 | WILL_LJMP(lua_error(L)); |
| 3489 | |
| 3490 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3491 | if (ret > 0 && filter) { |
| 3492 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3493 | |
| 3494 | flt_update_offsets(filter, chn, ret); |
| 3495 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3496 | } |
| 3497 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3498 | lua_pushinteger(L, ret); |
| 3499 | return 1; |
| 3500 | } |
| 3501 | |
| 3502 | /* Inserts a given amount of input data at the given offset by a string |
| 3503 | * content. By default the string is appended at the end of input data. It |
| 3504 | * returns the length of the written string, or -1 if the channel is closed or |
| 3505 | * if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3506 | * |
| 3507 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3508 | */ |
| 3509 | __LJMP static int hlua_channel_insert_data(lua_State *L) |
| 3510 | { |
| 3511 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3512 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3513 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3514 | size_t sz, input, output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3515 | int ret, offset; |
| 3516 | |
| 3517 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 3518 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
| 3519 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3520 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3521 | |
| 3522 | output = co_data(chn); |
| 3523 | input = ci_data(chn); |
| 3524 | |
| 3525 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3526 | if (filter && !hlua_filter_from_payload(filter)) |
| 3527 | WILL_LJMP(lua_error(L)); |
| 3528 | |
| 3529 | offset = input + output; |
| 3530 | if (lua_gettop(L) > 2) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3531 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3532 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3533 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3534 | offset += output; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3535 | if (offset < output || offset > output + input) { |
| 3536 | lua_pushfstring(L, "offset out of range."); |
| 3537 | WILL_LJMP(lua_error(L)); |
| 3538 | } |
| 3539 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3540 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3541 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3542 | WILL_LJMP(lua_error(L)); |
| 3543 | } |
| 3544 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3545 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3546 | if (ret > 0 && filter) { |
| 3547 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3548 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3549 | flt_update_offsets(filter, chn, ret); |
| 3550 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3551 | } |
| 3552 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3553 | lua_pushinteger(L, ret); |
| 3554 | return 1; |
| 3555 | } |
| 3556 | /* Replaces a given amount of input data at the given offset by a string |
| 3557 | * content. By default all remaining data are removed (offset = 0 and len = |
| 3558 | * -1). It returns the length of the written string, or -1 if the channel is |
| 3559 | * closed or if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3560 | * |
| 3561 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3562 | */ |
| 3563 | __LJMP static int hlua_channel_set_data(lua_State *L) |
| 3564 | { |
| 3565 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3566 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3567 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3568 | size_t sz, input, output; |
| 3569 | int ret, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3570 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3571 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 3572 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 3573 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3574 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3575 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3576 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3577 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3578 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3579 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3580 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3581 | output = co_data(chn); |
| 3582 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3583 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3584 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3585 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3586 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3587 | |
| 3588 | offset = output; |
| 3589 | if (lua_gettop(L) > 2) { |
| 3590 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3591 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3592 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3593 | offset += output; |
| 3594 | if (offset < output || offset > input + output) { |
| 3595 | lua_pushfstring(L, "offset out of range."); |
| 3596 | WILL_LJMP(lua_error(L)); |
| 3597 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3598 | } |
| 3599 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3600 | len = output + input - offset; |
| 3601 | if (lua_gettop(L) == 4) { |
| 3602 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 3603 | if (!len) |
| 3604 | goto set; |
| 3605 | if (len == -1) |
| 3606 | len = output + input - offset; |
| 3607 | if (len < 0 || offset + len > output + input) { |
| 3608 | lua_pushfstring(L, "length out of range."); |
| 3609 | WILL_LJMP(lua_error(L)); |
| 3610 | } |
| 3611 | } |
| 3612 | |
| 3613 | set: |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3614 | /* 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] | 3615 | if (sz > c_room(chn) + len) |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3616 | lua_pushinteger(L, -1); |
| 3617 | else { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3618 | _hlua_channel_delete(chn, offset, len); |
| 3619 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3620 | if (filter) { |
| 3621 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3622 | |
| 3623 | len -= (ret > 0 ? ret : 0); |
| 3624 | flt_update_offsets(filter, chn, -len); |
| 3625 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3626 | } |
| 3627 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3628 | lua_pushinteger(L, ret); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3629 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3630 | return 1; |
| 3631 | } |
| 3632 | |
| 3633 | /* 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] | 3634 | * 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] | 3635 | * the removed data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3636 | * |
| 3637 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3638 | */ |
| 3639 | __LJMP static int hlua_channel_del_data(lua_State *L) |
| 3640 | { |
| 3641 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3642 | struct filter *filter; |
| 3643 | size_t input, output; |
| 3644 | int offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3645 | |
| 3646 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3647 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
| 3648 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3649 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3650 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3651 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3652 | WILL_LJMP(lua_error(L)); |
| 3653 | } |
| 3654 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3655 | output = co_data(chn); |
| 3656 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3657 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3658 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3659 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3660 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3661 | |
| 3662 | offset = output; |
| 3663 | if (lua_gettop(L) > 2) { |
| 3664 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3665 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3666 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3667 | offset += output; |
| 3668 | if (offset < output || offset > input + output) { |
| 3669 | lua_pushfstring(L, "offset out of range."); |
| 3670 | WILL_LJMP(lua_error(L)); |
| 3671 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3672 | } |
| 3673 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3674 | len = output + input - offset; |
| 3675 | if (lua_gettop(L) == 4) { |
| 3676 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 3677 | if (!len) |
| 3678 | goto end; |
| 3679 | if (len == -1) |
| 3680 | len = output + input - offset; |
| 3681 | if (len < 0 || offset + len > output + input) { |
| 3682 | lua_pushfstring(L, "length out of range."); |
| 3683 | WILL_LJMP(lua_error(L)); |
| 3684 | } |
| 3685 | } |
| 3686 | |
| 3687 | _hlua_channel_delete(chn, offset, len); |
| 3688 | if (filter) { |
| 3689 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3690 | |
| 3691 | flt_update_offsets(filter, chn, -len); |
| 3692 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3693 | } |
| 3694 | |
| 3695 | end: |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3696 | lua_pushinteger(L, len); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3697 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3698 | } |
| 3699 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3700 | /* Append data in the output side of the buffer. This data is immediately |
| 3701 | * sent. The function returns the amount of data written. If the buffer |
| 3702 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3703 | * if the channel is closed. |
| 3704 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3705 | __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] | 3706 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3707 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3708 | struct filter *filter; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3709 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3710 | size_t offset, len, sz; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3711 | int l, ret; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3712 | struct hlua *hlua; |
| 3713 | |
| 3714 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3715 | hlua = hlua_gethlua(L); |
| 3716 | if (!hlua) { |
| 3717 | lua_pushnil(L); |
| 3718 | return 1; |
| 3719 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3720 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3721 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3722 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3723 | l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3724 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3725 | offset = co_data(chn); |
| 3726 | len = ci_data(chn); |
| 3727 | |
| 3728 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3729 | if (filter && !hlua_filter_from_payload(filter)) |
| 3730 | WILL_LJMP(lua_error(L)); |
| 3731 | |
| 3732 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3733 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3734 | lua_pushinteger(L, -1); |
| 3735 | return 1; |
| 3736 | } |
| 3737 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3738 | len = c_room(chn); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3739 | if (len > sz -l) { |
| 3740 | if (filter) { |
| 3741 | lua_pushinteger(L, -1); |
| 3742 | return 1; |
| 3743 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3744 | len = sz - l; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3745 | } |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3746 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3747 | ret = _hlua_channel_insert(chn, L, ist2(str, len), offset); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3748 | if (ret == -1) { |
| 3749 | lua_pop(L, 1); |
| 3750 | lua_pushinteger(L, -1); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3751 | return 1; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3752 | } |
| 3753 | if (ret) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3754 | if (filter) { |
| 3755 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3756 | |
| 3757 | |
| 3758 | flt_update_offsets(filter, chn, ret); |
| 3759 | FLT_OFF(filter, chn) += ret; |
| 3760 | flt_ctx->cur_off[CHN_IDX(chn)] += ret; |
| 3761 | } |
| 3762 | else |
| 3763 | c_adv(chn, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3764 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3765 | l += ret; |
| 3766 | lua_pop(L, 1); |
| 3767 | lua_pushinteger(L, l); |
| 3768 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3769 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3770 | if (l < sz) { |
| 3771 | /* Yield only if the channel's output is not empty. |
| 3772 | * Otherwise it means we cannot add more data. */ |
| 3773 | if (co_data(chn) == 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3774 | return 1; |
| 3775 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3776 | /* If we are waiting for space in the response buffer, we |
| 3777 | * must set the flag WAKERESWR. This flag required the task |
| 3778 | * wake up if any activity is detected on the response buffer. |
| 3779 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3780 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3781 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3782 | else |
| 3783 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3784 | 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] | 3785 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3786 | |
| 3787 | return 1; |
| 3788 | } |
| 3789 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3790 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3791 | * yield the LUA process, and resume it without checking the |
| 3792 | * input arguments. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3793 | * |
| 3794 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3795 | */ |
| 3796 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3797 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3798 | struct channel *chn; |
| 3799 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3800 | MAY_LJMP(check_args(L, 2, "send")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3801 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3802 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3803 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3804 | WILL_LJMP(lua_error(L)); |
| 3805 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3806 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3807 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3808 | } |
| 3809 | |
| 3810 | /* This function forward and amount of butes. The data pass from |
| 3811 | * the input side of the buffer to the output side, and can be |
| 3812 | * forwarded. This function never fails. |
| 3813 | * |
| 3814 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3815 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3816 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3817 | __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] | 3818 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3819 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3820 | struct filter *filter; |
| 3821 | size_t offset, len, fwd; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3822 | int l, max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3823 | struct hlua *hlua; |
| 3824 | |
| 3825 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3826 | hlua = hlua_gethlua(L); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3827 | if (!hlua) { |
| 3828 | lua_pushnil(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3829 | return 1; |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3830 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3831 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3832 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3833 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3834 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3835 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3836 | offset = co_data(chn); |
| 3837 | len = ci_data(chn); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3838 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3839 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3840 | if (filter && !hlua_filter_from_payload(filter)) |
| 3841 | WILL_LJMP(lua_error(L)); |
| 3842 | |
| 3843 | max = fwd - l; |
| 3844 | if (max > len) |
| 3845 | max = len; |
| 3846 | |
| 3847 | if (filter) { |
| 3848 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3849 | |
| 3850 | FLT_OFF(filter, chn) += max; |
| 3851 | flt_ctx->cur_off[CHN_IDX(chn)] += max; |
| 3852 | flt_ctx->cur_len[CHN_IDX(chn)] -= max; |
| 3853 | } |
| 3854 | else |
| 3855 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3856 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3857 | l += max; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3858 | lua_pop(L, 1); |
| 3859 | lua_pushinteger(L, l); |
| 3860 | |
| 3861 | /* Check if it miss bytes to forward. */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3862 | if (l < fwd) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3863 | /* The the input channel or the output channel are closed, we |
| 3864 | * must return the amount of data forwarded. |
| 3865 | */ |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3866 | 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] | 3867 | return 1; |
| 3868 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3869 | /* If we are waiting for space data in the response buffer, we |
| 3870 | * must set the flag WAKERESWR. This flag required the task |
| 3871 | * wake up if any activity is detected on the response buffer. |
| 3872 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3873 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3874 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3875 | else |
| 3876 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3877 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3878 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3879 | 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] | 3880 | } |
| 3881 | |
| 3882 | return 1; |
| 3883 | } |
| 3884 | |
| 3885 | /* Just check the input and prepare the stack for the previous |
| 3886 | * function "hlua_channel_forward_yield" |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3887 | * |
| 3888 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3889 | */ |
| 3890 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3891 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3892 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3893 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3894 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3895 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3896 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3897 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3898 | WILL_LJMP(lua_error(L)); |
| 3899 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3900 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3901 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3902 | } |
| 3903 | |
| 3904 | /* Just returns the number of bytes available in the input |
| 3905 | * side of the buffer. This function never fails. |
| 3906 | */ |
| 3907 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3908 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3909 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3910 | struct filter *filter; |
| 3911 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3912 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3913 | MAY_LJMP(check_args(L, 1, "input")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3914 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3915 | |
| 3916 | output = co_data(chn); |
| 3917 | input = ci_data(chn); |
| 3918 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3919 | if (filter || !IS_HTX_STRM(chn_strm(chn))) |
| 3920 | lua_pushinteger(L, input); |
| 3921 | else { |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3922 | struct htx *htx = htxbuf(&chn->buf); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3923 | |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3924 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3925 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3926 | return 1; |
| 3927 | } |
| 3928 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3929 | /* Returns true if the channel is full. */ |
| 3930 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3931 | { |
| 3932 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3933 | |
| 3934 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3935 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3936 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3937 | * applet). |
| 3938 | */ |
| 3939 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3940 | return 1; |
| 3941 | } |
| 3942 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3943 | /* Returns true if the channel may still receive data. */ |
| 3944 | __LJMP static int hlua_channel_may_recv(lua_State *L) |
| 3945 | { |
| 3946 | struct channel *chn; |
| 3947 | |
| 3948 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 3949 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3950 | lua_pushboolean(L, (!channel_input_closed(chn) && channel_may_recv(chn))); |
| 3951 | return 1; |
| 3952 | } |
| 3953 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3954 | /* Returns true if the channel is the response channel. */ |
| 3955 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3956 | { |
| 3957 | struct channel *chn; |
| 3958 | |
| 3959 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3960 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3961 | |
| 3962 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3963 | return 1; |
| 3964 | } |
| 3965 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3966 | /* Just returns the number of bytes available in the output |
| 3967 | * side of the buffer. This function never fails. |
| 3968 | */ |
| 3969 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3970 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3971 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3972 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3973 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3974 | MAY_LJMP(check_args(L, 1, "output")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3975 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3976 | |
| 3977 | output = co_data(chn); |
| 3978 | input = ci_data(chn); |
| 3979 | hlua_channel_filter(L, 1, chn, &output, &input); |
| 3980 | |
| 3981 | lua_pushinteger(L, output); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3982 | return 1; |
| 3983 | } |
| 3984 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3985 | /* |
| 3986 | * |
| 3987 | * |
| 3988 | * Class Fetches |
| 3989 | * |
| 3990 | * |
| 3991 | */ |
| 3992 | |
| 3993 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3994 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3995 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3996 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3997 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3998 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3999 | } |
| 4000 | |
| 4001 | /* This function creates and push in the stack a fetch object according |
| 4002 | * with a current TXN. |
| 4003 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4004 | 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] | 4005 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4006 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4007 | |
| 4008 | /* Check stack size. */ |
| 4009 | if (!lua_checkstack(L, 3)) |
| 4010 | return 0; |
| 4011 | |
| 4012 | /* Create the object: obj[0] = userdata. |
| 4013 | * Note that the base of the Fetches object is the |
| 4014 | * transaction object. |
| 4015 | */ |
| 4016 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4017 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4018 | lua_rawseti(L, -2, 0); |
| 4019 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4020 | hsmp->s = txn->s; |
| 4021 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4022 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4023 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4024 | |
| 4025 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 4026 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 4027 | lua_setmetatable(L, -2); |
| 4028 | |
| 4029 | return 1; |
| 4030 | } |
| 4031 | |
| 4032 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 4033 | * It uses closure argument to store the associated sample-fetch. It |
| 4034 | * returns only one argument or throws an error. An error is thrown |
| 4035 | * only if an error is encountered during the argument parsing. If |
| 4036 | * the "sample-fetch" function fails, nil is returned. |
| 4037 | */ |
| 4038 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 4039 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4040 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4041 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4042 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4043 | int i; |
| 4044 | struct sample smp; |
| 4045 | |
| 4046 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4047 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4048 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4049 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4050 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4051 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 4052 | /* Check execution authorization. */ |
| 4053 | if (f->use & SMP_USE_HTTP_ANY && |
| 4054 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 4055 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 4056 | "is not available in Lua services", f->kw); |
| 4057 | WILL_LJMP(lua_error(L)); |
| 4058 | } |
| 4059 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4060 | /* Get extra arguments. */ |
| 4061 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 4062 | if (i >= ARGM_NBARGS) |
| 4063 | break; |
| 4064 | hlua_lua2arg(L, i + 2, &args[i]); |
| 4065 | } |
| 4066 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4067 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4068 | |
| 4069 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4070 | 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] | 4071 | |
| 4072 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4073 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4074 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4075 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4076 | } |
| 4077 | |
| 4078 | /* Initialise the sample. */ |
| 4079 | memset(&smp, 0, sizeof(smp)); |
| 4080 | |
| 4081 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4082 | 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] | 4083 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4084 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4085 | lua_pushstring(L, ""); |
| 4086 | else |
| 4087 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4088 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4089 | } |
| 4090 | |
| 4091 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4092 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4093 | hlua_smp2lua_str(L, &smp); |
| 4094 | else |
| 4095 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4096 | |
| 4097 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4098 | free_args(args); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4099 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4100 | |
| 4101 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4102 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4103 | WILL_LJMP(lua_error(L)); |
| 4104 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4105 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4106 | |
| 4107 | /* |
| 4108 | * |
| 4109 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4110 | * Class Converters |
| 4111 | * |
| 4112 | * |
| 4113 | */ |
| 4114 | |
| 4115 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4116 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4117 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4118 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4119 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4120 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4121 | } |
| 4122 | |
| 4123 | /* This function creates and push in the stack a Converters object |
| 4124 | * according with a current TXN. |
| 4125 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4126 | 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] | 4127 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4128 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4129 | |
| 4130 | /* Check stack size. */ |
| 4131 | if (!lua_checkstack(L, 3)) |
| 4132 | return 0; |
| 4133 | |
| 4134 | /* Create the object: obj[0] = userdata. |
| 4135 | * Note that the base of the Converters object is the |
| 4136 | * same than the TXN object. |
| 4137 | */ |
| 4138 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4139 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4140 | lua_rawseti(L, -2, 0); |
| 4141 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4142 | hsmp->s = txn->s; |
| 4143 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4144 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4145 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4146 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4147 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4148 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 4149 | lua_setmetatable(L, -2); |
| 4150 | |
| 4151 | return 1; |
| 4152 | } |
| 4153 | |
| 4154 | /* This function is an LUA binding. It is called with each converter. |
| 4155 | * It uses closure argument to store the associated converter. It |
| 4156 | * returns only one argument or throws an error. An error is thrown |
| 4157 | * only if an error is encountered during the argument parsing. If |
| 4158 | * the converter function function fails, nil is returned. |
| 4159 | */ |
| 4160 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 4161 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4162 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4163 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4164 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4165 | int i; |
| 4166 | struct sample smp; |
| 4167 | |
| 4168 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4169 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4170 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4171 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4172 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4173 | |
| 4174 | /* Get extra arguments. */ |
| 4175 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 4176 | if (i >= ARGM_NBARGS) |
| 4177 | break; |
| 4178 | hlua_lua2arg(L, i + 3, &args[i]); |
| 4179 | } |
| 4180 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4181 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4182 | |
| 4183 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4184 | 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] | 4185 | |
| 4186 | /* Run the special args checker. */ |
| 4187 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 4188 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4189 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4190 | } |
| 4191 | |
| 4192 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4193 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4194 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 4195 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4196 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4197 | } |
| 4198 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4199 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 4200 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4201 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4202 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4203 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4204 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4205 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4206 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4207 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 4208 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4209 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4210 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4211 | } |
| 4212 | |
| 4213 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4214 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4215 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4216 | lua_pushstring(L, ""); |
| 4217 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4218 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4219 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4220 | } |
| 4221 | |
| 4222 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4223 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4224 | hlua_smp2lua_str(L, &smp); |
| 4225 | else |
| 4226 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4227 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4228 | free_args(args); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4229 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4230 | |
| 4231 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4232 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4233 | WILL_LJMP(lua_error(L)); |
| 4234 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4235 | } |
| 4236 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4237 | /* |
| 4238 | * |
| 4239 | * |
| 4240 | * Class AppletTCP |
| 4241 | * |
| 4242 | * |
| 4243 | */ |
| 4244 | |
| 4245 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4246 | * a class stream, otherwise it throws an error. |
| 4247 | */ |
| 4248 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 4249 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4250 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4251 | } |
| 4252 | |
| 4253 | /* This function creates and push in the stack an Applet object |
| 4254 | * according with a current TXN. |
| 4255 | */ |
| 4256 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 4257 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4258 | struct hlua_appctx *luactx; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4259 | struct stream *s = __cs_strm(ctx->owner); |
Christopher Faulet | 2da02ae | 2022-02-24 13:45:27 +0100 | [diff] [blame] | 4260 | struct proxy *p; |
| 4261 | |
| 4262 | ALREADY_CHECKED(s); |
| 4263 | p = s->be; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4264 | |
| 4265 | /* Check stack size. */ |
| 4266 | if (!lua_checkstack(L, 3)) |
| 4267 | return 0; |
| 4268 | |
| 4269 | /* Create the object: obj[0] = userdata. |
| 4270 | * Note that the base of the Converters object is the |
| 4271 | * same than the TXN object. |
| 4272 | */ |
| 4273 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4274 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4275 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4276 | luactx->appctx = ctx; |
| 4277 | luactx->htxn.s = s; |
| 4278 | luactx->htxn.p = p; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4279 | |
| 4280 | /* Create the "f" field that contains a list of fetches. */ |
| 4281 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4282 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4283 | return 0; |
| 4284 | lua_settable(L, -3); |
| 4285 | |
| 4286 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4287 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4288 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4289 | return 0; |
| 4290 | lua_settable(L, -3); |
| 4291 | |
| 4292 | /* Create the "c" field that contains a list of converters. */ |
| 4293 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4294 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4295 | return 0; |
| 4296 | lua_settable(L, -3); |
| 4297 | |
| 4298 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4299 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4300 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4301 | return 0; |
| 4302 | lua_settable(L, -3); |
| 4303 | |
| 4304 | /* Pop a class stream metatable and affect it to the table. */ |
| 4305 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 4306 | lua_setmetatable(L, -2); |
| 4307 | |
| 4308 | return 1; |
| 4309 | } |
| 4310 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4311 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 4312 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4313 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4314 | struct stream *s; |
| 4315 | const char *name; |
| 4316 | size_t len; |
| 4317 | struct sample smp; |
| 4318 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4319 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4320 | 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] | 4321 | |
| 4322 | /* It is useles to retrieve the stream, but this function |
| 4323 | * runs only in a stream context. |
| 4324 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4325 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4326 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4327 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4328 | |
| 4329 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4330 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4331 | hlua_lua2smp(L, 3, &smp); |
| 4332 | |
| 4333 | /* Store the sample in a variable. */ |
| 4334 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4335 | |
| 4336 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4337 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4338 | else |
| 4339 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4340 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4341 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4342 | } |
| 4343 | |
| 4344 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 4345 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4346 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4347 | struct stream *s; |
| 4348 | const char *name; |
| 4349 | size_t len; |
| 4350 | struct sample smp; |
| 4351 | |
| 4352 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4353 | |
| 4354 | /* It is useles to retrieve the stream, but this function |
| 4355 | * runs only in a stream context. |
| 4356 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4357 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4358 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4359 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4360 | |
| 4361 | /* Unset the variable. */ |
| 4362 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4363 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4364 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4365 | } |
| 4366 | |
| 4367 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 4368 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4369 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4370 | struct stream *s; |
| 4371 | const char *name; |
| 4372 | size_t len; |
| 4373 | struct sample smp; |
| 4374 | |
| 4375 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4376 | |
| 4377 | /* It is useles to retrieve the stream, but this function |
| 4378 | * runs only in a stream context. |
| 4379 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4380 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4381 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4382 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4383 | |
| 4384 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 4385 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4386 | lua_pushnil(L); |
| 4387 | return 1; |
| 4388 | } |
| 4389 | |
| 4390 | return hlua_smp2lua(L, &smp); |
| 4391 | } |
| 4392 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4393 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 4394 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4395 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4396 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4397 | struct hlua *hlua; |
| 4398 | |
| 4399 | /* 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] | 4400 | if (!s->hlua) |
| 4401 | return 0; |
| 4402 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4403 | |
| 4404 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4405 | |
| 4406 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4407 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4408 | |
| 4409 | /* Get and store new value. */ |
| 4410 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4411 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4412 | |
| 4413 | return 0; |
| 4414 | } |
| 4415 | |
| 4416 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 4417 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4418 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4419 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4420 | struct hlua *hlua; |
| 4421 | |
| 4422 | /* 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] | 4423 | if (!s->hlua) { |
| 4424 | lua_pushnil(L); |
| 4425 | return 1; |
| 4426 | } |
| 4427 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4428 | |
| 4429 | /* Push configuration index in the stack. */ |
| 4430 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4431 | |
| 4432 | return 1; |
| 4433 | } |
| 4434 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4435 | /* If expected data not yet available, it returns a yield. This function |
| 4436 | * consumes the data in the buffer. It returns a string containing the |
| 4437 | * data. This string can be empty. |
| 4438 | */ |
| 4439 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4440 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4441 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4442 | struct conn_stream *cs = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4443 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4444 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4445 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4446 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4447 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4448 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4449 | /* Read the maximum amount of data available. */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4450 | ret = co_getline_nc(cs_oc(cs), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4451 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4452 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4453 | if (ret == 0) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 4454 | cs_cant_get(cs); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4455 | 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] | 4456 | } |
| 4457 | |
| 4458 | /* End of data: commit the total strings and return. */ |
| 4459 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4460 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4461 | return 1; |
| 4462 | } |
| 4463 | |
| 4464 | /* Ensure that the block 2 length is usable. */ |
| 4465 | if (ret == 1) |
| 4466 | len2 = 0; |
| 4467 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4468 | /* don't check the max length read and don't check. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4469 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4470 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4471 | |
| 4472 | /* Consume input channel output buffer data. */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4473 | co_skip(cs_oc(cs), len1 + len2); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4474 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4475 | return 1; |
| 4476 | } |
| 4477 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4478 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4479 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 4480 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4481 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4482 | |
| 4483 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4484 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4485 | |
| 4486 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 4487 | } |
| 4488 | |
| 4489 | /* If expected data not yet available, it returns a yield. This function |
| 4490 | * consumes the data in the buffer. It returns a string containing the |
| 4491 | * data. This string can be empty. |
| 4492 | */ |
| 4493 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4494 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4495 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4496 | struct conn_stream *cs = luactx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4497 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4498 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4499 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4500 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4501 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4502 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4503 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4504 | /* Read the maximum amount of data available. */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4505 | ret = co_getblk_nc(cs_oc(cs), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4506 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4507 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4508 | if (ret == 0) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 4509 | cs_cant_get(cs); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4510 | 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] | 4511 | } |
| 4512 | |
| 4513 | /* End of data: commit the total strings and return. */ |
| 4514 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4515 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4516 | return 1; |
| 4517 | } |
| 4518 | |
| 4519 | /* Ensure that the block 2 length is usable. */ |
| 4520 | if (ret == 1) |
| 4521 | len2 = 0; |
| 4522 | |
| 4523 | if (len == -1) { |
| 4524 | |
| 4525 | /* If len == -1, catenate all the data avalaile and |
| 4526 | * yield because we want to get all the data until |
| 4527 | * the end of data stream. |
| 4528 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4529 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4530 | luaL_addlstring(&luactx->b, blk2, len2); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4531 | co_skip(cs_oc(cs), len1 + len2); |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 4532 | cs_cant_get(cs); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4533 | 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] | 4534 | |
| 4535 | } else { |
| 4536 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4537 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4538 | if (len1 > len) |
| 4539 | len1 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4540 | luaL_addlstring(&luactx->b, blk1, len1); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4541 | len -= len1; |
| 4542 | |
| 4543 | /* Copy the second block. */ |
| 4544 | if (len2 > len) |
| 4545 | len2 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4546 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4547 | len -= len2; |
| 4548 | |
| 4549 | /* Consume input channel output buffer data. */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4550 | co_skip(cs_oc(cs), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4551 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4552 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4553 | if (len > 0) { |
| 4554 | lua_pushinteger(L, len); |
| 4555 | lua_replace(L, 2); |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 4556 | cs_cant_get(cs); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4557 | 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] | 4558 | } |
| 4559 | |
| 4560 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4561 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4562 | return 1; |
| 4563 | } |
| 4564 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4565 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4566 | hlua_pusherror(L, "Lua: internal error"); |
| 4567 | WILL_LJMP(lua_error(L)); |
| 4568 | return 0; |
| 4569 | } |
| 4570 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4571 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4572 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 4573 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4574 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4575 | int len = -1; |
| 4576 | |
| 4577 | if (lua_gettop(L) > 2) |
| 4578 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4579 | if (lua_gettop(L) >= 2) { |
| 4580 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4581 | lua_pop(L, 1); |
| 4582 | } |
| 4583 | |
| 4584 | /* Confirm or set the required length */ |
| 4585 | lua_pushinteger(L, len); |
| 4586 | |
| 4587 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4588 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4589 | |
| 4590 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4591 | } |
| 4592 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4593 | /* Append data in the output side of the buffer. This data is immediately |
| 4594 | * sent. The function returns the amount of data written. If the buffer |
| 4595 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4596 | * if the channel is closed. |
| 4597 | */ |
| 4598 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4599 | { |
| 4600 | size_t len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4601 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4602 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4603 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4604 | struct conn_stream *cs = luactx->appctx->owner; |
| 4605 | struct channel *chn = cs_ic(cs); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4606 | int max; |
| 4607 | |
| 4608 | /* Get the max amount of data which can write as input in the channel. */ |
| 4609 | max = channel_recv_max(chn); |
| 4610 | if (max > (len - l)) |
| 4611 | max = len - l; |
| 4612 | |
| 4613 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4614 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4615 | |
| 4616 | /* update counters. */ |
| 4617 | l += max; |
| 4618 | lua_pop(L, 1); |
| 4619 | lua_pushinteger(L, l); |
| 4620 | |
| 4621 | /* If some data is not send, declares the situation to the |
| 4622 | * applet, and returns a yield. |
| 4623 | */ |
| 4624 | if (l < len) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 4625 | cs_rx_room_blk(cs); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4626 | 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] | 4627 | } |
| 4628 | |
| 4629 | return 1; |
| 4630 | } |
| 4631 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4632 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4633 | * yield the LUA process, and resume it without checking the |
| 4634 | * input arguments. |
| 4635 | */ |
| 4636 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4637 | { |
| 4638 | MAY_LJMP(check_args(L, 2, "send")); |
| 4639 | lua_pushinteger(L, 0); |
| 4640 | |
| 4641 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4642 | } |
| 4643 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4644 | /* |
| 4645 | * |
| 4646 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4647 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4648 | * |
| 4649 | * |
| 4650 | */ |
| 4651 | |
| 4652 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4653 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4654 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4655 | __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] | 4656 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4657 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4658 | } |
| 4659 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4660 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4661 | * according with a current TXN. |
| 4662 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4663 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4664 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4665 | struct hlua_appctx *luactx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4666 | struct hlua_txn htxn; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4667 | struct stream *s = __cs_strm(ctx->owner); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4668 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4669 | struct htx *htx; |
| 4670 | struct htx_blk *blk; |
| 4671 | struct htx_sl *sl; |
| 4672 | struct ist path; |
| 4673 | unsigned long long len = 0; |
| 4674 | int32_t pos; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4675 | struct http_uri_parser parser; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4676 | |
| 4677 | /* Check stack size. */ |
| 4678 | if (!lua_checkstack(L, 3)) |
| 4679 | return 0; |
| 4680 | |
| 4681 | /* Create the object: obj[0] = userdata. |
| 4682 | * Note that the base of the Converters object is the |
| 4683 | * same than the TXN object. |
| 4684 | */ |
| 4685 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4686 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4687 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4688 | luactx->appctx = ctx; |
| 4689 | luactx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
| 4690 | luactx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */ |
| 4691 | luactx->htxn.s = s; |
| 4692 | luactx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4693 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4694 | /* Create the "f" field that contains a list of fetches. */ |
| 4695 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4696 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4697 | return 0; |
| 4698 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4699 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4700 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4701 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4702 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4703 | return 0; |
| 4704 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4705 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4706 | /* Create the "c" field that contains a list of converters. */ |
| 4707 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4708 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4709 | return 0; |
| 4710 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4711 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4712 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4713 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4714 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4715 | return 0; |
| 4716 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4717 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4718 | htx = htxbuf(&s->req.buf); |
| 4719 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4720 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4721 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4722 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4723 | /* Stores the request method. */ |
| 4724 | lua_pushstring(L, "method"); |
| 4725 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4726 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4727 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4728 | /* Stores the http version. */ |
| 4729 | lua_pushstring(L, "version"); |
| 4730 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4731 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4732 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4733 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4734 | * the array on the top of the stack. |
| 4735 | */ |
| 4736 | lua_pushstring(L, "headers"); |
| 4737 | htxn.s = s; |
| 4738 | htxn.p = px; |
| 4739 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4740 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4741 | return 0; |
| 4742 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4743 | |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4744 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 4745 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4746 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4747 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4748 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4749 | p = path.ptr; |
Tim Duesterhus | 4c8f75f | 2021-11-06 15:14:44 +0100 | [diff] [blame] | 4750 | end = istend(path); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4751 | q = p; |
| 4752 | while (q < end && *q != '?') |
| 4753 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4754 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4755 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4756 | lua_pushstring(L, "path"); |
| 4757 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4758 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4759 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4760 | /* Stores the query string. */ |
| 4761 | lua_pushstring(L, "qs"); |
| 4762 | if (*q == '?') |
| 4763 | q++; |
| 4764 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4765 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4766 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4767 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4768 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4769 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4770 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4771 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4772 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4773 | break; |
| 4774 | if (type == HTX_BLK_DATA) |
| 4775 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4776 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4777 | if (htx->extra != ULLONG_MAX) |
| 4778 | len += htx->extra; |
| 4779 | |
| 4780 | /* Stores the request path. */ |
| 4781 | lua_pushstring(L, "length"); |
| 4782 | lua_pushinteger(L, len); |
| 4783 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4784 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4785 | /* Create an empty array of HTTP request headers. */ |
| 4786 | lua_pushstring(L, "response"); |
| 4787 | lua_newtable(L); |
| 4788 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4789 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4790 | /* Pop a class stream metatable and affect it to the table. */ |
| 4791 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4792 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4793 | |
| 4794 | return 1; |
| 4795 | } |
| 4796 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4797 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4798 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4799 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4800 | struct stream *s; |
| 4801 | const char *name; |
| 4802 | size_t len; |
| 4803 | struct sample smp; |
| 4804 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4805 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4806 | 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] | 4807 | |
| 4808 | /* It is useles to retrieve the stream, but this function |
| 4809 | * runs only in a stream context. |
| 4810 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4811 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4812 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4813 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4814 | |
| 4815 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4816 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4817 | hlua_lua2smp(L, 3, &smp); |
| 4818 | |
| 4819 | /* Store the sample in a variable. */ |
| 4820 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4821 | |
| 4822 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4823 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4824 | else |
| 4825 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4826 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4827 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4828 | } |
| 4829 | |
| 4830 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4831 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4832 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4833 | struct stream *s; |
| 4834 | const char *name; |
| 4835 | size_t len; |
| 4836 | struct sample smp; |
| 4837 | |
| 4838 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4839 | |
| 4840 | /* It is useles to retrieve the stream, but this function |
| 4841 | * runs only in a stream context. |
| 4842 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4843 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4844 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4845 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4846 | |
| 4847 | /* Unset the variable. */ |
| 4848 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4849 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4850 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4851 | } |
| 4852 | |
| 4853 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4854 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4855 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4856 | struct stream *s; |
| 4857 | const char *name; |
| 4858 | size_t len; |
| 4859 | struct sample smp; |
| 4860 | |
| 4861 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4862 | |
| 4863 | /* It is useles to retrieve the stream, but this function |
| 4864 | * runs only in a stream context. |
| 4865 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4866 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4867 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4868 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4869 | |
| 4870 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 4871 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4872 | lua_pushnil(L); |
| 4873 | return 1; |
| 4874 | } |
| 4875 | |
| 4876 | return hlua_smp2lua(L, &smp); |
| 4877 | } |
| 4878 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4879 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4880 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4881 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4882 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4883 | struct hlua *hlua; |
| 4884 | |
| 4885 | /* 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] | 4886 | if (!s->hlua) |
| 4887 | return 0; |
| 4888 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4889 | |
| 4890 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4891 | |
| 4892 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4893 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4894 | |
| 4895 | /* Get and store new value. */ |
| 4896 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4897 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4898 | |
| 4899 | return 0; |
| 4900 | } |
| 4901 | |
| 4902 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4903 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4904 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4905 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4906 | struct hlua *hlua; |
| 4907 | |
| 4908 | /* 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] | 4909 | if (!s->hlua) { |
| 4910 | lua_pushnil(L); |
| 4911 | return 1; |
| 4912 | } |
| 4913 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4914 | |
| 4915 | /* Push configuration index in the stack. */ |
| 4916 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4917 | |
| 4918 | return 1; |
| 4919 | } |
| 4920 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4921 | /* If expected data not yet available, it returns a yield. This function |
| 4922 | * consumes the data in the buffer. It returns a string containing the |
| 4923 | * data. This string can be empty. |
| 4924 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4925 | __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] | 4926 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4927 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4928 | struct conn_stream *cs = luactx->appctx->owner; |
| 4929 | struct channel *req = cs_oc(cs); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4930 | struct htx *htx; |
| 4931 | struct htx_blk *blk; |
| 4932 | size_t count; |
| 4933 | int stop = 0; |
| 4934 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4935 | htx = htx_from_buf(&req->buf); |
| 4936 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4937 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4938 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4939 | while (count && !stop && blk) { |
| 4940 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4941 | uint32_t sz = htx_get_blksz(blk); |
| 4942 | struct ist v; |
| 4943 | uint32_t vlen; |
| 4944 | char *nl; |
| 4945 | |
| 4946 | vlen = sz; |
| 4947 | if (vlen > count) { |
| 4948 | if (type != HTX_BLK_DATA) |
| 4949 | break; |
| 4950 | vlen = count; |
| 4951 | } |
| 4952 | |
| 4953 | switch (type) { |
| 4954 | case HTX_BLK_UNUSED: |
| 4955 | break; |
| 4956 | |
| 4957 | case HTX_BLK_DATA: |
| 4958 | v = htx_get_blk_value(htx, blk); |
| 4959 | v.len = vlen; |
| 4960 | nl = istchr(v, '\n'); |
| 4961 | if (nl != NULL) { |
| 4962 | stop = 1; |
| 4963 | vlen = nl - v.ptr + 1; |
| 4964 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4965 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4966 | break; |
| 4967 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4968 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4969 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4970 | stop = 1; |
| 4971 | break; |
| 4972 | |
| 4973 | default: |
| 4974 | break; |
| 4975 | } |
| 4976 | |
Willy Tarreau | 8424004 | 2022-02-28 16:51:23 +0100 | [diff] [blame] | 4977 | c_rew(req, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4978 | count -= vlen; |
| 4979 | if (sz == vlen) |
| 4980 | blk = htx_remove_blk(htx, blk); |
| 4981 | else { |
| 4982 | htx_cut_data_blk(htx, blk, vlen); |
| 4983 | break; |
| 4984 | } |
| 4985 | } |
| 4986 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4987 | /* The message was fully consumed and no more data are expected |
| 4988 | * (EOM flag set). |
| 4989 | */ |
| 4990 | if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) |
| 4991 | stop = 1; |
| 4992 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4993 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4994 | if (!stop) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 4995 | cs_cant_get(cs); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4996 | 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] | 4997 | } |
| 4998 | |
| 4999 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5000 | luaL_pushresult(&luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5001 | return 1; |
| 5002 | } |
| 5003 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5004 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5005 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5006 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5007 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5008 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5009 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5010 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5011 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5012 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5013 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5014 | } |
| 5015 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5016 | /* If expected data not yet available, it returns a yield. This function |
| 5017 | * consumes the data in the buffer. It returns a string containing the |
| 5018 | * data. This string can be empty. |
| 5019 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5020 | __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] | 5021 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5022 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 5023 | struct conn_stream *cs = luactx->appctx->owner; |
| 5024 | struct channel *req = cs_oc(cs); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5025 | struct htx *htx; |
| 5026 | struct htx_blk *blk; |
| 5027 | size_t count; |
| 5028 | int len; |
| 5029 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5030 | htx = htx_from_buf(&req->buf); |
| 5031 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5032 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 5033 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5034 | while (count && len && blk) { |
| 5035 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5036 | uint32_t sz = htx_get_blksz(blk); |
| 5037 | struct ist v; |
| 5038 | uint32_t vlen; |
| 5039 | |
| 5040 | vlen = sz; |
| 5041 | if (len > 0 && vlen > len) |
| 5042 | vlen = len; |
| 5043 | if (vlen > count) { |
| 5044 | if (type != HTX_BLK_DATA) |
| 5045 | break; |
| 5046 | vlen = count; |
| 5047 | } |
| 5048 | |
| 5049 | switch (type) { |
| 5050 | case HTX_BLK_UNUSED: |
| 5051 | break; |
| 5052 | |
| 5053 | case HTX_BLK_DATA: |
| 5054 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5055 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5056 | break; |
| 5057 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5058 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5059 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5060 | len = 0; |
| 5061 | break; |
| 5062 | |
| 5063 | default: |
| 5064 | break; |
| 5065 | } |
| 5066 | |
Willy Tarreau | 8424004 | 2022-02-28 16:51:23 +0100 | [diff] [blame] | 5067 | c_rew(req, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5068 | count -= vlen; |
| 5069 | if (len > 0) |
| 5070 | len -= vlen; |
| 5071 | if (sz == vlen) |
| 5072 | blk = htx_remove_blk(htx, blk); |
| 5073 | else { |
| 5074 | htx_cut_data_blk(htx, blk, vlen); |
| 5075 | break; |
| 5076 | } |
| 5077 | } |
| 5078 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5079 | /* The message was fully consumed and no more data are expected |
| 5080 | * (EOM flag set). |
| 5081 | */ |
| 5082 | if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) |
| 5083 | len = 0; |
| 5084 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5085 | htx_to_buf(htx, &req->buf); |
| 5086 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5087 | /* If we are no other data available, yield waiting for new data. */ |
| 5088 | if (len) { |
| 5089 | if (len > 0) { |
| 5090 | lua_pushinteger(L, len); |
| 5091 | lua_replace(L, 2); |
| 5092 | } |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 5093 | cs_cant_get(cs); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5094 | 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] | 5095 | } |
| 5096 | |
| 5097 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5098 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5099 | return 1; |
| 5100 | } |
| 5101 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5102 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5103 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 5104 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5105 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5106 | int len = -1; |
| 5107 | |
| 5108 | /* Check arguments. */ |
| 5109 | if (lua_gettop(L) > 2) |
| 5110 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 5111 | if (lua_gettop(L) >= 2) { |
| 5112 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5113 | lua_pop(L, 1); |
| 5114 | } |
| 5115 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5116 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5117 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5118 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5119 | luaL_buffinit(L, &luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5120 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5121 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5122 | } |
| 5123 | |
| 5124 | /* Append data in the output side of the buffer. This data is immediately |
| 5125 | * sent. The function returns the amount of data written. If the buffer |
| 5126 | * cannot contain the data, the function yields. The function returns -1 |
| 5127 | * if the channel is closed. |
| 5128 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5129 | __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] | 5130 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5131 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 5132 | struct conn_stream *cs = luactx->appctx->owner; |
| 5133 | struct channel *res = cs_ic(cs); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5134 | struct htx *htx = htx_from_buf(&res->buf); |
| 5135 | const char *data; |
| 5136 | size_t len; |
| 5137 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 5138 | int max; |
| 5139 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 5140 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5141 | if (!max) |
| 5142 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5143 | |
| 5144 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5145 | |
| 5146 | /* Get the max amount of data which can write as input in the channel. */ |
| 5147 | if (max > (len - l)) |
| 5148 | max = len - l; |
| 5149 | |
| 5150 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 5151 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5152 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5153 | |
| 5154 | /* update counters. */ |
| 5155 | l += max; |
| 5156 | lua_pop(L, 1); |
| 5157 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5158 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5159 | /* If some data is not send, declares the situation to the |
| 5160 | * applet, and returns a yield. |
| 5161 | */ |
| 5162 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5163 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5164 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 5165 | cs_rx_room_blk(cs); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5166 | 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] | 5167 | } |
| 5168 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5169 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5170 | return 1; |
| 5171 | } |
| 5172 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5173 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5174 | * yield the LUA process, and resume it without checking the |
| 5175 | * input arguments. |
| 5176 | */ |
| 5177 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 5178 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5179 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5180 | |
| 5181 | /* We want to send some data. Headers must be sent. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5182 | if (!(luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5183 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 5184 | WILL_LJMP(lua_error(L)); |
| 5185 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5186 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5187 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5188 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5189 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5190 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5191 | } |
| 5192 | |
| 5193 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 5194 | { |
| 5195 | const char *name; |
| 5196 | int ret; |
| 5197 | |
| 5198 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5199 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5200 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 5201 | |
| 5202 | /* Push in the stack the "response" entry. */ |
| 5203 | ret = lua_getfield(L, 1, "response"); |
| 5204 | if (ret != LUA_TTABLE) { |
| 5205 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 5206 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 5207 | WILL_LJMP(lua_error(L)); |
| 5208 | } |
| 5209 | |
| 5210 | /* check if the header is already registered if it is not |
| 5211 | * the case, register it. |
| 5212 | */ |
| 5213 | ret = lua_getfield(L, -1, name); |
| 5214 | if (ret == LUA_TNIL) { |
| 5215 | |
| 5216 | /* Entry not found. */ |
| 5217 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 5218 | |
| 5219 | /* Insert the new header name in the array in the top of the stack. |
| 5220 | * It left the new array in the top of the stack. |
| 5221 | */ |
| 5222 | lua_newtable(L); |
| 5223 | lua_pushvalue(L, 2); |
| 5224 | lua_pushvalue(L, -2); |
| 5225 | lua_settable(L, -4); |
| 5226 | |
| 5227 | } else if (ret != LUA_TTABLE) { |
| 5228 | |
| 5229 | /* corruption error. */ |
| 5230 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 5231 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 5232 | WILL_LJMP(lua_error(L)); |
| 5233 | } |
| 5234 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5235 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5236 | * the header value as new entry. |
| 5237 | */ |
| 5238 | lua_pushvalue(L, 3); |
| 5239 | ret = lua_rawlen(L, -2); |
| 5240 | lua_rawseti(L, -2, ret + 1); |
| 5241 | lua_pushboolean(L, 1); |
| 5242 | return 1; |
| 5243 | } |
| 5244 | |
| 5245 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 5246 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5247 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5248 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5249 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5250 | |
| 5251 | if (status < 100 || status > 599) { |
| 5252 | lua_pushboolean(L, 0); |
| 5253 | return 1; |
| 5254 | } |
| 5255 | |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5256 | luactx->appctx->ctx.hlua_apphttp.status = status; |
| 5257 | luactx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5258 | lua_pushboolean(L, 1); |
| 5259 | return 1; |
| 5260 | } |
| 5261 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5262 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5263 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5264 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5265 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 5266 | struct conn_stream *cs = luactx->appctx->owner; |
| 5267 | struct channel *res = cs_ic(cs); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5268 | struct htx *htx; |
| 5269 | struct htx_sl *sl; |
| 5270 | struct h1m h1m; |
| 5271 | const char *status, *reason; |
| 5272 | const char *name, *value; |
| 5273 | size_t nlen, vlen; |
| 5274 | unsigned int flags; |
| 5275 | |
| 5276 | /* Send the message at once. */ |
| 5277 | htx = htx_from_buf(&res->buf); |
| 5278 | h1m_init_res(&h1m); |
| 5279 | |
| 5280 | /* Use the same http version than the request. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5281 | status = ultoa_r(luactx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 5282 | reason = luactx->appctx->ctx.hlua_apphttp.reason; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5283 | if (reason == NULL) |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5284 | reason = http_get_reason(luactx->appctx->ctx.hlua_apphttp.status); |
| 5285 | if (luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5286 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5287 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 5288 | } |
| 5289 | else { |
| 5290 | flags = HTX_SL_F_IS_RESP; |
| 5291 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 5292 | } |
| 5293 | if (!sl) { |
| 5294 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5295 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5296 | WILL_LJMP(lua_error(L)); |
| 5297 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5298 | sl->info.res.status = luactx->appctx->ctx.hlua_apphttp.status; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5299 | |
| 5300 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 5301 | lua_pushvalue(L, 0); |
| 5302 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 5303 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5304 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5305 | WILL_LJMP(lua_error(L)); |
| 5306 | } |
| 5307 | |
| 5308 | /* Browse the list of headers. */ |
| 5309 | lua_pushnil(L); |
| 5310 | while(lua_next(L, -2) != 0) { |
| 5311 | /* We expect a string as -2. */ |
| 5312 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 5313 | 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] | 5314 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5315 | lua_typename(L, lua_type(L, -2))); |
| 5316 | WILL_LJMP(lua_error(L)); |
| 5317 | } |
| 5318 | name = lua_tolstring(L, -2, &nlen); |
| 5319 | |
| 5320 | /* We expect an array as -1. */ |
| 5321 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 5322 | 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] | 5323 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5324 | name, |
| 5325 | lua_typename(L, lua_type(L, -1))); |
| 5326 | WILL_LJMP(lua_error(L)); |
| 5327 | } |
| 5328 | |
| 5329 | /* Browse the table who is on the top of the stack. */ |
| 5330 | lua_pushnil(L); |
| 5331 | while(lua_next(L, -2) != 0) { |
| 5332 | int id; |
| 5333 | |
| 5334 | /* We expect a number as -2. */ |
| 5335 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 5336 | 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] | 5337 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5338 | name, |
| 5339 | lua_typename(L, lua_type(L, -2))); |
| 5340 | WILL_LJMP(lua_error(L)); |
| 5341 | } |
| 5342 | id = lua_tointeger(L, -2); |
| 5343 | |
| 5344 | /* We expect a string as -2. */ |
| 5345 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 5346 | 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] | 5347 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5348 | name, id, |
| 5349 | lua_typename(L, lua_type(L, -1))); |
| 5350 | WILL_LJMP(lua_error(L)); |
| 5351 | } |
| 5352 | value = lua_tolstring(L, -1, &vlen); |
| 5353 | |
| 5354 | /* Simple Protocol checks. */ |
Christopher Faulet | 545fbba | 2021-09-28 09:36:25 +0200 | [diff] [blame] | 5355 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) { |
| 5356 | int ret; |
| 5357 | |
| 5358 | ret = h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
| 5359 | if (ret < 0) { |
| 5360 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 5361 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
| 5362 | name); |
| 5363 | WILL_LJMP(lua_error(L)); |
| 5364 | } |
| 5365 | else if (ret == 0) |
| 5366 | goto next; /* Skip it */ |
| 5367 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5368 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 5369 | struct ist v = ist2(value, vlen); |
| 5370 | int ret; |
| 5371 | |
| 5372 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 5373 | if (ret < 0) { |
| 5374 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5375 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5376 | name); |
| 5377 | WILL_LJMP(lua_error(L)); |
| 5378 | } |
| 5379 | else if (ret == 0) |
| 5380 | goto next; /* Skip it */ |
| 5381 | } |
| 5382 | |
| 5383 | /* Add a new header */ |
| 5384 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 5385 | 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] | 5386 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5387 | name); |
| 5388 | WILL_LJMP(lua_error(L)); |
| 5389 | } |
| 5390 | next: |
| 5391 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5392 | lua_pop(L, 1); |
| 5393 | } |
| 5394 | |
| 5395 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5396 | lua_pop(L, 1); |
| 5397 | } |
| 5398 | |
| 5399 | if (h1m.flags & H1_MF_CHNK) |
| 5400 | h1m.flags &= ~H1_MF_CLEN; |
| 5401 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5402 | h1m.flags |= H1_MF_XFER_LEN; |
| 5403 | |
| 5404 | /* Uset HTX start-line flags */ |
| 5405 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5406 | flags |= HTX_SL_F_XFER_ENC; |
| 5407 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5408 | flags |= HTX_SL_F_XFER_LEN; |
| 5409 | if (h1m.flags & H1_MF_CHNK) |
| 5410 | flags |= HTX_SL_F_CHNK; |
| 5411 | else if (h1m.flags & H1_MF_CLEN) |
| 5412 | flags |= HTX_SL_F_CLEN; |
| 5413 | if (h1m.body_len == 0) |
| 5414 | flags |= HTX_SL_F_BODYLESS; |
| 5415 | } |
| 5416 | sl->flags |= flags; |
| 5417 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5418 | /* 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] | 5419 | * and the status code implies the presence of a message body, we must |
| 5420 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5421 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 5422 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5423 | */ |
| 5424 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5425 | luactx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 5426 | luactx->appctx->ctx.hlua_apphttp.status != 204 && |
| 5427 | luactx->appctx->ctx.hlua_apphttp.status != 304) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5428 | /* Add a new header */ |
| 5429 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 5430 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 5431 | 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] | 5432 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5433 | WILL_LJMP(lua_error(L)); |
| 5434 | } |
| 5435 | } |
| 5436 | |
| 5437 | /* Finalize headers. */ |
| 5438 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 5439 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5440 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5441 | WILL_LJMP(lua_error(L)); |
| 5442 | } |
| 5443 | |
| 5444 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 5445 | b_reset(&res->buf); |
| 5446 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 5447 | WILL_LJMP(lua_error(L)); |
| 5448 | } |
| 5449 | |
| 5450 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5451 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5452 | |
| 5453 | /* Headers sent, set the flag. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5454 | luactx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5455 | return 0; |
| 5456 | |
| 5457 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5458 | /* We will build the status line and the headers of the HTTP response. |
| 5459 | * We will try send at once if its not possible, we give back the hand |
| 5460 | * waiting for more room. |
| 5461 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5462 | __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] | 5463 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5464 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 5465 | struct conn_stream *cs = luactx->appctx->owner; |
| 5466 | struct channel *res = cs_ic(cs); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5467 | |
| 5468 | if (co_data(res)) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 5469 | cs_rx_room_blk(cs); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5470 | 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] | 5471 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5472 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5473 | } |
| 5474 | |
| 5475 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5476 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5477 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5478 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5479 | } |
| 5480 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5481 | /* |
| 5482 | * |
| 5483 | * |
| 5484 | * Class HTTP |
| 5485 | * |
| 5486 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5487 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5488 | |
| 5489 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5490 | * a class stream, otherwise it throws an error. |
| 5491 | */ |
| 5492 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5493 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5494 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 5495 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5496 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5497 | /* This function creates and push in the stack a HTTP object |
| 5498 | * according with a current TXN. |
| 5499 | */ |
| 5500 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5501 | { |
| 5502 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5503 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5504 | /* Check stack size. */ |
| 5505 | if (!lua_checkstack(L, 3)) |
| 5506 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5507 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5508 | /* Create the object: obj[0] = userdata. |
| 5509 | * Note that the base of the Converters object is the |
| 5510 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5511 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5512 | lua_newtable(L); |
| 5513 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5514 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5515 | |
| 5516 | htxn->s = txn->s; |
| 5517 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 5518 | htxn->dir = txn->dir; |
| 5519 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5520 | |
| 5521 | /* Pop a class stream metatable and affect it to the table. */ |
| 5522 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5523 | lua_setmetatable(L, -2); |
| 5524 | |
| 5525 | return 1; |
| 5526 | } |
| 5527 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5528 | /* This function creates and returns an array containing the status-line |
| 5529 | * elements. This function does not fails. |
| 5530 | */ |
| 5531 | __LJMP static int hlua_http_get_stline(lua_State *L, struct htx_sl *sl) |
| 5532 | { |
| 5533 | /* Create the table. */ |
| 5534 | lua_newtable(L); |
| 5535 | |
| 5536 | if (sl->flags & HTX_SL_F_IS_RESP) { |
| 5537 | lua_pushstring(L, "version"); |
| 5538 | lua_pushlstring(L, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); |
| 5539 | lua_settable(L, -3); |
| 5540 | lua_pushstring(L, "code"); |
| 5541 | lua_pushlstring(L, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); |
| 5542 | lua_settable(L, -3); |
| 5543 | lua_pushstring(L, "reason"); |
| 5544 | lua_pushlstring(L, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); |
| 5545 | lua_settable(L, -3); |
| 5546 | } |
| 5547 | else { |
| 5548 | lua_pushstring(L, "method"); |
| 5549 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 5550 | lua_settable(L, -3); |
| 5551 | lua_pushstring(L, "uri"); |
| 5552 | lua_pushlstring(L, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); |
| 5553 | lua_settable(L, -3); |
| 5554 | lua_pushstring(L, "version"); |
| 5555 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 5556 | lua_settable(L, -3); |
| 5557 | } |
| 5558 | return 1; |
| 5559 | } |
| 5560 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5561 | /* This function creates ans returns an array of HTTP headers. |
| 5562 | * This function does not fails. It is used as wrapper with the |
| 5563 | * 2 following functions. |
| 5564 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5565 | __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] | 5566 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5567 | struct htx *htx; |
| 5568 | int32_t pos; |
| 5569 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5570 | /* Create the table. */ |
| 5571 | lua_newtable(L); |
| 5572 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5573 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5574 | htx = htxbuf(&msg->chn->buf); |
| 5575 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5576 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5577 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5578 | struct ist n, v; |
| 5579 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5580 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5581 | if (type == HTX_BLK_HDR) { |
| 5582 | n = htx_get_blk_name(htx,blk); |
| 5583 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5584 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5585 | else if (type == HTX_BLK_EOH) |
| 5586 | break; |
| 5587 | else |
| 5588 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5589 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5590 | /* Check for existing entry: |
| 5591 | * assume that the table is on the top of the stack, and |
| 5592 | * push the key in the stack, the function lua_gettable() |
| 5593 | * perform the lookup. |
| 5594 | */ |
| 5595 | lua_pushlstring(L, n.ptr, n.len); |
| 5596 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5597 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5598 | switch (lua_type(L, -1)) { |
| 5599 | case LUA_TNIL: |
| 5600 | /* Table not found, create it. */ |
| 5601 | lua_pop(L, 1); /* remove the nil value. */ |
| 5602 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5603 | lua_newtable(L); /* create and push empty table. */ |
| 5604 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5605 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5606 | 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] | 5607 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5608 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5609 | case LUA_TTABLE: |
| 5610 | /* Entry found: push the value in the table. */ |
| 5611 | len = lua_rawlen(L, -1); |
| 5612 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5613 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5614 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5615 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5616 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5617 | default: |
| 5618 | /* Other cases are errors. */ |
| 5619 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5620 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5621 | } |
| 5622 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5623 | return 1; |
| 5624 | } |
| 5625 | |
| 5626 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5627 | { |
| 5628 | struct hlua_txn *htxn; |
| 5629 | |
| 5630 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5631 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5632 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5633 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5634 | WILL_LJMP(lua_error(L)); |
| 5635 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5636 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5637 | } |
| 5638 | |
| 5639 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5640 | { |
| 5641 | struct hlua_txn *htxn; |
| 5642 | |
| 5643 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5644 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5645 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5646 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5647 | WILL_LJMP(lua_error(L)); |
| 5648 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5649 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5650 | } |
| 5651 | |
| 5652 | /* This function replace full header, or just a value in |
| 5653 | * the request or in the response. It is a wrapper fir the |
| 5654 | * 4 following functions. |
| 5655 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5656 | __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] | 5657 | { |
| 5658 | size_t name_len; |
| 5659 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5660 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5661 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5662 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5663 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5664 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5665 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5666 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5667 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5668 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5669 | 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] | 5670 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5671 | return 0; |
| 5672 | } |
| 5673 | |
| 5674 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5675 | { |
| 5676 | struct hlua_txn *htxn; |
| 5677 | |
| 5678 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5679 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5680 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5681 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5682 | WILL_LJMP(lua_error(L)); |
| 5683 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5684 | 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] | 5685 | } |
| 5686 | |
| 5687 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5688 | { |
| 5689 | struct hlua_txn *htxn; |
| 5690 | |
| 5691 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5692 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5693 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5694 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5695 | WILL_LJMP(lua_error(L)); |
| 5696 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5697 | 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] | 5698 | } |
| 5699 | |
| 5700 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5701 | { |
| 5702 | struct hlua_txn *htxn; |
| 5703 | |
| 5704 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5705 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5706 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5707 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5708 | WILL_LJMP(lua_error(L)); |
| 5709 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5710 | 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] | 5711 | } |
| 5712 | |
| 5713 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5714 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5715 | struct hlua_txn *htxn; |
| 5716 | |
| 5717 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5718 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5719 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5720 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5721 | WILL_LJMP(lua_error(L)); |
| 5722 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5723 | 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] | 5724 | } |
| 5725 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5726 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5727 | * It is a wrapper for the 2 following functions. |
| 5728 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5729 | __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] | 5730 | { |
| 5731 | size_t len; |
| 5732 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5733 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5734 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5735 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5736 | ctx.blk = NULL; |
| 5737 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5738 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5739 | return 0; |
| 5740 | } |
| 5741 | |
| 5742 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5743 | { |
| 5744 | struct hlua_txn *htxn; |
| 5745 | |
| 5746 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5747 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5748 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5749 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5750 | WILL_LJMP(lua_error(L)); |
| 5751 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5752 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5753 | } |
| 5754 | |
| 5755 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5756 | { |
| 5757 | struct hlua_txn *htxn; |
| 5758 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5759 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5760 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5761 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5762 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5763 | WILL_LJMP(lua_error(L)); |
| 5764 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5765 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5766 | } |
| 5767 | |
| 5768 | /* This function adds an header. It is a wrapper used by |
| 5769 | * the 2 following functions. |
| 5770 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5771 | __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] | 5772 | { |
| 5773 | size_t name_len; |
| 5774 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5775 | size_t value_len; |
| 5776 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5777 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5778 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5779 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5780 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5781 | return 0; |
| 5782 | } |
| 5783 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5784 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5785 | { |
| 5786 | struct hlua_txn *htxn; |
| 5787 | |
| 5788 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5789 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5790 | |
| 5791 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5792 | WILL_LJMP(lua_error(L)); |
| 5793 | |
| 5794 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 5795 | } |
| 5796 | |
| 5797 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5798 | { |
| 5799 | struct hlua_txn *htxn; |
| 5800 | |
| 5801 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5802 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5803 | |
| 5804 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5805 | WILL_LJMP(lua_error(L)); |
| 5806 | |
| 5807 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 5808 | } |
| 5809 | |
| 5810 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5811 | { |
| 5812 | struct hlua_txn *htxn; |
| 5813 | |
| 5814 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5815 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5816 | |
| 5817 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5818 | WILL_LJMP(lua_error(L)); |
| 5819 | |
| 5820 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5821 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 5822 | } |
| 5823 | |
| 5824 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5825 | { |
| 5826 | struct hlua_txn *htxn; |
| 5827 | |
| 5828 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5829 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5830 | |
| 5831 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5832 | WILL_LJMP(lua_error(L)); |
| 5833 | |
| 5834 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5835 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 5836 | } |
| 5837 | |
| 5838 | /* This function set the method. */ |
| 5839 | static int hlua_http_req_set_meth(lua_State *L) |
| 5840 | { |
| 5841 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5842 | size_t name_len; |
| 5843 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5844 | |
| 5845 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5846 | WILL_LJMP(lua_error(L)); |
| 5847 | |
| 5848 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1); |
| 5849 | return 1; |
| 5850 | } |
| 5851 | |
| 5852 | /* This function set the method. */ |
| 5853 | static int hlua_http_req_set_path(lua_State *L) |
| 5854 | { |
| 5855 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5856 | size_t name_len; |
| 5857 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5858 | |
| 5859 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5860 | WILL_LJMP(lua_error(L)); |
| 5861 | |
| 5862 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1); |
| 5863 | return 1; |
| 5864 | } |
| 5865 | |
| 5866 | /* This function set the query-string. */ |
| 5867 | static int hlua_http_req_set_query(lua_State *L) |
| 5868 | { |
| 5869 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5870 | size_t name_len; |
| 5871 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5872 | |
| 5873 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5874 | WILL_LJMP(lua_error(L)); |
| 5875 | |
| 5876 | /* Check length. */ |
| 5877 | if (name_len > trash.size - 1) { |
| 5878 | lua_pushboolean(L, 0); |
| 5879 | return 1; |
| 5880 | } |
| 5881 | |
| 5882 | /* Add the mark question as prefix. */ |
| 5883 | chunk_reset(&trash); |
| 5884 | trash.area[trash.data++] = '?'; |
| 5885 | memcpy(trash.area + trash.data, name, name_len); |
| 5886 | trash.data += name_len; |
| 5887 | |
| 5888 | lua_pushboolean(L, |
| 5889 | http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1); |
| 5890 | return 1; |
| 5891 | } |
| 5892 | |
| 5893 | /* This function set the uri. */ |
| 5894 | static int hlua_http_req_set_uri(lua_State *L) |
| 5895 | { |
| 5896 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5897 | size_t name_len; |
| 5898 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5899 | |
| 5900 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5901 | WILL_LJMP(lua_error(L)); |
| 5902 | |
| 5903 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1); |
| 5904 | return 1; |
| 5905 | } |
| 5906 | |
| 5907 | /* This function set the response code & optionally reason. */ |
| 5908 | static int hlua_http_res_set_status(lua_State *L) |
| 5909 | { |
| 5910 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5911 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5912 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5913 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
| 5914 | |
| 5915 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5916 | WILL_LJMP(lua_error(L)); |
| 5917 | |
| 5918 | http_res_set_status(code, reason, htxn->s); |
| 5919 | return 0; |
| 5920 | } |
| 5921 | |
| 5922 | /* |
| 5923 | * |
| 5924 | * |
| 5925 | * Class HTTPMessage |
| 5926 | * |
| 5927 | * |
| 5928 | */ |
| 5929 | |
| 5930 | /* Returns a struct http_msg if the stack entry "ud" is a class HTTPMessage, |
| 5931 | * otherwise it throws an error. |
| 5932 | */ |
| 5933 | __LJMP static struct http_msg *hlua_checkhttpmsg(lua_State *L, int ud) |
| 5934 | { |
| 5935 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_msg_ref)); |
| 5936 | } |
| 5937 | |
| 5938 | /* Creates and pushes on the stack a HTTP object according with a current TXN. |
| 5939 | */ |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 5940 | 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] | 5941 | { |
| 5942 | /* Check stack size. */ |
| 5943 | if (!lua_checkstack(L, 3)) |
| 5944 | return 0; |
| 5945 | |
| 5946 | lua_newtable(L); |
| 5947 | lua_pushlightuserdata(L, msg); |
| 5948 | lua_rawseti(L, -2, 0); |
| 5949 | |
| 5950 | /* Create the "channel" field that contains the request channel object. */ |
| 5951 | lua_pushstring(L, "channel"); |
| 5952 | if (!hlua_channel_new(L, msg->chn)) |
| 5953 | return 0; |
| 5954 | lua_rawset(L, -3); |
| 5955 | |
| 5956 | /* Pop a class stream metatable and affect it to the table. */ |
| 5957 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_msg_ref); |
| 5958 | lua_setmetatable(L, -2); |
| 5959 | |
| 5960 | return 1; |
| 5961 | } |
| 5962 | |
| 5963 | /* Helper function returning a filter attached to the HTTP message at the |
| 5964 | * 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] | 5965 | * 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] | 5966 | * filled with output and input length respectively. |
| 5967 | */ |
| 5968 | static struct filter *hlua_http_msg_filter(lua_State *L, int ud, struct http_msg *msg, size_t *offset, size_t *len) |
| 5969 | { |
| 5970 | struct channel *chn = msg->chn; |
| 5971 | struct htx *htx = htxbuf(&chn->buf); |
| 5972 | struct filter *filter = NULL; |
| 5973 | |
| 5974 | *offset = co_data(msg->chn); |
| 5975 | *len = htx->data - co_data(msg->chn); |
| 5976 | |
| 5977 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 5978 | filter = lua_touserdata (L, -1); |
| 5979 | if (msg->msg_state >= HTTP_MSG_DATA) { |
| 5980 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 5981 | |
| 5982 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 5983 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 5984 | } |
| 5985 | } |
| 5986 | |
| 5987 | lua_pop(L, 1); |
| 5988 | return filter; |
| 5989 | } |
| 5990 | |
| 5991 | /* Returns true if the channel attached to the HTTP message is the response |
| 5992 | * channel. |
| 5993 | */ |
| 5994 | __LJMP static int hlua_http_msg_is_resp(lua_State *L) |
| 5995 | { |
| 5996 | struct http_msg *msg; |
| 5997 | |
| 5998 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 5999 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6000 | |
| 6001 | lua_pushboolean(L, !!(msg->chn->flags & CF_ISRESP)); |
| 6002 | return 1; |
| 6003 | } |
| 6004 | |
| 6005 | /* Returns an array containing the elements status-line of the HTTP message. It relies |
| 6006 | * on hlua_http_get_stline(). |
| 6007 | */ |
| 6008 | __LJMP static int hlua_http_msg_get_stline(lua_State *L) |
| 6009 | { |
| 6010 | struct http_msg *msg; |
| 6011 | struct htx *htx; |
| 6012 | struct htx_sl *sl; |
| 6013 | |
| 6014 | MAY_LJMP(check_args(L, 1, "get_stline")); |
| 6015 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6016 | |
| 6017 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6018 | WILL_LJMP(lua_error(L)); |
| 6019 | |
| 6020 | htx = htxbuf(&msg->chn->buf); |
| 6021 | sl = http_get_stline(htx); |
| 6022 | if (!sl) |
| 6023 | return 0; |
| 6024 | return hlua_http_get_stline(L, sl); |
| 6025 | } |
| 6026 | |
| 6027 | /* Returns an array containing all headers of the HTTP message. it relies on |
| 6028 | * hlua_http_get_headers(). |
| 6029 | */ |
| 6030 | __LJMP static int hlua_http_msg_get_headers(lua_State *L) |
| 6031 | { |
| 6032 | struct http_msg *msg; |
| 6033 | |
| 6034 | MAY_LJMP(check_args(L, 1, "get_headers")); |
| 6035 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6036 | |
| 6037 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6038 | WILL_LJMP(lua_error(L)); |
| 6039 | |
| 6040 | return hlua_http_get_headers(L, msg); |
| 6041 | } |
| 6042 | |
| 6043 | /* Deletes all occurrences of an header in the HTTP message matching on its |
| 6044 | * name. It relies on hlua_http_del_hdr(). |
| 6045 | */ |
| 6046 | __LJMP static int hlua_http_msg_del_hdr(lua_State *L) |
| 6047 | { |
| 6048 | struct http_msg *msg; |
| 6049 | |
| 6050 | MAY_LJMP(check_args(L, 2, "del_header")); |
| 6051 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6052 | |
| 6053 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6054 | WILL_LJMP(lua_error(L)); |
| 6055 | |
| 6056 | return hlua_http_del_hdr(L, msg); |
| 6057 | } |
| 6058 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6059 | /* 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] | 6060 | * message given its name against a regex and replaces it if it matches. It |
| 6061 | * relies on hlua_http_rep_hdr(). |
| 6062 | */ |
| 6063 | __LJMP static int hlua_http_msg_rep_hdr(lua_State *L) |
| 6064 | { |
| 6065 | struct http_msg *msg; |
| 6066 | |
| 6067 | MAY_LJMP(check_args(L, 4, "rep_header")); |
| 6068 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6069 | |
| 6070 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6071 | WILL_LJMP(lua_error(L)); |
| 6072 | |
| 6073 | return hlua_http_rep_hdr(L, msg, 1); |
| 6074 | } |
| 6075 | |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 6076 | /* 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] | 6077 | * message given its name against a regex and replaces it if it matches. It |
| 6078 | * relies on hlua_http_rep_hdr(). |
| 6079 | */ |
| 6080 | __LJMP static int hlua_http_msg_rep_val(lua_State *L) |
| 6081 | { |
| 6082 | struct http_msg *msg; |
| 6083 | |
| 6084 | MAY_LJMP(check_args(L, 4, "rep_value")); |
| 6085 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6086 | |
| 6087 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6088 | WILL_LJMP(lua_error(L)); |
| 6089 | |
| 6090 | return hlua_http_rep_hdr(L, msg, 0); |
| 6091 | } |
| 6092 | |
| 6093 | /* Add an header in the HTTP message. It relies on hlua_http_add_hdr() */ |
| 6094 | __LJMP static int hlua_http_msg_add_hdr(lua_State *L) |
| 6095 | { |
| 6096 | struct http_msg *msg; |
| 6097 | |
| 6098 | MAY_LJMP(check_args(L, 3, "add_header")); |
| 6099 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6100 | |
| 6101 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6102 | WILL_LJMP(lua_error(L)); |
| 6103 | |
| 6104 | return hlua_http_add_hdr(L, msg); |
| 6105 | } |
| 6106 | |
| 6107 | /* Add an header in the HTTP message removing existing headers with the same |
| 6108 | * name. It relies on hlua_http_del_hdr() and hlua_http_add_hdr(). |
| 6109 | */ |
| 6110 | __LJMP static int hlua_http_msg_set_hdr(lua_State *L) |
| 6111 | { |
| 6112 | struct http_msg *msg; |
| 6113 | |
| 6114 | MAY_LJMP(check_args(L, 3, "set_header")); |
| 6115 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6116 | |
| 6117 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6118 | WILL_LJMP(lua_error(L)); |
| 6119 | |
| 6120 | hlua_http_del_hdr(L, msg); |
| 6121 | return hlua_http_add_hdr(L, msg); |
| 6122 | } |
| 6123 | |
| 6124 | /* Rewrites the request method. It relies on http_req_replace_stline(). */ |
| 6125 | __LJMP static int hlua_http_msg_set_meth(lua_State *L) |
| 6126 | { |
| 6127 | struct stream *s; |
| 6128 | struct http_msg *msg; |
| 6129 | const char *name; |
| 6130 | size_t name_len; |
| 6131 | |
| 6132 | MAY_LJMP(check_args(L, 2, "set_method")); |
| 6133 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6134 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6135 | |
| 6136 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6137 | WILL_LJMP(lua_error(L)); |
| 6138 | |
| 6139 | s = chn_strm(msg->chn); |
| 6140 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, s->be, s) != -1); |
| 6141 | return 1; |
| 6142 | } |
| 6143 | |
| 6144 | /* Rewrites the request path. It relies on http_req_replace_stline(). */ |
| 6145 | __LJMP static int hlua_http_msg_set_path(lua_State *L) |
| 6146 | { |
| 6147 | struct stream *s; |
| 6148 | struct http_msg *msg; |
| 6149 | const char *name; |
| 6150 | size_t name_len; |
| 6151 | |
| 6152 | MAY_LJMP(check_args(L, 2, "set_path")); |
| 6153 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6154 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6155 | |
| 6156 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6157 | WILL_LJMP(lua_error(L)); |
| 6158 | |
| 6159 | s = chn_strm(msg->chn); |
| 6160 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, s->be, s) != -1); |
| 6161 | return 1; |
| 6162 | } |
| 6163 | |
| 6164 | /* Rewrites the request query-string. It relies on http_req_replace_stline(). */ |
| 6165 | __LJMP static int hlua_http_msg_set_query(lua_State *L) |
| 6166 | { |
| 6167 | struct stream *s; |
| 6168 | struct http_msg *msg; |
| 6169 | const char *name; |
| 6170 | size_t name_len; |
| 6171 | |
| 6172 | MAY_LJMP(check_args(L, 2, "set_query")); |
| 6173 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6174 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6175 | |
| 6176 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6177 | WILL_LJMP(lua_error(L)); |
| 6178 | |
| 6179 | /* Check length. */ |
| 6180 | if (name_len > trash.size - 1) { |
| 6181 | lua_pushboolean(L, 0); |
| 6182 | return 1; |
| 6183 | } |
| 6184 | |
| 6185 | /* Add the mark question as prefix. */ |
| 6186 | chunk_reset(&trash); |
| 6187 | trash.area[trash.data++] = '?'; |
| 6188 | memcpy(trash.area + trash.data, name, name_len); |
| 6189 | trash.data += name_len; |
| 6190 | |
| 6191 | s = chn_strm(msg->chn); |
| 6192 | lua_pushboolean(L, http_req_replace_stline(2, trash.area, trash.data, s->be, s) != -1); |
| 6193 | return 1; |
| 6194 | } |
| 6195 | |
| 6196 | /* Rewrites the request URI. It relies on http_req_replace_stline(). */ |
| 6197 | __LJMP static int hlua_http_msg_set_uri(lua_State *L) |
| 6198 | { |
| 6199 | struct stream *s; |
| 6200 | struct http_msg *msg; |
| 6201 | const char *name; |
| 6202 | size_t name_len; |
| 6203 | |
| 6204 | MAY_LJMP(check_args(L, 2, "set_uri")); |
| 6205 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6206 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6207 | |
| 6208 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6209 | WILL_LJMP(lua_error(L)); |
| 6210 | |
| 6211 | s = chn_strm(msg->chn); |
| 6212 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, s->be, s) != -1); |
| 6213 | return 1; |
| 6214 | } |
| 6215 | |
| 6216 | /* Rewrites the response status code. It relies on http_res_set_status(). */ |
| 6217 | __LJMP static int hlua_http_msg_set_status(lua_State *L) |
| 6218 | { |
| 6219 | struct http_msg *msg; |
| 6220 | unsigned int code; |
| 6221 | const char *reason; |
| 6222 | size_t reason_len; |
| 6223 | |
| 6224 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6225 | code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6226 | reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, &reason_len)); |
| 6227 | |
| 6228 | if (!(msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6229 | WILL_LJMP(lua_error(L)); |
| 6230 | |
| 6231 | lua_pushboolean(L, http_res_set_status(code, ist2(reason, reason_len), chn_strm(msg->chn)) != -1); |
| 6232 | return 1; |
| 6233 | } |
| 6234 | |
| 6235 | /* Returns true if the HTTP message is full. */ |
| 6236 | __LJMP static int hlua_http_msg_is_full(lua_State *L) |
| 6237 | { |
| 6238 | struct http_msg *msg; |
| 6239 | |
| 6240 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 6241 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6242 | lua_pushboolean(L, channel_full(msg->chn, 0)); |
| 6243 | return 1; |
| 6244 | } |
| 6245 | |
| 6246 | /* Returns true if the HTTP message may still receive data. */ |
| 6247 | __LJMP static int hlua_http_msg_may_recv(lua_State *L) |
| 6248 | { |
| 6249 | struct http_msg *msg; |
| 6250 | struct htx *htx; |
| 6251 | |
| 6252 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6253 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6254 | htx = htxbuf(&msg->chn->buf); |
| 6255 | lua_pushboolean(L, (htx_expect_more(htx) && !channel_input_closed(msg->chn) && channel_may_recv(msg->chn))); |
| 6256 | return 1; |
| 6257 | } |
| 6258 | |
| 6259 | /* Returns true if the HTTP message EOM was received */ |
| 6260 | __LJMP static int hlua_http_msg_is_eom(lua_State *L) |
| 6261 | { |
| 6262 | struct http_msg *msg; |
| 6263 | struct htx *htx; |
| 6264 | |
| 6265 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6266 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6267 | htx = htxbuf(&msg->chn->buf); |
| 6268 | lua_pushboolean(L, !htx_expect_more(htx)); |
| 6269 | return 1; |
| 6270 | } |
| 6271 | |
| 6272 | /* Returns the number of bytes available in the input side of the HTTP |
| 6273 | * message. This function never fails. |
| 6274 | */ |
| 6275 | __LJMP static int hlua_http_msg_get_in_len(lua_State *L) |
| 6276 | { |
| 6277 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6278 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6279 | |
| 6280 | MAY_LJMP(check_args(L, 1, "input")); |
| 6281 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6282 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6283 | lua_pushinteger(L, input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6284 | return 1; |
| 6285 | } |
| 6286 | |
| 6287 | /* Returns the number of bytes available in the output side of the HTTP |
| 6288 | * message. This function never fails. |
| 6289 | */ |
| 6290 | __LJMP static int hlua_http_msg_get_out_len(lua_State *L) |
| 6291 | { |
| 6292 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6293 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6294 | |
| 6295 | MAY_LJMP(check_args(L, 1, "output")); |
| 6296 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6297 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6298 | lua_pushinteger(L, output); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6299 | return 1; |
| 6300 | } |
| 6301 | |
| 6302 | /* Copies at most <len> bytes of DATA blocks from the HTTP message <msg> |
| 6303 | * 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] | 6304 | * 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] | 6305 | * block. This function is called during the payload filtering, so the headers |
| 6306 | * are already scheduled for output (from the filter point of view). |
| 6307 | */ |
| 6308 | static int _hlua_http_msg_dup(struct http_msg *msg, lua_State *L, size_t offset, size_t len) |
| 6309 | { |
| 6310 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 6311 | struct htx_blk *blk; |
| 6312 | struct htx_ret htxret; |
| 6313 | luaL_Buffer b; |
| 6314 | int ret = 0; |
| 6315 | |
| 6316 | luaL_buffinit(L, &b); |
| 6317 | htxret = htx_find_offset(htx, offset); |
| 6318 | for (blk = htxret.blk, offset = htxret.ret; blk && len; blk = htx_get_next_blk(htx, blk)) { |
| 6319 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6320 | struct ist v; |
| 6321 | |
| 6322 | switch (type) { |
| 6323 | case HTX_BLK_UNUSED: |
| 6324 | break; |
| 6325 | |
| 6326 | case HTX_BLK_DATA: |
| 6327 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6328 | v = istadv(v, offset); |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6329 | v = isttrim(v, len); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6330 | |
| 6331 | luaL_addlstring(&b, v.ptr, v.len); |
| 6332 | ret += v.len; |
| 6333 | break; |
| 6334 | |
| 6335 | default: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6336 | if (!ret) |
| 6337 | goto no_data; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6338 | goto end; |
| 6339 | } |
| 6340 | offset = 0; |
| 6341 | } |
| 6342 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6343 | end: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6344 | if (!ret && (htx->flags & HTX_FL_EOM)) |
| 6345 | goto no_data; |
| 6346 | luaL_pushresult(&b); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6347 | return ret; |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6348 | |
| 6349 | no_data: |
| 6350 | /* Remove the empty string and push nil on the stack */ |
| 6351 | lua_pop(L, 1); |
| 6352 | lua_pushnil(L); |
| 6353 | return 0; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6354 | } |
| 6355 | |
| 6356 | /* Copies the string <str> to the HTTP message <msg> at the offset |
| 6357 | * <offset>. This function returns -1 if data cannot be copied. Otherwise, it |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6358 | * returns the amount of data written. This function is responsible to update |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6359 | * the filter context. |
| 6360 | */ |
| 6361 | static int _hlua_http_msg_insert(struct http_msg *msg, struct filter *filter, struct ist str, size_t offset) |
| 6362 | { |
| 6363 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6364 | struct htx_ret htxret; |
| 6365 | int /*max, */ret = 0; |
| 6366 | |
| 6367 | /* Nothing to do, just return */ |
| 6368 | if (unlikely(istlen(str) == 0)) |
| 6369 | goto end; |
| 6370 | |
| 6371 | if (istlen(str) > htx_free_data_space(htx)) { |
| 6372 | ret = -1; |
| 6373 | goto end; |
| 6374 | } |
| 6375 | |
| 6376 | htxret = htx_find_offset(htx, offset); |
| 6377 | if (!htxret.blk || htx_get_blk_type(htxret.blk) != HTX_BLK_DATA) { |
| 6378 | if (!htx_add_last_data(htx, str)) |
| 6379 | goto end; |
| 6380 | } |
| 6381 | else { |
| 6382 | struct ist v = htx_get_blk_value(htx, htxret.blk); |
| 6383 | v.ptr += htxret.ret; |
| 6384 | v.len = 0; |
| 6385 | if (!htx_replace_blk_value(htx, htxret.blk, v, str)) |
| 6386 | goto end; |
| 6387 | } |
| 6388 | ret = str.len; |
| 6389 | if (ret) { |
| 6390 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6391 | flt_update_offsets(filter, msg->chn, ret); |
| 6392 | flt_ctx->cur_len[CHN_IDX(msg->chn)] += ret; |
| 6393 | } |
| 6394 | |
| 6395 | end: |
| 6396 | htx_to_buf(htx, &msg->chn->buf); |
| 6397 | return ret; |
| 6398 | } |
| 6399 | |
| 6400 | /* Helper function removing at most <len> bytes of DATA blocks at the absolute |
| 6401 | * position <offset>. It stops on the first non-DATA HTX block. This function is |
| 6402 | * called during the payload filtering, so the headers are already scheduled for |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6403 | * output (from the filter point of view). This function is responsible to |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6404 | * update the filter context. |
| 6405 | */ |
| 6406 | static void _hlua_http_msg_delete(struct http_msg *msg, struct filter *filter, size_t offset, size_t len) |
| 6407 | { |
| 6408 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6409 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6410 | struct htx_blk *blk; |
| 6411 | struct htx_ret htxret; |
| 6412 | size_t ret = 0; |
| 6413 | |
| 6414 | /* Be sure <len> is always the amount of DATA to remove */ |
| 6415 | if (htx->data == offset+len && htx_get_tail_type(htx) == HTX_BLK_DATA) { |
| 6416 | htx_truncate(htx, offset); |
| 6417 | ret = len; |
| 6418 | goto end; |
| 6419 | } |
| 6420 | |
| 6421 | htxret = htx_find_offset(htx, offset); |
| 6422 | blk = htxret.blk; |
| 6423 | if (htxret.ret) { |
| 6424 | struct ist v; |
| 6425 | |
| 6426 | if (htx_get_blk_type(blk) != HTX_BLK_DATA) |
| 6427 | goto end; |
| 6428 | v = htx_get_blk_value(htx, blk); |
| 6429 | v.ptr += htxret.ret; |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6430 | v = isttrim(v, len); |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6431 | blk = htx_replace_blk_value(htx, blk, v, IST_NULL); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6432 | len -= v.len; |
| 6433 | ret += v.len; |
| 6434 | } |
| 6435 | |
| 6436 | |
| 6437 | while (blk && len) { |
| 6438 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6439 | uint32_t sz = htx_get_blksz(blk); |
| 6440 | |
| 6441 | switch (type) { |
| 6442 | case HTX_BLK_UNUSED: |
| 6443 | break; |
| 6444 | |
| 6445 | case HTX_BLK_DATA: |
| 6446 | if (len < sz) { |
| 6447 | htx_cut_data_blk(htx, blk, len); |
| 6448 | ret += len; |
| 6449 | goto end; |
| 6450 | } |
| 6451 | break; |
| 6452 | |
| 6453 | default: |
| 6454 | goto end; |
| 6455 | } |
| 6456 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6457 | /* Remove all the data block */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6458 | len -= sz; |
| 6459 | ret += sz; |
| 6460 | blk = htx_remove_blk(htx, blk); |
| 6461 | } |
| 6462 | |
| 6463 | end: |
| 6464 | flt_update_offsets(filter, msg->chn, -ret); |
| 6465 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6466 | /* WARNING: we don't call htx_to_buf() on purpose, because we don't want |
| 6467 | * to loose the EOM flag if the message is empty. |
| 6468 | */ |
| 6469 | } |
| 6470 | |
| 6471 | /* Copies input data found in an HTTP message. Unlike the channel function used |
| 6472 | * to duplicate raw data, this one can only be called inside a filter, from |
| 6473 | * http_payload callback. So it cannot yield. An exception is returned if it is |
| 6474 | * called from another callback. If nothing was copied, a nil value is pushed on |
| 6475 | * the stack. |
| 6476 | */ |
| 6477 | __LJMP static int hlua_http_msg_get_body(lua_State *L) |
| 6478 | { |
| 6479 | struct http_msg *msg; |
| 6480 | struct filter *filter; |
| 6481 | size_t output, input; |
| 6482 | int offset, len; |
| 6483 | |
| 6484 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 6485 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 6486 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6487 | |
| 6488 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6489 | WILL_LJMP(lua_error(L)); |
| 6490 | |
| 6491 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6492 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6493 | WILL_LJMP(lua_error(L)); |
| 6494 | |
| 6495 | if (!ci_data(msg->chn) && channel_input_closed(msg->chn)) { |
| 6496 | lua_pushnil(L); |
| 6497 | return 1; |
| 6498 | } |
| 6499 | |
| 6500 | offset = output; |
| 6501 | if (lua_gettop(L) > 1) { |
| 6502 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6503 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6504 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6505 | offset += output; |
| 6506 | if (offset < output || offset > input + output) { |
| 6507 | lua_pushfstring(L, "offset out of range."); |
| 6508 | WILL_LJMP(lua_error(L)); |
| 6509 | } |
| 6510 | } |
| 6511 | len = output + input - offset; |
| 6512 | if (lua_gettop(L) == 3) { |
| 6513 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6514 | if (!len) |
| 6515 | goto dup; |
| 6516 | if (len == -1) |
| 6517 | len = global.tune.bufsize; |
| 6518 | if (len < 0) { |
| 6519 | lua_pushfstring(L, "length out of range."); |
| 6520 | WILL_LJMP(lua_error(L)); |
| 6521 | } |
| 6522 | } |
| 6523 | |
| 6524 | dup: |
| 6525 | _hlua_http_msg_dup(msg, L, offset, len); |
| 6526 | return 1; |
| 6527 | } |
| 6528 | |
| 6529 | /* Appends a string to the HTTP message, after all existing DATA blocks but |
| 6530 | * before the trailers, if any. It returns the amount of data written or -1 if |
| 6531 | * nothing was copied. Unlike the channel function used to append data, this one |
| 6532 | * can only be called inside a filter, from http_payload callback. So it cannot |
| 6533 | * yield. An exception is returned if it is called from another callback. |
| 6534 | */ |
| 6535 | __LJMP static int hlua_http_msg_append(lua_State *L) |
| 6536 | { |
| 6537 | struct http_msg *msg; |
| 6538 | struct filter *filter; |
| 6539 | const char *str; |
| 6540 | size_t offset, len, sz; |
| 6541 | int ret; |
| 6542 | |
| 6543 | MAY_LJMP(check_args(L, 2, "append")); |
| 6544 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6545 | |
| 6546 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6547 | WILL_LJMP(lua_error(L)); |
| 6548 | |
| 6549 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6550 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6551 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6552 | WILL_LJMP(lua_error(L)); |
| 6553 | |
| 6554 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset+len); |
| 6555 | lua_pushinteger(L, ret); |
| 6556 | return 1; |
| 6557 | } |
| 6558 | |
| 6559 | /* Prepends a string to the HTTP message, before all existing DATA blocks. It |
| 6560 | * returns the amount of data written or -1 if nothing was copied. Unlike the |
| 6561 | * channel function used to prepend data, this one can only be called inside a |
| 6562 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6563 | * returned if it is called from another callback. |
| 6564 | */ |
| 6565 | __LJMP static int hlua_http_msg_prepend(lua_State *L) |
| 6566 | { |
| 6567 | struct http_msg *msg; |
| 6568 | struct filter *filter; |
| 6569 | const char *str; |
| 6570 | size_t offset, len, sz; |
| 6571 | int ret; |
| 6572 | |
| 6573 | MAY_LJMP(check_args(L, 2, "prepend")); |
| 6574 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6575 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6576 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6577 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6578 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6579 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6580 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6581 | if (!filter || !hlua_filter_from_payload(filter)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6582 | WILL_LJMP(lua_error(L)); |
| 6583 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6584 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6585 | lua_pushinteger(L, ret); |
| 6586 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6587 | } |
| 6588 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6589 | /* Inserts a string to the HTTP message at a given offset. By default the string |
| 6590 | * is appended at the end of DATA blocks. It returns the amount of data written |
| 6591 | * or -1 if nothing was copied. Unlike the channel function used to insert data, |
| 6592 | * this one can only be called inside a filter, from http_payload callback. So |
| 6593 | * it cannot yield. An exception is returned if it is called from another |
| 6594 | * callback. |
| 6595 | */ |
| 6596 | __LJMP static int hlua_http_msg_insert_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6597 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6598 | struct http_msg *msg; |
| 6599 | struct filter *filter; |
| 6600 | const char *str; |
| 6601 | size_t input, output, sz; |
| 6602 | int offset; |
| 6603 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6604 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6605 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 6606 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
| 6607 | MAY_LJMP(check_args(L, 2, "insert")); |
| 6608 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6609 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6610 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6611 | WILL_LJMP(lua_error(L)); |
| 6612 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6613 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6614 | filter = hlua_http_msg_filter(L, 1, msg, &input, &output); |
| 6615 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6616 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6617 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6618 | offset = input + output; |
| 6619 | if (lua_gettop(L) > 2) { |
| 6620 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6621 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6622 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6623 | offset += output; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6624 | if (offset < output || offset > output + input) { |
| 6625 | lua_pushfstring(L, "offset out of range."); |
| 6626 | WILL_LJMP(lua_error(L)); |
| 6627 | } |
| 6628 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6629 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6630 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6631 | lua_pushinteger(L, ret); |
| 6632 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6633 | } |
| 6634 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6635 | /* Removes a given amount of data from the HTTP message at a given offset. By |
| 6636 | * default all DATA blocks are removed. It returns the amount of data |
| 6637 | * removed. Unlike the channel function used to remove data, this one can only |
| 6638 | * be called inside a filter, from http_payload callback. So it cannot yield. An |
| 6639 | * exception is returned if it is called from another callback. |
| 6640 | */ |
| 6641 | __LJMP static int hlua_http_msg_del_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6642 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6643 | struct http_msg *msg; |
| 6644 | struct filter *filter; |
| 6645 | size_t input, output; |
| 6646 | int offset, len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6647 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6648 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 6649 | WILL_LJMP(luaL_error(L, "'insert' expects at most 2 arguments")); |
| 6650 | MAY_LJMP(check_args(L, 2, "insert")); |
| 6651 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6652 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6653 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6654 | WILL_LJMP(lua_error(L)); |
| 6655 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6656 | filter = hlua_http_msg_filter(L, 1, msg, &input, &output); |
| 6657 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6658 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6659 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6660 | offset = input + output; |
| 6661 | if (lua_gettop(L) > 2) { |
| 6662 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6663 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6664 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6665 | offset += output; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6666 | if (offset < output || offset > output + input) { |
| 6667 | lua_pushfstring(L, "offset out of range."); |
| 6668 | WILL_LJMP(lua_error(L)); |
| 6669 | } |
| 6670 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6671 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6672 | len = output + input - offset; |
| 6673 | if (lua_gettop(L) == 4) { |
| 6674 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6675 | if (!len) |
| 6676 | goto end; |
| 6677 | if (len == -1) |
| 6678 | len = output + input - offset; |
| 6679 | if (len < 0 || offset + len > output + input) { |
| 6680 | lua_pushfstring(L, "length out of range."); |
| 6681 | WILL_LJMP(lua_error(L)); |
| 6682 | } |
| 6683 | } |
| 6684 | |
| 6685 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6686 | |
| 6687 | end: |
| 6688 | lua_pushinteger(L, len); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6689 | return 1; |
| 6690 | } |
| 6691 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6692 | /* 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] | 6693 | * all remaining data are removed, accordingly to the filter context. It returns |
| 6694 | * the amount of data written or -1 if nothing was copied. Unlike the channel |
| 6695 | * function used to replace data, this one can only be called inside a filter, |
| 6696 | * from http_payload callback. So it cannot yield. An exception is returned if |
| 6697 | * it is called from another callback. |
| 6698 | */ |
| 6699 | __LJMP static int hlua_http_msg_set_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6700 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6701 | struct http_msg *msg; |
| 6702 | struct filter *filter; |
| 6703 | struct htx *htx; |
| 6704 | const char *str; |
| 6705 | size_t input, output, sz; |
| 6706 | int offset, len; |
| 6707 | int ret; |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 6708 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6709 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 6710 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 6711 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6712 | |
| 6713 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6714 | WILL_LJMP(lua_error(L)); |
| 6715 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6716 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6717 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6718 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6719 | WILL_LJMP(lua_error(L)); |
| 6720 | |
| 6721 | offset = output; |
| 6722 | if (lua_gettop(L) > 2) { |
| 6723 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6724 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6725 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6726 | offset += output; |
| 6727 | if (offset < output || offset > input + output) { |
| 6728 | lua_pushfstring(L, "offset out of range."); |
| 6729 | WILL_LJMP(lua_error(L)); |
| 6730 | } |
| 6731 | } |
| 6732 | |
| 6733 | len = output + input - offset; |
| 6734 | if (lua_gettop(L) == 4) { |
| 6735 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6736 | if (!len) |
| 6737 | goto set; |
| 6738 | if (len == -1) |
| 6739 | len = output + input - offset; |
| 6740 | if (len < 0 || offset + len > output + input) { |
| 6741 | lua_pushfstring(L, "length out of range."); |
| 6742 | WILL_LJMP(lua_error(L)); |
| 6743 | } |
| 6744 | } |
| 6745 | |
| 6746 | set: |
| 6747 | /* Be sure we can copied the string once input data will be removed. */ |
| 6748 | htx = htx_from_buf(&msg->chn->buf); |
| 6749 | if (sz > htx_free_data_space(htx) + len) |
| 6750 | lua_pushinteger(L, -1); |
| 6751 | else { |
| 6752 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6753 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6754 | lua_pushinteger(L, ret); |
| 6755 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6756 | return 1; |
| 6757 | } |
| 6758 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6759 | /* Prepends data into an HTTP message and forward it, from the filter point of |
| 6760 | * view. It returns the amount of data written or -1 if nothing was sent. Unlike |
| 6761 | * the channel function used to send data, this one can only be called inside a |
| 6762 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6763 | * returned if it is called from another callback. |
| 6764 | */ |
| 6765 | __LJMP static int hlua_http_msg_send(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6766 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6767 | struct http_msg *msg; |
| 6768 | struct filter *filter; |
| 6769 | struct htx *htx; |
| 6770 | const char *str; |
| 6771 | size_t offset, len, sz; |
| 6772 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6773 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6774 | MAY_LJMP(check_args(L, 2, "send")); |
| 6775 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6776 | |
| 6777 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6778 | WILL_LJMP(lua_error(L)); |
| 6779 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6780 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6781 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6782 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6783 | WILL_LJMP(lua_error(L)); |
| 6784 | |
| 6785 | /* Return an error if the channel's output is closed */ |
| 6786 | if (unlikely(channel_output_closed(msg->chn))) { |
| 6787 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6788 | return 1; |
| 6789 | } |
| 6790 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6791 | htx = htx_from_buf(&msg->chn->buf); |
| 6792 | if (sz > htx_free_data_space(htx)) { |
| 6793 | lua_pushinteger(L, -1); |
| 6794 | return 1; |
| 6795 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6796 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6797 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6798 | if (ret > 0) { |
| 6799 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6800 | |
| 6801 | FLT_OFF(filter, msg->chn) += ret; |
| 6802 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6803 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 6804 | } |
| 6805 | |
| 6806 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6807 | return 1; |
| 6808 | } |
| 6809 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6810 | /* Forwards a given amount of bytes. It return -1 if the channel's output is |
| 6811 | * closed. Otherwise, it returns the number of bytes forwarded. Unlike the |
| 6812 | * channel function used to forward data, this one can only be called inside a |
| 6813 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6814 | * returned if it is called from another callback. All other functions deal with |
| 6815 | * DATA block, this one not. |
| 6816 | */ |
| 6817 | __LJMP static int hlua_http_msg_forward(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6818 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6819 | struct http_msg *msg; |
| 6820 | struct filter *filter; |
| 6821 | size_t offset, len; |
| 6822 | int fwd, ret = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6823 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6824 | MAY_LJMP(check_args(L, 2, "forward")); |
| 6825 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6826 | |
| 6827 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6828 | WILL_LJMP(lua_error(L)); |
| 6829 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6830 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6831 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6832 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6833 | WILL_LJMP(lua_error(L)); |
| 6834 | |
| 6835 | /* Nothing to do, just return */ |
| 6836 | if (!fwd) |
| 6837 | goto end; |
| 6838 | |
| 6839 | /* Return an error if the channel's output is closed */ |
| 6840 | if (unlikely(channel_output_closed(msg->chn))) { |
| 6841 | ret = -1; |
| 6842 | goto end; |
| 6843 | } |
| 6844 | |
| 6845 | ret = fwd; |
| 6846 | if (ret > len) |
| 6847 | ret = len; |
| 6848 | |
| 6849 | if (ret) { |
| 6850 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6851 | |
| 6852 | FLT_OFF(filter, msg->chn) += ret; |
| 6853 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 6854 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6855 | } |
| 6856 | |
| 6857 | end: |
| 6858 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6859 | return 1; |
| 6860 | } |
| 6861 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6862 | /* Set EOM flag on the HTX message. |
| 6863 | * |
| 6864 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 6865 | * really know how to do without this feature. |
| 6866 | */ |
| 6867 | __LJMP static int hlua_http_msg_set_eom(lua_State *L) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6868 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6869 | struct http_msg *msg; |
| 6870 | struct htx *htx; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6871 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6872 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 6873 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6874 | htx = htxbuf(&msg->chn->buf); |
| 6875 | htx->flags |= HTX_FL_EOM; |
| 6876 | return 0; |
| 6877 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6878 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6879 | /* Unset EOM flag on the HTX message. |
| 6880 | * |
| 6881 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 6882 | * really know how to do without this feature. |
| 6883 | */ |
| 6884 | __LJMP static int hlua_http_msg_unset_eom(lua_State *L) |
| 6885 | { |
| 6886 | struct http_msg *msg; |
| 6887 | struct htx *htx; |
| 6888 | |
| 6889 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 6890 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6891 | htx = htxbuf(&msg->chn->buf); |
| 6892 | htx->flags &= ~HTX_FL_EOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6893 | return 0; |
| 6894 | } |
| 6895 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6896 | /* |
| 6897 | * |
| 6898 | * |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 6899 | * Class HTTPClient |
| 6900 | * |
| 6901 | * |
| 6902 | */ |
| 6903 | __LJMP static struct hlua_httpclient *hlua_checkhttpclient(lua_State *L, int ud) |
| 6904 | { |
| 6905 | return MAY_LJMP(hlua_checkudata(L, ud, class_httpclient_ref)); |
| 6906 | } |
| 6907 | |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 6908 | |
| 6909 | /* stops the httpclient and ask it to kill itself */ |
| 6910 | __LJMP static int hlua_httpclient_gc(lua_State *L) |
| 6911 | { |
| 6912 | struct hlua_httpclient *hlua_hc; |
| 6913 | |
| 6914 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 6915 | |
| 6916 | hlua_hc = MAY_LJMP(hlua_checkhttpclient(L, 1)); |
| 6917 | |
| 6918 | httpclient_stop_and_destroy(hlua_hc->hc); |
| 6919 | |
| 6920 | hlua_hc->hc = NULL; |
| 6921 | |
| 6922 | |
| 6923 | return 0; |
| 6924 | } |
| 6925 | |
| 6926 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 6927 | __LJMP static int hlua_httpclient_new(lua_State *L) |
| 6928 | { |
| 6929 | struct hlua_httpclient *hlua_hc; |
| 6930 | struct hlua *hlua; |
| 6931 | |
| 6932 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6933 | hlua = hlua_gethlua(L); |
| 6934 | if (!hlua) |
| 6935 | return 0; |
| 6936 | |
| 6937 | /* Check stack size. */ |
| 6938 | if (!lua_checkstack(L, 3)) { |
| 6939 | hlua_pusherror(L, "httpclient: full stack"); |
| 6940 | goto err; |
| 6941 | } |
| 6942 | /* Create the object: obj[0] = userdata. */ |
| 6943 | lua_newtable(L); |
| 6944 | hlua_hc = MAY_LJMP(lua_newuserdata(L, sizeof(*hlua_hc))); |
| 6945 | lua_rawseti(L, -2, 0); |
| 6946 | memset(hlua_hc, 0, sizeof(*hlua_hc)); |
| 6947 | |
| 6948 | hlua_hc->hc = httpclient_new(hlua, 0, IST_NULL); |
| 6949 | if (!hlua_hc->hc) |
| 6950 | goto err; |
| 6951 | |
| 6952 | /* Pop a class stream metatable and affect it to the userdata. */ |
| 6953 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_httpclient_ref); |
| 6954 | lua_setmetatable(L, -2); |
| 6955 | |
| 6956 | return 1; |
| 6957 | |
| 6958 | err: |
| 6959 | WILL_LJMP(lua_error(L)); |
| 6960 | return 0; |
| 6961 | } |
| 6962 | |
| 6963 | |
| 6964 | /* |
| 6965 | * Callback of the httpclient, this callback wakes the lua task up, once the |
| 6966 | * httpclient receives some data |
| 6967 | * |
| 6968 | */ |
| 6969 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 6970 | static void hlua_httpclient_cb(struct httpclient *hc) |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 6971 | { |
| 6972 | struct hlua *hlua = hc->caller; |
| 6973 | |
| 6974 | if (!hlua || !hlua->task) |
| 6975 | return; |
| 6976 | |
| 6977 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 6978 | } |
| 6979 | |
| 6980 | /* |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 6981 | * Fill the lua stack with headers from the httpclient response |
| 6982 | * This works the same way as the hlua_http_get_headers() function |
| 6983 | */ |
| 6984 | __LJMP static int hlua_httpclient_get_headers(lua_State *L, struct hlua_httpclient *hlua_hc) |
| 6985 | { |
| 6986 | struct http_hdr *hdr; |
| 6987 | |
| 6988 | lua_newtable(L); |
| 6989 | |
William Lallemand | ef574b2 | 2021-10-05 16:19:31 +0200 | [diff] [blame] | 6990 | for (hdr = hlua_hc->hc->res.hdrs; hdr && isttest(hdr->n); hdr++) { |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 6991 | struct ist n, v; |
| 6992 | int len; |
| 6993 | |
| 6994 | n = hdr->n; |
| 6995 | v = hdr->v; |
| 6996 | |
| 6997 | /* Check for existing entry: |
| 6998 | * assume that the table is on the top of the stack, and |
| 6999 | * push the key in the stack, the function lua_gettable() |
| 7000 | * perform the lookup. |
| 7001 | */ |
| 7002 | |
| 7003 | lua_pushlstring(L, n.ptr, n.len); |
| 7004 | lua_gettable(L, -2); |
| 7005 | |
| 7006 | switch (lua_type(L, -1)) { |
| 7007 | case LUA_TNIL: |
| 7008 | /* Table not found, create it. */ |
| 7009 | lua_pop(L, 1); /* remove the nil value. */ |
| 7010 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 7011 | lua_newtable(L); /* create and push empty table. */ |
| 7012 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7013 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 7014 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 7015 | break; |
| 7016 | |
| 7017 | case LUA_TTABLE: |
| 7018 | /* Entry found: push the value in the table. */ |
| 7019 | len = lua_rawlen(L, -1); |
| 7020 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7021 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 7022 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 7023 | break; |
| 7024 | |
| 7025 | default: |
| 7026 | /* Other cases are errors. */ |
| 7027 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 7028 | WILL_LJMP(lua_error(L)); |
| 7029 | } |
| 7030 | } |
| 7031 | return 1; |
| 7032 | } |
| 7033 | |
| 7034 | /* |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7035 | * Allocate and return an array of http_hdr ist extracted from the <headers> lua table |
| 7036 | * |
| 7037 | * Caller must free the result |
| 7038 | */ |
| 7039 | struct http_hdr *hlua_httpclient_table_to_hdrs(lua_State *L) |
| 7040 | { |
| 7041 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
| 7042 | struct http_hdr *result = NULL; |
| 7043 | uint32_t hdr_num = 0; |
| 7044 | |
| 7045 | lua_pushnil(L); |
| 7046 | while (lua_next(L, -2) != 0) { |
| 7047 | struct ist name, value; |
| 7048 | const char *n, *v; |
| 7049 | size_t nlen, vlen; |
| 7050 | |
| 7051 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 7052 | /* Skip element if the key is not a string or if the value is not a table */ |
| 7053 | goto next_hdr; |
| 7054 | } |
| 7055 | |
| 7056 | n = lua_tolstring(L, -2, &nlen); |
| 7057 | name = ist2(n, nlen); |
| 7058 | |
| 7059 | /* Loop on header's values */ |
| 7060 | lua_pushnil(L); |
| 7061 | while (lua_next(L, -2)) { |
| 7062 | if (!lua_isstring(L, -1)) { |
| 7063 | /* Skip the value if it is not a string */ |
| 7064 | goto next_value; |
| 7065 | } |
| 7066 | |
| 7067 | v = lua_tolstring(L, -1, &vlen); |
| 7068 | value = ist2(v, vlen); |
| 7069 | name = ist2(n, nlen); |
| 7070 | |
| 7071 | hdrs[hdr_num].n = istdup(name); |
| 7072 | hdrs[hdr_num].v = istdup(value); |
| 7073 | |
| 7074 | hdr_num++; |
| 7075 | |
| 7076 | next_value: |
| 7077 | lua_pop(L, 1); |
| 7078 | } |
| 7079 | |
| 7080 | next_hdr: |
| 7081 | lua_pop(L, 1); |
| 7082 | |
| 7083 | } |
| 7084 | |
| 7085 | if (hdr_num) { |
| 7086 | /* alloc and copy the headers in the httpclient struct */ |
Tim Duesterhus | 16cc16d | 2021-11-06 15:14:45 +0100 | [diff] [blame] | 7087 | result = calloc((hdr_num + 1), sizeof(*result)); |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7088 | if (!result) |
| 7089 | goto skip_headers; |
| 7090 | memcpy(result, hdrs, sizeof(struct http_hdr) * (hdr_num + 1)); |
| 7091 | |
| 7092 | result[hdr_num].n = IST_NULL; |
| 7093 | result[hdr_num].v = IST_NULL; |
| 7094 | } |
| 7095 | |
| 7096 | skip_headers: |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7097 | |
| 7098 | return result; |
| 7099 | } |
| 7100 | |
| 7101 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7102 | /* |
| 7103 | * For each yield, checks if there is some data in the httpclient and push them |
| 7104 | * in the lua buffer, once the httpclient finished its job, push the result on |
| 7105 | * the stack |
| 7106 | */ |
| 7107 | __LJMP static int hlua_httpclient_rcv_yield(lua_State *L, int status, lua_KContext ctx) |
| 7108 | { |
| 7109 | struct buffer *tr; |
| 7110 | int res; |
| 7111 | struct hlua *hlua = hlua_gethlua(L); |
| 7112 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7113 | |
| 7114 | |
| 7115 | tr = get_trash_chunk(); |
| 7116 | |
| 7117 | res = httpclient_res_xfer(hlua_hc->hc, tr); |
| 7118 | luaL_addlstring(&hlua_hc->b, b_orig(tr), res); |
| 7119 | |
| 7120 | if (!httpclient_data(hlua_hc->hc) && httpclient_ended(hlua_hc->hc)) { |
| 7121 | |
| 7122 | luaL_pushresult(&hlua_hc->b); |
| 7123 | lua_settable(L, -3); |
| 7124 | |
| 7125 | lua_pushstring(L, "status"); |
| 7126 | lua_pushinteger(L, hlua_hc->hc->res.status); |
| 7127 | lua_settable(L, -3); |
| 7128 | |
| 7129 | |
| 7130 | lua_pushstring(L, "reason"); |
| 7131 | lua_pushlstring(L, hlua_hc->hc->res.reason.ptr, hlua_hc->hc->res.reason.len); |
| 7132 | lua_settable(L, -3); |
| 7133 | |
| 7134 | lua_pushstring(L, "headers"); |
| 7135 | hlua_httpclient_get_headers(L, hlua_hc); |
| 7136 | lua_settable(L, -3); |
| 7137 | |
| 7138 | return 1; |
| 7139 | } |
| 7140 | |
| 7141 | if (httpclient_data(hlua_hc->hc)) |
| 7142 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 7143 | |
| 7144 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7145 | |
| 7146 | return 0; |
| 7147 | } |
| 7148 | |
| 7149 | /* |
| 7150 | * Call this when trying to stream a body during a request |
| 7151 | */ |
| 7152 | __LJMP static int hlua_httpclient_snd_yield(lua_State *L, int status, lua_KContext ctx) |
| 7153 | { |
| 7154 | struct hlua *hlua; |
| 7155 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7156 | const char *body_str = NULL; |
| 7157 | int ret; |
| 7158 | int end = 0; |
| 7159 | size_t buf_len; |
| 7160 | size_t to_send = 0; |
| 7161 | |
| 7162 | hlua = hlua_gethlua(L); |
| 7163 | |
| 7164 | if (!hlua || !hlua->task) |
| 7165 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7166 | "'frontend', 'backend' or 'task'")); |
| 7167 | |
| 7168 | ret = lua_getfield(L, -1, "body"); |
| 7169 | if (ret != LUA_TSTRING) |
| 7170 | goto rcv; |
| 7171 | |
| 7172 | body_str = lua_tolstring(L, -1, &buf_len); |
| 7173 | lua_pop(L, 1); |
| 7174 | |
Christopher Faulet | fc59129 | 2022-01-12 14:46:03 +0100 | [diff] [blame] | 7175 | to_send = buf_len - hlua_hc->sent; |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7176 | |
| 7177 | if ((hlua_hc->sent + to_send) >= buf_len) |
| 7178 | end = 1; |
| 7179 | |
| 7180 | /* the end flag is always set since we are using the whole remaining size */ |
| 7181 | hlua_hc->sent += httpclient_req_xfer(hlua_hc->hc, ist2(body_str + hlua_hc->sent, to_send), end); |
| 7182 | |
| 7183 | if (buf_len > hlua_hc->sent) { |
| 7184 | /* still need to process the buffer */ |
| 7185 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_snd_yield, TICK_ETERNITY, 0)); |
| 7186 | } else { |
| 7187 | goto rcv; |
| 7188 | /* we sent the whole request buffer we can recv */ |
| 7189 | } |
| 7190 | return 0; |
| 7191 | |
| 7192 | rcv: |
| 7193 | |
| 7194 | /* we return a "res" object */ |
| 7195 | lua_newtable(L); |
| 7196 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7197 | lua_pushstring(L, "body"); |
William Lallemand | d1187eb | 2021-11-02 10:40:06 +0100 | [diff] [blame] | 7198 | luaL_buffinit(L, &hlua_hc->b); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7199 | |
William Lallemand | 933fe39 | 2021-11-04 09:45:58 +0100 | [diff] [blame] | 7200 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7201 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7202 | |
| 7203 | return 1; |
| 7204 | } |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7205 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7206 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7207 | * Send an HTTP request and wait for a response |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7208 | */ |
| 7209 | |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7210 | __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] | 7211 | { |
| 7212 | struct hlua_httpclient *hlua_hc; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7213 | struct http_hdr *hdrs = NULL; |
| 7214 | struct http_hdr *hdrs_i = NULL; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7215 | struct hlua *hlua; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7216 | const char *url_str = NULL; |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7217 | const char *body_str = NULL; |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7218 | size_t buf_len; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7219 | int ret; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7220 | |
| 7221 | hlua = hlua_gethlua(L); |
| 7222 | |
| 7223 | if (!hlua || !hlua->task) |
| 7224 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7225 | "'frontend', 'backend' or 'task'")); |
| 7226 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7227 | if (lua_gettop(L) != 2 || lua_type(L, -1) != LUA_TTABLE) |
| 7228 | WILL_LJMP(luaL_error(L, "'get' needs a table as argument")); |
| 7229 | |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 7230 | hlua_hc = hlua_checkhttpclient(L, 1); |
| 7231 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7232 | lua_pushnil(L); /* first key */ |
| 7233 | while (lua_next(L, 2)) { |
| 7234 | if (strcmp(lua_tostring(L, -2), "dst") == 0) { |
| 7235 | if (httpclient_set_dst(hlua_hc->hc, lua_tostring(L, -1)) < 0) |
| 7236 | WILL_LJMP(luaL_error(L, "Can't use the 'dst' argument")); |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 7237 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7238 | } else if (strcmp(lua_tostring(L, -2), "url") == 0) { |
| 7239 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7240 | WILL_LJMP(luaL_error(L, "invalid parameter in 'url', must be a string")); |
| 7241 | url_str = lua_tostring(L, -1); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7242 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7243 | } else if (strcmp(lua_tostring(L, -2), "timeout") == 0) { |
| 7244 | if (lua_type(L, -1) != LUA_TNUMBER) |
| 7245 | WILL_LJMP(luaL_error(L, "invalid parameter in 'timeout', must be a number")); |
| 7246 | httpclient_set_timeout(hlua_hc->hc, lua_tointeger(L, -1)); |
William Lallemand | b4a4ef6 | 2022-02-23 14:18:16 +0100 | [diff] [blame] | 7247 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7248 | } else if (strcmp(lua_tostring(L, -2), "headers") == 0) { |
| 7249 | if (lua_type(L, -1) != LUA_TTABLE) |
| 7250 | WILL_LJMP(luaL_error(L, "invalid parameter in 'headers', must be a table")); |
| 7251 | hdrs = hlua_httpclient_table_to_hdrs(L); |
William Lallemand | 79416cb | 2021-09-24 14:51:44 +0200 | [diff] [blame] | 7252 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7253 | } else if (strcmp(lua_tostring(L, -2), "body") == 0) { |
| 7254 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7255 | WILL_LJMP(luaL_error(L, "invalid parameter in 'body', must be a string")); |
| 7256 | body_str = lua_tolstring(L, -1, &buf_len); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7257 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7258 | } else { |
| 7259 | WILL_LJMP(luaL_error(L, "'%s' invalid parameter name", lua_tostring(L, -2))); |
| 7260 | } |
| 7261 | /* removes 'value'; keeps 'key' for next iteration */ |
| 7262 | lua_pop(L, 1); |
| 7263 | } |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7264 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7265 | if (!url_str) { |
| 7266 | WILL_LJMP(luaL_error(L, "'get' need a 'url' argument")); |
| 7267 | return 0; |
| 7268 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7269 | |
William Lallemand | 10a3736 | 2022-03-02 16:18:26 +0100 | [diff] [blame] | 7270 | hlua_hc->sent = 0; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7271 | |
| 7272 | hlua_hc->hc->req.url = istdup(ist(url_str)); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7273 | hlua_hc->hc->req.meth = meth; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7274 | |
| 7275 | /* update the httpclient callbacks */ |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7276 | hlua_hc->hc->ops.res_stline = hlua_httpclient_cb; |
| 7277 | hlua_hc->hc->ops.res_headers = hlua_httpclient_cb; |
| 7278 | hlua_hc->hc->ops.res_payload = hlua_httpclient_cb; |
William Lallemand | 8f170c7 | 2022-03-15 10:52:07 +0100 | [diff] [blame] | 7279 | hlua_hc->hc->ops.res_end = hlua_httpclient_cb; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7280 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7281 | /* a body is available, it will use the request callback */ |
| 7282 | if (body_str) { |
| 7283 | hlua_hc->hc->ops.req_payload = hlua_httpclient_cb; |
| 7284 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7285 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7286 | 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] | 7287 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7288 | /* free the temporary headers array */ |
| 7289 | hdrs_i = hdrs; |
| 7290 | while (hdrs_i && isttest(hdrs_i->n)) { |
| 7291 | istfree(&hdrs_i->n); |
| 7292 | istfree(&hdrs_i->v); |
| 7293 | hdrs_i++; |
| 7294 | } |
| 7295 | ha_free(&hdrs); |
| 7296 | |
| 7297 | |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7298 | if (ret != ERR_NONE) { |
| 7299 | WILL_LJMP(luaL_error(L, "Can't generate the HTTP request")); |
| 7300 | return 0; |
| 7301 | } |
| 7302 | |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7303 | httpclient_start(hlua_hc->hc); |
| 7304 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7305 | 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] | 7306 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7307 | return 0; |
| 7308 | } |
| 7309 | |
| 7310 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7311 | * Sends an HTTP HEAD request and wait for a response |
| 7312 | * |
| 7313 | * httpclient:head(url, headers, payload) |
| 7314 | */ |
| 7315 | __LJMP static int hlua_httpclient_head(lua_State *L) |
| 7316 | { |
| 7317 | return hlua_httpclient_send(L, HTTP_METH_HEAD); |
| 7318 | } |
| 7319 | |
| 7320 | /* |
| 7321 | * Send an HTTP GET request and wait for a response |
| 7322 | * |
| 7323 | * httpclient:get(url, headers, payload) |
| 7324 | */ |
| 7325 | __LJMP static int hlua_httpclient_get(lua_State *L) |
| 7326 | { |
| 7327 | return hlua_httpclient_send(L, HTTP_METH_GET); |
| 7328 | |
| 7329 | } |
| 7330 | |
| 7331 | /* |
| 7332 | * Sends an HTTP PUT request and wait for a response |
| 7333 | * |
| 7334 | * httpclient:put(url, headers, payload) |
| 7335 | */ |
| 7336 | __LJMP static int hlua_httpclient_put(lua_State *L) |
| 7337 | { |
| 7338 | return hlua_httpclient_send(L, HTTP_METH_PUT); |
| 7339 | } |
| 7340 | |
| 7341 | /* |
| 7342 | * Send an HTTP POST request and wait for a response |
| 7343 | * |
| 7344 | * httpclient:post(url, headers, payload) |
| 7345 | */ |
| 7346 | __LJMP static int hlua_httpclient_post(lua_State *L) |
| 7347 | { |
| 7348 | return hlua_httpclient_send(L, HTTP_METH_POST); |
| 7349 | } |
| 7350 | |
| 7351 | |
| 7352 | /* |
| 7353 | * Sends an HTTP DELETE request and wait for a response |
| 7354 | * |
| 7355 | * httpclient:delete(url, headers, payload) |
| 7356 | */ |
| 7357 | __LJMP static int hlua_httpclient_delete(lua_State *L) |
| 7358 | { |
| 7359 | return hlua_httpclient_send(L, HTTP_METH_DELETE); |
| 7360 | } |
| 7361 | |
| 7362 | /* |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7363 | * |
| 7364 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7365 | * Class TXN |
| 7366 | * |
| 7367 | * |
| 7368 | */ |
| 7369 | |
| 7370 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7371 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7372 | */ |
| 7373 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 7374 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7375 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7376 | } |
| 7377 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7378 | __LJMP static int hlua_set_var(lua_State *L) |
| 7379 | { |
| 7380 | struct hlua_txn *htxn; |
| 7381 | const char *name; |
| 7382 | size_t len; |
| 7383 | struct sample smp; |
| 7384 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 7385 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7386 | 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] | 7387 | |
| 7388 | /* It is useles to retrieve the stream, but this function |
| 7389 | * runs only in a stream context. |
| 7390 | */ |
| 7391 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7392 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7393 | |
| 7394 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 7395 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7396 | hlua_lua2smp(L, 3, &smp); |
| 7397 | |
| 7398 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7399 | 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] | 7400 | |
| 7401 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 7402 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 7403 | else |
| 7404 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 7405 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 7406 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7407 | } |
| 7408 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7409 | __LJMP static int hlua_unset_var(lua_State *L) |
| 7410 | { |
| 7411 | struct hlua_txn *htxn; |
| 7412 | const char *name; |
| 7413 | size_t len; |
| 7414 | struct sample smp; |
| 7415 | |
| 7416 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 7417 | |
| 7418 | /* It is useles to retrieve the stream, but this function |
| 7419 | * runs only in a stream context. |
| 7420 | */ |
| 7421 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7422 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7423 | |
| 7424 | /* Unset the variable. */ |
| 7425 | 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] | 7426 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 7427 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7428 | } |
| 7429 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7430 | __LJMP static int hlua_get_var(lua_State *L) |
| 7431 | { |
| 7432 | struct hlua_txn *htxn; |
| 7433 | const char *name; |
| 7434 | size_t len; |
| 7435 | struct sample smp; |
| 7436 | |
| 7437 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 7438 | |
| 7439 | /* It is useles to retrieve the stream, but this function |
| 7440 | * runs only in a stream context. |
| 7441 | */ |
| 7442 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7443 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7444 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7445 | 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] | 7446 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7447 | lua_pushnil(L); |
| 7448 | return 1; |
| 7449 | } |
| 7450 | |
| 7451 | return hlua_smp2lua(L, &smp); |
| 7452 | } |
| 7453 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7454 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7455 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7456 | struct hlua *hlua; |
| 7457 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7458 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 7459 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7460 | /* It is useles to retrieve the stream, but this function |
| 7461 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7462 | */ |
| 7463 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7464 | |
| 7465 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7466 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7467 | if (!hlua) |
| 7468 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7469 | |
| 7470 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 7471 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7472 | |
| 7473 | /* Get and store new value. */ |
| 7474 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 7475 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 7476 | |
| 7477 | return 0; |
| 7478 | } |
| 7479 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7480 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7481 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7482 | struct hlua *hlua; |
| 7483 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7484 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 7485 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7486 | /* It is useles to retrieve the stream, but this function |
| 7487 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7488 | */ |
| 7489 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7490 | |
| 7491 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7492 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7493 | if (!hlua) { |
| 7494 | lua_pushnil(L); |
| 7495 | return 1; |
| 7496 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7497 | |
| 7498 | /* Push configuration index in the stack. */ |
| 7499 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 7500 | |
| 7501 | return 1; |
| 7502 | } |
| 7503 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7504 | /* Create stack entry containing a class TXN. This function |
| 7505 | * return 0 if the stack does not contains free slots, |
| 7506 | * otherwise it returns 1. |
| 7507 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7508 | 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] | 7509 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7510 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7511 | |
| 7512 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7513 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7514 | return 0; |
| 7515 | |
| 7516 | /* NOTE: The allocation never fails. The failure |
| 7517 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7518 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7519 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7520 | /* Create the object: obj[0] = userdata. */ |
| 7521 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7522 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7523 | lua_rawseti(L, -2, 0); |
| 7524 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7525 | htxn->s = s; |
| 7526 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 7527 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7528 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7529 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7530 | /* Create the "f" field that contains a list of fetches. */ |
| 7531 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7532 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7533 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7534 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7535 | |
| 7536 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 7537 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7538 | 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] | 7539 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7540 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7541 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7542 | /* Create the "c" field that contains a list of converters. */ |
| 7543 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7544 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7545 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7546 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7547 | |
| 7548 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 7549 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 7550 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7551 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7552 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7553 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7554 | /* Create the "req" field that contains the request channel object. */ |
| 7555 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7556 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7557 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7558 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7559 | |
| 7560 | /* Create the "res" field that contains the response channel object. */ |
| 7561 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7562 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7563 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7564 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7565 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7566 | /* Creates the HTTP object is the current proxy allows http. */ |
| 7567 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 7568 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7569 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7570 | return 0; |
| 7571 | } |
| 7572 | else |
| 7573 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7574 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7575 | |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 7576 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) { |
| 7577 | /* HTTPMessage object are created when a lua TXN is created from |
| 7578 | * a filter context only |
| 7579 | */ |
| 7580 | |
| 7581 | /* Creates the HTTP-Request object is the current proxy allows http. */ |
| 7582 | lua_pushstring(L, "http_req"); |
| 7583 | if (p->mode == PR_MODE_HTTP) { |
| 7584 | if (!hlua_http_msg_new(L, &s->txn->req)) |
| 7585 | return 0; |
| 7586 | } |
| 7587 | else |
| 7588 | lua_pushnil(L); |
| 7589 | lua_rawset(L, -3); |
| 7590 | |
| 7591 | /* Creates the HTTP-Response object is the current proxy allows http. */ |
| 7592 | lua_pushstring(L, "http_res"); |
| 7593 | if (p->mode == PR_MODE_HTTP) { |
| 7594 | if (!hlua_http_msg_new(L, &s->txn->rsp)) |
| 7595 | return 0; |
| 7596 | } |
| 7597 | else |
| 7598 | lua_pushnil(L); |
| 7599 | lua_rawset(L, -3); |
| 7600 | } |
| 7601 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7602 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 7603 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 7604 | lua_setmetatable(L, -2); |
| 7605 | |
| 7606 | return 1; |
| 7607 | } |
| 7608 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7609 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 7610 | { |
| 7611 | const char *msg; |
| 7612 | struct hlua_txn *htxn; |
| 7613 | |
| 7614 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 7615 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7616 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7617 | |
| 7618 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 7619 | return 0; |
| 7620 | } |
| 7621 | |
| 7622 | __LJMP static int hlua_txn_log(lua_State *L) |
| 7623 | { |
| 7624 | int level; |
| 7625 | const char *msg; |
| 7626 | struct hlua_txn *htxn; |
| 7627 | |
| 7628 | MAY_LJMP(check_args(L, 3, "log")); |
| 7629 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7630 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7631 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 7632 | |
| 7633 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 7634 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 7635 | |
| 7636 | hlua_sendlog(htxn->s->be, level, msg); |
| 7637 | return 0; |
| 7638 | } |
| 7639 | |
| 7640 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 7641 | { |
| 7642 | const char *msg; |
| 7643 | struct hlua_txn *htxn; |
| 7644 | |
| 7645 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 7646 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7647 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7648 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 7649 | return 0; |
| 7650 | } |
| 7651 | |
| 7652 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 7653 | { |
| 7654 | const char *msg; |
| 7655 | struct hlua_txn *htxn; |
| 7656 | |
| 7657 | MAY_LJMP(check_args(L, 2, "Info")); |
| 7658 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7659 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7660 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 7661 | return 0; |
| 7662 | } |
| 7663 | |
| 7664 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 7665 | { |
| 7666 | const char *msg; |
| 7667 | struct hlua_txn *htxn; |
| 7668 | |
| 7669 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 7670 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7671 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7672 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 7673 | return 0; |
| 7674 | } |
| 7675 | |
| 7676 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 7677 | { |
| 7678 | const char *msg; |
| 7679 | struct hlua_txn *htxn; |
| 7680 | |
| 7681 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 7682 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7683 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7684 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 7685 | return 0; |
| 7686 | } |
| 7687 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7688 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 7689 | { |
| 7690 | struct hlua_txn *htxn; |
| 7691 | int ll; |
| 7692 | |
| 7693 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 7694 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7695 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7696 | |
| 7697 | if (ll < 0 || ll > 7) |
| 7698 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 7699 | |
| 7700 | htxn->s->logs.level = ll; |
| 7701 | return 0; |
| 7702 | } |
| 7703 | |
| 7704 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 7705 | { |
| 7706 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7707 | int tos; |
| 7708 | |
| 7709 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 7710 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7711 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7712 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 7713 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7714 | return 0; |
| 7715 | } |
| 7716 | |
| 7717 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 7718 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7719 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7720 | int mark; |
| 7721 | |
| 7722 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 7723 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7724 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7725 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 7726 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7727 | return 0; |
| 7728 | } |
| 7729 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 7730 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 7731 | { |
| 7732 | struct hlua_txn *htxn; |
| 7733 | |
| 7734 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 7735 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7736 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 7737 | return 0; |
| 7738 | } |
| 7739 | |
| 7740 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 7741 | { |
| 7742 | struct hlua_txn *htxn; |
| 7743 | |
| 7744 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 7745 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7746 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 7747 | return 0; |
| 7748 | } |
| 7749 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7750 | /* 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] | 7751 | * 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] | 7752 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 7753 | * error. The Reply must be on top of the stack. |
| 7754 | */ |
| 7755 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 7756 | { |
| 7757 | struct htx *htx; |
| 7758 | struct htx_sl *sl; |
| 7759 | struct h1m h1m; |
| 7760 | const char *status, *reason, *body; |
| 7761 | size_t status_len, reason_len, body_len; |
| 7762 | int ret, code, flags; |
| 7763 | |
| 7764 | code = 200; |
| 7765 | status = "200"; |
| 7766 | status_len = 3; |
| 7767 | ret = lua_getfield(L, -1, "status"); |
| 7768 | if (ret == LUA_TNUMBER) { |
| 7769 | code = lua_tointeger(L, -1); |
| 7770 | status = lua_tolstring(L, -1, &status_len); |
| 7771 | } |
| 7772 | lua_pop(L, 1); |
| 7773 | |
| 7774 | reason = http_get_reason(code); |
| 7775 | reason_len = strlen(reason); |
| 7776 | ret = lua_getfield(L, -1, "reason"); |
| 7777 | if (ret == LUA_TSTRING) |
| 7778 | reason = lua_tolstring(L, -1, &reason_len); |
| 7779 | lua_pop(L, 1); |
| 7780 | |
| 7781 | body = NULL; |
| 7782 | body_len = 0; |
| 7783 | ret = lua_getfield(L, -1, "body"); |
| 7784 | if (ret == LUA_TSTRING) |
| 7785 | body = lua_tolstring(L, -1, &body_len); |
| 7786 | lua_pop(L, 1); |
| 7787 | |
| 7788 | /* Prepare the response before inserting the headers */ |
| 7789 | h1m_init_res(&h1m); |
| 7790 | htx = htx_from_buf(&s->res.buf); |
| 7791 | channel_htx_truncate(&s->res, htx); |
| 7792 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 7793 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 7794 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 7795 | ist2(status, status_len), ist2(reason, reason_len)); |
| 7796 | } |
| 7797 | else { |
| 7798 | flags = HTX_SL_F_IS_RESP; |
| 7799 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 7800 | ist2(status, status_len), ist2(reason, reason_len)); |
| 7801 | } |
| 7802 | if (!sl) |
| 7803 | goto fail; |
| 7804 | sl->info.res.status = code; |
| 7805 | |
| 7806 | /* Push in the stack the "headers" entry. */ |
| 7807 | ret = lua_getfield(L, -1, "headers"); |
| 7808 | if (ret != LUA_TTABLE) |
| 7809 | goto skip_headers; |
| 7810 | |
| 7811 | lua_pushnil(L); |
| 7812 | while (lua_next(L, -2) != 0) { |
| 7813 | struct ist name, value; |
| 7814 | const char *n, *v; |
| 7815 | size_t nlen, vlen; |
| 7816 | |
| 7817 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 7818 | /* Skip element if the key is not a string or if the value is not a table */ |
| 7819 | goto next_hdr; |
| 7820 | } |
| 7821 | |
| 7822 | n = lua_tolstring(L, -2, &nlen); |
| 7823 | name = ist2(n, nlen); |
| 7824 | if (isteqi(name, ist("content-length"))) { |
| 7825 | /* Always skip content-length header. It will be added |
| 7826 | * later with the correct len |
| 7827 | */ |
| 7828 | goto next_hdr; |
| 7829 | } |
| 7830 | |
| 7831 | /* Loop on header's values */ |
| 7832 | lua_pushnil(L); |
| 7833 | while (lua_next(L, -2)) { |
| 7834 | if (!lua_isstring(L, -1)) { |
| 7835 | /* Skip the value if it is not a string */ |
| 7836 | goto next_value; |
| 7837 | } |
| 7838 | |
| 7839 | v = lua_tolstring(L, -1, &vlen); |
| 7840 | value = ist2(v, vlen); |
| 7841 | |
| 7842 | if (isteqi(name, ist("transfer-encoding"))) |
| 7843 | h1_parse_xfer_enc_header(&h1m, value); |
| 7844 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 7845 | goto fail; |
| 7846 | |
| 7847 | next_value: |
| 7848 | lua_pop(L, 1); |
| 7849 | } |
| 7850 | |
| 7851 | next_hdr: |
| 7852 | lua_pop(L, 1); |
| 7853 | } |
| 7854 | skip_headers: |
| 7855 | lua_pop(L, 1); |
| 7856 | |
| 7857 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 7858 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 7859 | const char *clen = ultoa(body_len); |
| 7860 | |
| 7861 | h1m.flags |= H1_MF_CLEN; |
| 7862 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 7863 | goto fail; |
| 7864 | } |
| 7865 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 7866 | h1m.flags |= H1_MF_XFER_LEN; |
| 7867 | |
| 7868 | /* Update HTX start-line flags */ |
| 7869 | if (h1m.flags & H1_MF_XFER_ENC) |
| 7870 | flags |= HTX_SL_F_XFER_ENC; |
| 7871 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 7872 | flags |= HTX_SL_F_XFER_LEN; |
| 7873 | if (h1m.flags & H1_MF_CHNK) |
| 7874 | flags |= HTX_SL_F_CHNK; |
| 7875 | else if (h1m.flags & H1_MF_CLEN) |
| 7876 | flags |= HTX_SL_F_CLEN; |
| 7877 | if (h1m.body_len == 0) |
| 7878 | flags |= HTX_SL_F_BODYLESS; |
| 7879 | } |
| 7880 | sl->flags |= flags; |
| 7881 | |
| 7882 | |
| 7883 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7884 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7885 | goto fail; |
| 7886 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7887 | htx->flags |= HTX_FL_EOM; |
| 7888 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7889 | /* Now, forward the response and terminate the transaction */ |
| 7890 | s->txn->status = code; |
| 7891 | htx_to_buf(htx, &s->res.buf); |
| 7892 | if (!http_forward_proxy_resp(s, 1)) |
| 7893 | goto fail; |
| 7894 | |
| 7895 | return 1; |
| 7896 | |
| 7897 | fail: |
| 7898 | channel_htx_truncate(&s->res, htx); |
| 7899 | return 0; |
| 7900 | } |
| 7901 | |
| 7902 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 7903 | * processing is just aborted. Nothing is returned to the client and all |
| 7904 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 7905 | * is forwarded to the client before terminating the transaction. On success, |
| 7906 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 7907 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 7908 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7909 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7910 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7911 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 7912 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7913 | struct stream *s; |
| 7914 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7915 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 7916 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7917 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7918 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 7919 | * just end the execution of the current lua code. */ |
| 7920 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7921 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7922 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7923 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 7924 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7925 | struct channel *req = &s->req; |
| 7926 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 7927 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7928 | channel_auto_read(req); |
| 7929 | channel_abort(req); |
| 7930 | channel_auto_close(req); |
| 7931 | channel_erase(req); |
| 7932 | |
| 7933 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 7934 | channel_auto_read(res); |
| 7935 | channel_auto_close(res); |
| 7936 | channel_shutr_now(res); |
| 7937 | |
| 7938 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 7939 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 7940 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 7941 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7942 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 7943 | /* No reply or invalid reply */ |
| 7944 | s->txn->status = 0; |
| 7945 | http_reply_and_close(s, 0, NULL); |
| 7946 | } |
| 7947 | else { |
| 7948 | /* Remove extra args to have the reply on top of the stack */ |
| 7949 | if (lua_gettop(L) > 2) |
| 7950 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7951 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7952 | if (!hlua_txn_forward_reply(L, s)) { |
| 7953 | if (!(s->flags & SF_ERR_MASK)) |
| 7954 | s->flags |= SF_ERR_PRXCOND; |
| 7955 | lua_pushinteger(L, ACT_RET_ERR); |
| 7956 | WILL_LJMP(hlua_done(L)); |
| 7957 | return 0; /* Never reached */ |
| 7958 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 7959 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7960 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7961 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 7962 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 7963 | /* let's log the request time */ |
| 7964 | s->logs.tv_request = now; |
| 7965 | 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] | 7966 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7967 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 7968 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7969 | done: |
| 7970 | if (!(s->flags & SF_ERR_MASK)) |
| 7971 | s->flags |= SF_ERR_LOCAL; |
| 7972 | if (!(s->flags & SF_FINST_MASK)) |
| 7973 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 7974 | |
Christopher Faulet | e48d1dc | 2021-08-13 14:11:17 +0200 | [diff] [blame] | 7975 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) |
| 7976 | lua_pushinteger(L, -1); |
| 7977 | else |
| 7978 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7979 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7980 | return 0; |
| 7981 | } |
| 7982 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7983 | /* |
| 7984 | * |
| 7985 | * |
| 7986 | * Class REPLY |
| 7987 | * |
| 7988 | * |
| 7989 | */ |
| 7990 | |
| 7991 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 7992 | * free slots, the function fails and returns 0; |
| 7993 | */ |
| 7994 | static int hlua_txn_reply_new(lua_State *L) |
| 7995 | { |
| 7996 | struct hlua_txn *htxn; |
| 7997 | const char *reason, *body = NULL; |
| 7998 | int ret, status; |
| 7999 | |
| 8000 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8001 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8002 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 8003 | WILL_LJMP(lua_error(L)); |
| 8004 | } |
| 8005 | |
| 8006 | /* Default value */ |
| 8007 | status = 200; |
| 8008 | reason = http_get_reason(status); |
| 8009 | |
| 8010 | if (lua_istable(L, 2)) { |
| 8011 | /* load status and reason from the table argument at index 2 */ |
| 8012 | ret = lua_getfield(L, 2, "status"); |
| 8013 | if (ret == LUA_TNIL) |
| 8014 | goto reason; |
| 8015 | else if (ret != LUA_TNUMBER) { |
| 8016 | /* invalid status: ignore the reason */ |
| 8017 | goto body; |
| 8018 | } |
| 8019 | status = lua_tointeger(L, -1); |
| 8020 | |
| 8021 | reason: |
| 8022 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 8023 | ret = lua_getfield(L, 2, "reason"); |
| 8024 | if (ret == LUA_TSTRING) |
| 8025 | reason = lua_tostring(L, -1); |
| 8026 | |
| 8027 | body: |
| 8028 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 8029 | ret = lua_getfield(L, 2, "body"); |
| 8030 | if (ret == LUA_TSTRING) |
| 8031 | body = lua_tostring(L, -1); |
| 8032 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 8033 | } |
| 8034 | |
| 8035 | /* Create the Reply table */ |
| 8036 | lua_newtable(L); |
| 8037 | |
| 8038 | /* Add status element */ |
| 8039 | lua_pushstring(L, "status"); |
| 8040 | lua_pushinteger(L, status); |
| 8041 | lua_settable(L, -3); |
| 8042 | |
| 8043 | /* Add reason element */ |
| 8044 | reason = http_get_reason(status); |
| 8045 | lua_pushstring(L, "reason"); |
| 8046 | lua_pushstring(L, reason); |
| 8047 | lua_settable(L, -3); |
| 8048 | |
| 8049 | /* Add body element, nil if undefined */ |
| 8050 | lua_pushstring(L, "body"); |
| 8051 | if (body) |
| 8052 | lua_pushstring(L, body); |
| 8053 | else |
| 8054 | lua_pushnil(L); |
| 8055 | lua_settable(L, -3); |
| 8056 | |
| 8057 | /* Add headers element */ |
| 8058 | lua_pushstring(L, "headers"); |
| 8059 | lua_newtable(L); |
| 8060 | |
| 8061 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8062 | if (lua_istable(L, 2)) { |
| 8063 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 8064 | ret = lua_getfield(L, 2, "headers"); |
| 8065 | if (ret == LUA_TTABLE) { |
| 8066 | /* stack: [ ... <headers:table>, <table> ] */ |
| 8067 | lua_pushnil(L); |
| 8068 | while (lua_next(L, -2) != 0) { |
| 8069 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 8070 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 8071 | /* invalid value type, skip it */ |
| 8072 | lua_pop(L, 1); |
| 8073 | continue; |
| 8074 | } |
| 8075 | |
| 8076 | |
| 8077 | /* Duplicate the key and swap it with the value. */ |
| 8078 | lua_pushvalue(L, -2); |
| 8079 | lua_insert(L, -2); |
| 8080 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 8081 | |
| 8082 | lua_newtable(L); |
| 8083 | lua_insert(L, -2); |
| 8084 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 8085 | |
| 8086 | if (lua_isstring(L, -1)) { |
| 8087 | /* push the value in the inner table */ |
| 8088 | lua_rawseti(L, -2, 1); |
| 8089 | } |
| 8090 | else { /* table */ |
| 8091 | lua_pushnil(L); |
| 8092 | while (lua_next(L, -2) != 0) { |
| 8093 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 8094 | if (!lua_isstring(L, -1)) { |
| 8095 | /* invalid value type, skip it*/ |
| 8096 | lua_pop(L, 1); |
| 8097 | continue; |
| 8098 | } |
| 8099 | /* push the value in the inner table */ |
| 8100 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 8101 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 8102 | } |
| 8103 | lua_pop(L, 1); |
| 8104 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 8105 | } |
| 8106 | |
| 8107 | /* push (k,v) on the stack in the headers table: |
| 8108 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 8109 | */ |
| 8110 | lua_settable(L, -5); |
| 8111 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 8112 | } |
| 8113 | } |
| 8114 | lua_pop(L, 1); |
| 8115 | } |
| 8116 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8117 | lua_settable(L, -3); |
| 8118 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 8119 | |
| 8120 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 8121 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 8122 | lua_setmetatable(L, -2); |
| 8123 | return 1; |
| 8124 | } |
| 8125 | |
| 8126 | /* Set the reply status code, and optionally the reason. If no reason is |
| 8127 | * provided, the default one corresponding to the status code is used. |
| 8128 | */ |
| 8129 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 8130 | { |
| 8131 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 8132 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 8133 | |
| 8134 | /* First argument (self) must be a table */ |
| 8135 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8136 | |
| 8137 | if (status < 100 || status > 599) { |
| 8138 | lua_pushboolean(L, 0); |
| 8139 | return 1; |
| 8140 | } |
| 8141 | if (!reason) |
| 8142 | reason = http_get_reason(status); |
| 8143 | |
| 8144 | lua_pushinteger(L, status); |
| 8145 | lua_setfield(L, 1, "status"); |
| 8146 | |
| 8147 | lua_pushstring(L, reason); |
| 8148 | lua_setfield(L, 1, "reason"); |
| 8149 | |
| 8150 | lua_pushboolean(L, 1); |
| 8151 | return 1; |
| 8152 | } |
| 8153 | |
| 8154 | /* Add a header into the reply object. Each header name is associated to an |
| 8155 | * array of values in the "headers" table. If the header name is not found, a |
| 8156 | * new entry is created. |
| 8157 | */ |
| 8158 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 8159 | { |
| 8160 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8161 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 8162 | int ret; |
| 8163 | |
| 8164 | /* First argument (self) must be a table */ |
| 8165 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8166 | |
| 8167 | /* Push in the stack the "headers" entry. */ |
| 8168 | ret = lua_getfield(L, 1, "headers"); |
| 8169 | if (ret != LUA_TTABLE) { |
| 8170 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 8171 | WILL_LJMP(lua_error(L)); |
| 8172 | } |
| 8173 | |
| 8174 | /* check if the header is already registered. If not, register it. */ |
| 8175 | ret = lua_getfield(L, -1, name); |
| 8176 | if (ret == LUA_TNIL) { |
| 8177 | /* Entry not found. */ |
| 8178 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 8179 | |
| 8180 | /* Insert the new header name in the array in the top of the stack. |
| 8181 | * It left the new array in the top of the stack. |
| 8182 | */ |
| 8183 | lua_newtable(L); |
| 8184 | lua_pushstring(L, name); |
| 8185 | lua_pushvalue(L, -2); |
| 8186 | lua_settable(L, -4); |
| 8187 | } |
| 8188 | else if (ret != LUA_TTABLE) { |
| 8189 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 8190 | WILL_LJMP(lua_error(L)); |
| 8191 | } |
| 8192 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8193 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8194 | * the header value as new entry. |
| 8195 | */ |
| 8196 | lua_pushstring(L, value); |
| 8197 | ret = lua_rawlen(L, -2); |
| 8198 | lua_rawseti(L, -2, ret + 1); |
| 8199 | |
| 8200 | lua_pushboolean(L, 1); |
| 8201 | return 1; |
| 8202 | } |
| 8203 | |
| 8204 | /* Remove all occurrences of a given header name. */ |
| 8205 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 8206 | { |
| 8207 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8208 | int ret; |
| 8209 | |
| 8210 | /* First argument (self) must be a table */ |
| 8211 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8212 | |
| 8213 | /* Push in the stack the "headers" entry. */ |
| 8214 | ret = lua_getfield(L, 1, "headers"); |
| 8215 | if (ret != LUA_TTABLE) { |
| 8216 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 8217 | WILL_LJMP(lua_error(L)); |
| 8218 | } |
| 8219 | |
| 8220 | lua_pushstring(L, name); |
| 8221 | lua_pushnil(L); |
| 8222 | lua_settable(L, -3); |
| 8223 | |
| 8224 | lua_pushboolean(L, 1); |
| 8225 | return 1; |
| 8226 | } |
| 8227 | |
| 8228 | /* Set the reply's body. Overwrite any existing entry. */ |
| 8229 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 8230 | { |
| 8231 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8232 | |
| 8233 | /* First argument (self) must be a table */ |
| 8234 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8235 | |
| 8236 | lua_pushstring(L, payload); |
| 8237 | lua_setfield(L, 1, "body"); |
| 8238 | |
| 8239 | lua_pushboolean(L, 1); |
| 8240 | return 1; |
| 8241 | } |
| 8242 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8243 | __LJMP static int hlua_log(lua_State *L) |
| 8244 | { |
| 8245 | int level; |
| 8246 | const char *msg; |
| 8247 | |
| 8248 | MAY_LJMP(check_args(L, 2, "log")); |
| 8249 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 8250 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8251 | |
| 8252 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 8253 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 8254 | |
| 8255 | hlua_sendlog(NULL, level, msg); |
| 8256 | return 0; |
| 8257 | } |
| 8258 | |
| 8259 | __LJMP static int hlua_log_debug(lua_State *L) |
| 8260 | { |
| 8261 | const char *msg; |
| 8262 | |
| 8263 | MAY_LJMP(check_args(L, 1, "debug")); |
| 8264 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8265 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 8266 | return 0; |
| 8267 | } |
| 8268 | |
| 8269 | __LJMP static int hlua_log_info(lua_State *L) |
| 8270 | { |
| 8271 | const char *msg; |
| 8272 | |
| 8273 | MAY_LJMP(check_args(L, 1, "info")); |
| 8274 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8275 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 8276 | return 0; |
| 8277 | } |
| 8278 | |
| 8279 | __LJMP static int hlua_log_warning(lua_State *L) |
| 8280 | { |
| 8281 | const char *msg; |
| 8282 | |
| 8283 | MAY_LJMP(check_args(L, 1, "warning")); |
| 8284 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8285 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 8286 | return 0; |
| 8287 | } |
| 8288 | |
| 8289 | __LJMP static int hlua_log_alert(lua_State *L) |
| 8290 | { |
| 8291 | const char *msg; |
| 8292 | |
| 8293 | MAY_LJMP(check_args(L, 1, "alert")); |
| 8294 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8295 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8296 | return 0; |
| 8297 | } |
| 8298 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8299 | __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] | 8300 | { |
| 8301 | int wakeup_ms = lua_tointeger(L, -1); |
Willy Tarreau | 12c0270 | 2021-09-30 16:12:31 +0200 | [diff] [blame] | 8302 | if (!tick_is_expired(wakeup_ms, now_ms)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8303 | 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] | 8304 | return 0; |
| 8305 | } |
| 8306 | |
| 8307 | __LJMP static int hlua_sleep(lua_State *L) |
| 8308 | { |
| 8309 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8310 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8311 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8312 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8313 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8314 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8315 | wakeup_ms = tick_add(now_ms, delay); |
| 8316 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8317 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8318 | 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] | 8319 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8320 | } |
| 8321 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8322 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8323 | { |
| 8324 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8325 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8326 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8327 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8328 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8329 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8330 | wakeup_ms = tick_add(now_ms, delay); |
| 8331 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8332 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8333 | 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] | 8334 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8335 | } |
| 8336 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8337 | /* This functionis an LUA binding. it permits to give back |
| 8338 | * the hand at the HAProxy scheduler. It is used when the |
| 8339 | * LUA processing consumes a lot of time. |
| 8340 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8341 | __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] | 8342 | { |
| 8343 | return 0; |
| 8344 | } |
| 8345 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8346 | __LJMP static int hlua_yield(lua_State *L) |
| 8347 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8348 | 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] | 8349 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8350 | } |
| 8351 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8352 | /* This function change the nice of the currently executed |
| 8353 | * task. It is used set low or high priority at the current |
| 8354 | * task. |
| 8355 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8356 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8357 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8358 | struct hlua *hlua; |
| 8359 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8360 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8361 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8362 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8363 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8364 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 8365 | hlua = hlua_gethlua(L); |
| 8366 | |
| 8367 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8368 | if (!hlua || !hlua->task) |
| 8369 | return 0; |
| 8370 | |
| 8371 | if (nice < -1024) |
| 8372 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8373 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8374 | nice = 1024; |
| 8375 | |
| 8376 | hlua->task->nice = nice; |
| 8377 | return 0; |
| 8378 | } |
| 8379 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8380 | /* 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] | 8381 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 8382 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 8383 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8384 | * |
| 8385 | * Task wrapper are longjmp safe because the only one Lua code |
| 8386 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8387 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 8388 | 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] | 8389 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 8390 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8391 | enum hlua_exec status; |
| 8392 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 8393 | if (task->thread_mask == MAX_THREADS_MASK) |
| 8394 | task_set_affinity(task, tid_bit); |
| 8395 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8396 | /* If it is the first call to the task, we must initialize the |
| 8397 | * execution timeouts. |
| 8398 | */ |
| 8399 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 8400 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8401 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8402 | /* Execute the Lua code. */ |
| 8403 | status = hlua_ctx_resume(hlua, 1); |
| 8404 | |
| 8405 | switch (status) { |
| 8406 | /* finished or yield */ |
| 8407 | case HLUA_E_OK: |
| 8408 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8409 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 8410 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8411 | break; |
| 8412 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 8413 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 8414 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 8415 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8416 | break; |
| 8417 | |
| 8418 | /* finished with error. */ |
| 8419 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8420 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8421 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8422 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8423 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8424 | break; |
| 8425 | |
| 8426 | case HLUA_E_ERR: |
| 8427 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8428 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8429 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8430 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8431 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8432 | break; |
| 8433 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8434 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8435 | } |
| 8436 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8437 | /* This function is an LUA binding that register LUA function to be |
| 8438 | * executed after the HAProxy configuration parsing and before the |
| 8439 | * HAProxy scheduler starts. This function expect only one LUA |
| 8440 | * argument that is a function. This function returns nothing, but |
| 8441 | * throws if an error is encountered. |
| 8442 | */ |
| 8443 | __LJMP static int hlua_register_init(lua_State *L) |
| 8444 | { |
| 8445 | struct hlua_init_function *init; |
| 8446 | int ref; |
| 8447 | |
| 8448 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 8449 | |
| 8450 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8451 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8452 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8453 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8454 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8455 | |
| 8456 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 8457 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8458 | return 0; |
| 8459 | } |
| 8460 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8461 | /* This functio is an LUA binding. It permits to register a task |
| 8462 | * executed in parallel of the main HAroxy activity. The task is |
| 8463 | * created and it is set in the HAProxy scheduler. It can be called |
| 8464 | * from the "init" section, "post init" or during the runtime. |
| 8465 | * |
| 8466 | * Lua prototype: |
| 8467 | * |
| 8468 | * <none> core.register_task(<function>) |
| 8469 | */ |
| 8470 | static int hlua_register_task(lua_State *L) |
| 8471 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8472 | struct hlua *hlua = NULL; |
| 8473 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8474 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8475 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8476 | |
| 8477 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 8478 | |
| 8479 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8480 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8481 | /* Get the reference state. If the reference is NULL, L is the master |
| 8482 | * state, otherwise hlua->T is. |
| 8483 | */ |
| 8484 | hlua = hlua_gethlua(L); |
| 8485 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8486 | /* we are in runtime processing */ |
| 8487 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8488 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8489 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8490 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8491 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8492 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8493 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8494 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8495 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8496 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8497 | /* We are in the common lua state, execute the task anywhere, |
| 8498 | * otherwise, inherit the current thread identifier |
| 8499 | */ |
| 8500 | if (state_id == 0) |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8501 | task = task_new_anywhere(); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8502 | else |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8503 | task = task_new_here(); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8504 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8505 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8506 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8507 | task->context = hlua; |
| 8508 | task->process = hlua_process_task; |
| 8509 | |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8510 | if (!hlua_ctx_init(hlua, state_id, task, 1)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8511 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8512 | |
| 8513 | /* Restore the function in the stack. */ |
| 8514 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 8515 | hlua->nargs = 0; |
| 8516 | |
| 8517 | /* Schedule task. */ |
Willy Tarreau | e3957f8 | 2021-09-30 16:17:37 +0200 | [diff] [blame] | 8518 | task_wakeup(task, TASK_WOKEN_INIT); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8519 | |
| 8520 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8521 | |
| 8522 | alloc_error: |
| 8523 | task_destroy(task); |
| 8524 | hlua_ctx_destroy(hlua); |
| 8525 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8526 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8527 | } |
| 8528 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8529 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 8530 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 8531 | * resume converters. |
| 8532 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8533 | 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] | 8534 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8535 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8536 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8537 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8538 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8539 | if (!stream) |
| 8540 | return 0; |
| 8541 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8542 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8543 | * Lua context can be not initialized. This behavior |
| 8544 | * permits to save performances because a systematic |
| 8545 | * Lua initialization cause 5% performances loss. |
| 8546 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8547 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8548 | struct hlua *hlua; |
| 8549 | |
| 8550 | hlua = pool_alloc(pool_head_hlua); |
| 8551 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8552 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8553 | return 0; |
| 8554 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8555 | HLUA_INIT(hlua); |
| 8556 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8557 | 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] | 8558 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8559 | return 0; |
| 8560 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8561 | } |
| 8562 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8563 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8564 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8565 | |
| 8566 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8567 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8568 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8569 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8570 | else |
| 8571 | error = "critical error"; |
| 8572 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8573 | return 0; |
| 8574 | } |
| 8575 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8576 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8577 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8578 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8579 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8580 | return 0; |
| 8581 | } |
| 8582 | |
| 8583 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8584 | 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] | 8585 | |
| 8586 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8587 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8588 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8589 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8590 | return 0; |
| 8591 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8592 | hlua_smp2lua(stream->hlua->T, smp); |
| 8593 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8594 | |
| 8595 | /* push keywords in the stack. */ |
| 8596 | if (arg_p) { |
| 8597 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8598 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8599 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8600 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8601 | return 0; |
| 8602 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8603 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8604 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8605 | } |
| 8606 | } |
| 8607 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8608 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8609 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8610 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8611 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8612 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8613 | } |
| 8614 | |
| 8615 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8616 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8617 | /* finished. */ |
| 8618 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8619 | /* If the stack is empty, the function fails. */ |
| 8620 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8621 | return 0; |
| 8622 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8623 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8624 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8625 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8626 | return 1; |
| 8627 | |
| 8628 | /* yield. */ |
| 8629 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8630 | 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] | 8631 | return 0; |
| 8632 | |
| 8633 | /* finished with error. */ |
| 8634 | case HLUA_E_ERRMSG: |
| 8635 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8636 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8637 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8638 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8639 | return 0; |
| 8640 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8641 | case HLUA_E_ETMOUT: |
| 8642 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 8643 | return 0; |
| 8644 | |
| 8645 | case HLUA_E_NOMEM: |
| 8646 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 8647 | return 0; |
| 8648 | |
| 8649 | case HLUA_E_YIELD: |
| 8650 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 8651 | return 0; |
| 8652 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8653 | case HLUA_E_ERR: |
| 8654 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8655 | 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] | 8656 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8657 | |
| 8658 | default: |
| 8659 | return 0; |
| 8660 | } |
| 8661 | } |
| 8662 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8663 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 8664 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8665 | * resume sample-fetches. This function will be called by the sample |
| 8666 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8667 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8668 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 8669 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8670 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8671 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8672 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8673 | const char *error; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 8674 | unsigned int hflags = HLUA_TXN_NOTERM | HLUA_TXN_SMP_CTX; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8675 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8676 | if (!stream) |
| 8677 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8678 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8679 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8680 | * Lua context can be not initialized. This behavior |
| 8681 | * permits to save performances because a systematic |
| 8682 | * Lua initialization cause 5% performances loss. |
| 8683 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8684 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8685 | struct hlua *hlua; |
| 8686 | |
| 8687 | hlua = pool_alloc(pool_head_hlua); |
| 8688 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8689 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8690 | return 0; |
| 8691 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8692 | hlua->T = NULL; |
| 8693 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8694 | 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] | 8695 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8696 | return 0; |
| 8697 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8698 | } |
| 8699 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8700 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8701 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8702 | |
| 8703 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8704 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8705 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8706 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8707 | else |
| 8708 | error = "critical error"; |
| 8709 | 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] | 8710 | return 0; |
| 8711 | } |
| 8712 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8713 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8714 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8715 | 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] | 8716 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8717 | return 0; |
| 8718 | } |
| 8719 | |
| 8720 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8721 | 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] | 8722 | |
| 8723 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 8724 | 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] | 8725 | 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] | 8726 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8727 | return 0; |
| 8728 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8729 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8730 | |
| 8731 | /* push keywords in the stack. */ |
| 8732 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 8733 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8734 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8735 | 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] | 8736 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8737 | return 0; |
| 8738 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8739 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8740 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8741 | } |
| 8742 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8743 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8744 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8745 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8746 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8747 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8748 | } |
| 8749 | |
| 8750 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8751 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8752 | /* finished. */ |
| 8753 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8754 | /* If the stack is empty, the function fails. */ |
| 8755 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8756 | return 0; |
| 8757 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8758 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8759 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8760 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8761 | |
| 8762 | /* Set the end of execution flag. */ |
| 8763 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 8764 | return 1; |
| 8765 | |
| 8766 | /* yield. */ |
| 8767 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8768 | 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] | 8769 | return 0; |
| 8770 | |
| 8771 | /* finished with error. */ |
| 8772 | case HLUA_E_ERRMSG: |
| 8773 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8774 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8775 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8776 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8777 | return 0; |
| 8778 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8779 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8780 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 8781 | return 0; |
| 8782 | |
| 8783 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8784 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 8785 | return 0; |
| 8786 | |
| 8787 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8788 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 8789 | return 0; |
| 8790 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8791 | case HLUA_E_ERR: |
| 8792 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8793 | 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] | 8794 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8795 | |
| 8796 | default: |
| 8797 | return 0; |
| 8798 | } |
| 8799 | } |
| 8800 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8801 | /* This function is an LUA binding used for registering |
| 8802 | * "sample-conv" functions. It expects a converter name used |
| 8803 | * in the haproxy configuration file, and an LUA function. |
| 8804 | */ |
| 8805 | __LJMP static int hlua_register_converters(lua_State *L) |
| 8806 | { |
| 8807 | struct sample_conv_kw_list *sck; |
| 8808 | const char *name; |
| 8809 | int ref; |
| 8810 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8811 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8812 | struct sample_conv *sc; |
| 8813 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8814 | |
| 8815 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 8816 | |
| 8817 | /* First argument : converter name. */ |
| 8818 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8819 | |
| 8820 | /* Second argument : lua function. */ |
| 8821 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 8822 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8823 | /* Check if the converter is already registered */ |
| 8824 | trash = get_trash_chunk(); |
| 8825 | chunk_printf(trash, "lua.%s", name); |
| 8826 | sc = find_sample_conv(trash->area, trash->data); |
| 8827 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8828 | fcn = sc->private; |
| 8829 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8830 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 8831 | "This will become a hard error in version 2.5.\n", name); |
| 8832 | } |
| 8833 | fcn->function_ref[hlua_state_id] = ref; |
| 8834 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8835 | } |
| 8836 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8837 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8838 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8839 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8840 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8841 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8842 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8843 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8844 | |
| 8845 | /* Fill fcn. */ |
| 8846 | fcn->name = strdup(name); |
| 8847 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8848 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8849 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8850 | |
| 8851 | /* List head */ |
| 8852 | sck->list.n = sck->list.p = NULL; |
| 8853 | |
| 8854 | /* converter keyword. */ |
| 8855 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8856 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8857 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8858 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8859 | |
| 8860 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 8861 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 8862 | 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] | 8863 | sck->kw[0].val_args = NULL; |
| 8864 | sck->kw[0].in_type = SMP_T_STR; |
| 8865 | sck->kw[0].out_type = SMP_T_STR; |
| 8866 | sck->kw[0].private = fcn; |
| 8867 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8868 | /* Register this new converter */ |
| 8869 | sample_register_convs(sck); |
| 8870 | |
| 8871 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8872 | |
| 8873 | alloc_error: |
| 8874 | release_hlua_function(fcn); |
| 8875 | ha_free(&sck); |
| 8876 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8877 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8878 | } |
| 8879 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8880 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8881 | * "sample-fetch" functions. It expects a converter name used |
| 8882 | * in the haproxy configuration file, and an LUA function. |
| 8883 | */ |
| 8884 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 8885 | { |
| 8886 | const char *name; |
| 8887 | int ref; |
| 8888 | int len; |
| 8889 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8890 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8891 | struct sample_fetch *sf; |
| 8892 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8893 | |
| 8894 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 8895 | |
| 8896 | /* First argument : sample-fetch name. */ |
| 8897 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8898 | |
| 8899 | /* Second argument : lua function. */ |
| 8900 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 8901 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8902 | /* Check if the sample-fetch is already registered */ |
| 8903 | trash = get_trash_chunk(); |
| 8904 | chunk_printf(trash, "lua.%s", name); |
| 8905 | sf = find_sample_fetch(trash->area, trash->data); |
| 8906 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8907 | fcn = sf->private; |
| 8908 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8909 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 8910 | "This will become a hard error in version 2.5.\n", name); |
| 8911 | } |
| 8912 | fcn->function_ref[hlua_state_id] = ref; |
| 8913 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8914 | } |
| 8915 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8916 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8917 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8918 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8919 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8920 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8921 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8922 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8923 | |
| 8924 | /* Fill fcn. */ |
| 8925 | fcn->name = strdup(name); |
| 8926 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8927 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8928 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8929 | |
| 8930 | /* List head */ |
| 8931 | sfk->list.n = sfk->list.p = NULL; |
| 8932 | |
| 8933 | /* sample-fetch keyword. */ |
| 8934 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8935 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8936 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8937 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8938 | |
| 8939 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 8940 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 8941 | 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] | 8942 | sfk->kw[0].val_args = NULL; |
| 8943 | sfk->kw[0].out_type = SMP_T_STR; |
| 8944 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 8945 | sfk->kw[0].val = 0; |
| 8946 | sfk->kw[0].private = fcn; |
| 8947 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8948 | /* Register this new fetch. */ |
| 8949 | sample_register_fetches(sfk); |
| 8950 | |
| 8951 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8952 | |
| 8953 | alloc_error: |
| 8954 | release_hlua_function(fcn); |
| 8955 | ha_free(&sfk); |
| 8956 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8957 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8958 | } |
| 8959 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 8960 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8961 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 8962 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8963 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8964 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8965 | unsigned int delay; |
| 8966 | unsigned int wakeup_ms; |
| 8967 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8968 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 8969 | hlua = hlua_gethlua(L); |
| 8970 | if (!hlua) { |
| 8971 | return 0; |
| 8972 | } |
| 8973 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8974 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 8975 | |
| 8976 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 8977 | wakeup_ms = tick_add(now_ms, delay); |
| 8978 | hlua->wake_time = wakeup_ms; |
| 8979 | return 0; |
| 8980 | } |
| 8981 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8982 | /* This function is a wrapper to execute each LUA function declared as an action |
| 8983 | * wrapper during the initialisation period. This function may return any |
| 8984 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 8985 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 8986 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8987 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 8988 | 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] | 8989 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8990 | { |
| 8991 | char **arg; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 8992 | unsigned int hflags = HLUA_TXN_ACT_CTX; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8993 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8994 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 8995 | |
| 8996 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8997 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 8998 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 8999 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 9000 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9001 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9002 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9003 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9004 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9005 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9006 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9007 | * Lua context can be not initialized. This behavior |
| 9008 | * permits to save performances because a systematic |
| 9009 | * Lua initialization cause 5% performances loss. |
| 9010 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9011 | if (!s->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9012 | struct hlua *hlua; |
| 9013 | |
| 9014 | hlua = pool_alloc(pool_head_hlua); |
| 9015 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9016 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9017 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9018 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9019 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9020 | HLUA_INIT(hlua); |
| 9021 | s->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9022 | 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] | 9023 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9024 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9025 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9026 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9027 | } |
| 9028 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9029 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9030 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9031 | |
| 9032 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9033 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9034 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 9035 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9036 | else |
| 9037 | error = "critical error"; |
| 9038 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9039 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9040 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9041 | } |
| 9042 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9043 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9044 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9045 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9046 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9047 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9048 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9049 | } |
| 9050 | |
| 9051 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9052 | 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] | 9053 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9054 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 9055 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9056 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9057 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9058 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9059 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9060 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9061 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9062 | |
| 9063 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9064 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9065 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9066 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9067 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9068 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9069 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9070 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9071 | lua_pushstring(s->hlua->T, *arg); |
| 9072 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9073 | } |
| 9074 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9075 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9076 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9077 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9078 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9079 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9080 | } |
| 9081 | |
| 9082 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 9083 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9084 | /* finished. */ |
| 9085 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9086 | /* Catch the return value */ |
| 9087 | if (lua_gettop(s->hlua->T) > 0) |
| 9088 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9089 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9090 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 9091 | if (act_ret == ACT_RET_YIELD) { |
| 9092 | if (flags & ACT_OPT_FINAL) |
| 9093 | goto err_yield; |
| 9094 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9095 | if (dir == SMP_OPT_DIR_REQ) |
| 9096 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 9097 | s->hlua->wake_time); |
| 9098 | else |
| 9099 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 9100 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9101 | } |
| 9102 | goto end; |
| 9103 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9104 | /* yield. */ |
| 9105 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 9106 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9107 | if (dir == SMP_OPT_DIR_REQ) |
| 9108 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 9109 | s->hlua->wake_time); |
| 9110 | else |
| 9111 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 9112 | s->hlua->wake_time); |
| 9113 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9114 | /* Some actions can be wake up when a "write" event |
| 9115 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9116 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9117 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 9118 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9119 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9120 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9121 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9122 | act_ret = ACT_RET_YIELD; |
| 9123 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9124 | |
| 9125 | /* finished with error. */ |
| 9126 | case HLUA_E_ERRMSG: |
| 9127 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9128 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9129 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9130 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9131 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9132 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9133 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9134 | 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] | 9135 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9136 | |
| 9137 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9138 | 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] | 9139 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9140 | |
| 9141 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 9142 | err_yield: |
| 9143 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9144 | 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] | 9145 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9146 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9147 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9148 | case HLUA_E_ERR: |
| 9149 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9150 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9151 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9152 | |
| 9153 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9154 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9155 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9156 | |
| 9157 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 9158 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9159 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9160 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9161 | } |
| 9162 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 9163 | 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] | 9164 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 9165 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9166 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9167 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 9168 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 9169 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9170 | } |
| 9171 | |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9172 | static int hlua_applet_tcp_init(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9173 | { |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 9174 | struct conn_stream *cs = ctx->owner; |
| 9175 | struct stream *strm = __cs_strm(cs); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9176 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9177 | struct task *task; |
| 9178 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9179 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9180 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9181 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9182 | if (!hlua) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9183 | SEND_ERR(strm->be, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9184 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9185 | return 0; |
| 9186 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9187 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9188 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9189 | ctx->ctx.hlua_apptcp.flags = 0; |
| 9190 | |
| 9191 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 9192 | task = task_new_here(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9193 | if (!task) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9194 | SEND_ERR(strm->be, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9195 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9196 | return 0; |
| 9197 | } |
| 9198 | task->nice = 0; |
| 9199 | task->context = ctx; |
| 9200 | task->process = hlua_applet_wakeup; |
| 9201 | ctx->ctx.hlua_apptcp.task = task; |
| 9202 | |
| 9203 | /* In the execution wrappers linked with a stream, the |
| 9204 | * Lua context can be not initialized. This behavior |
| 9205 | * permits to save performances because a systematic |
| 9206 | * Lua initialization cause 5% performances loss. |
| 9207 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9208 | 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] | 9209 | 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] | 9210 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9211 | return 0; |
| 9212 | } |
| 9213 | |
| 9214 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 9215 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9216 | |
| 9217 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9218 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9219 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9220 | error = lua_tostring(hlua->T, -1); |
| 9221 | else |
| 9222 | error = "critical error"; |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9223 | SEND_ERR(strm->be, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9224 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9225 | return 0; |
| 9226 | } |
| 9227 | |
| 9228 | /* Check stack available size. */ |
| 9229 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9230 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9231 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9232 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9233 | return 0; |
| 9234 | } |
| 9235 | |
| 9236 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9237 | 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] | 9238 | |
| 9239 | /* Create and and push object stream in the stack. */ |
| 9240 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9241 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9242 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9243 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9244 | return 0; |
| 9245 | } |
| 9246 | hlua->nargs = 1; |
| 9247 | |
| 9248 | /* push keywords in the stack. */ |
| 9249 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 9250 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9251 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9252 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9253 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9254 | return 0; |
| 9255 | } |
| 9256 | lua_pushstring(hlua->T, *arg); |
| 9257 | hlua->nargs++; |
| 9258 | } |
| 9259 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9260 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9261 | |
| 9262 | /* Wakeup the applet ASAP. */ |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 9263 | cs_cant_get(cs); |
| 9264 | cs_rx_endp_more(cs); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9265 | |
| 9266 | return 1; |
| 9267 | } |
| 9268 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 9269 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9270 | { |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 9271 | struct conn_stream *cs = ctx->owner; |
| 9272 | struct stream *strm = __cs_strm(cs); |
| 9273 | struct channel *res = cs_ic(cs); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9274 | struct act_rule *rule = ctx->rule; |
| 9275 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9276 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9277 | |
| 9278 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 9279 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 9280 | /* eat the whole request */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 9281 | co_skip(cs_oc(cs), co_data(cs_oc(cs))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9282 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 9283 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9284 | |
| 9285 | /* If the stream is disconnect or closed, ldo nothing. */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 9286 | if (unlikely(cs->state == CS_ST_DIS || cs->state == CS_ST_CLO)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9287 | return; |
| 9288 | |
| 9289 | /* Execute the function. */ |
| 9290 | switch (hlua_ctx_resume(hlua, 1)) { |
| 9291 | /* finished. */ |
| 9292 | case HLUA_E_OK: |
| 9293 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 9294 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9295 | /* eat the whole request */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 9296 | co_skip(cs_oc(cs), co_data(cs_oc(cs))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9297 | res->flags |= CF_READ_NULL; |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame] | 9298 | cs_shutr(cs); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9299 | return; |
| 9300 | |
| 9301 | /* yield. */ |
| 9302 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 9303 | if (hlua->wake_time != TICK_ETERNITY) |
| 9304 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9305 | return; |
| 9306 | |
| 9307 | /* finished with error. */ |
| 9308 | case HLUA_E_ERRMSG: |
| 9309 | /* Display log. */ |
| 9310 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9311 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9312 | lua_pop(hlua->T, 1); |
| 9313 | goto error; |
| 9314 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9315 | case HLUA_E_ETMOUT: |
| 9316 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9317 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9318 | goto error; |
| 9319 | |
| 9320 | case HLUA_E_NOMEM: |
| 9321 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9322 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9323 | goto error; |
| 9324 | |
| 9325 | case HLUA_E_YIELD: /* unexpected */ |
| 9326 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9327 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9328 | goto error; |
| 9329 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9330 | case HLUA_E_ERR: |
| 9331 | /* Display log. */ |
| 9332 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9333 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9334 | goto error; |
| 9335 | |
| 9336 | default: |
| 9337 | goto error; |
| 9338 | } |
| 9339 | |
| 9340 | error: |
| 9341 | |
| 9342 | /* For all other cases, just close the stream. */ |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame] | 9343 | cs_shutw(cs); |
| 9344 | cs_shutr(cs); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9345 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 9346 | } |
| 9347 | |
| 9348 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 9349 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 9350 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9351 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9352 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9353 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9354 | } |
| 9355 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9356 | /* The function returns 1 if the initialisation is complete, 0 if |
| 9357 | * an errors occurs and -1 if more data are required for initializing |
| 9358 | * the applet. |
| 9359 | */ |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9360 | static int hlua_applet_http_init(struct appctx *ctx) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9361 | { |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 9362 | struct conn_stream *cs = ctx->owner; |
| 9363 | struct stream *strm = __cs_strm(cs); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9364 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9365 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9366 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9367 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9368 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9369 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9370 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9371 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9372 | if (!hlua) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9373 | SEND_ERR(strm->be, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9374 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9375 | return 0; |
| 9376 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9377 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9378 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9379 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 9380 | ctx->ctx.hlua_apphttp.flags = 0; |
| 9381 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 9382 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 9383 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 9384 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9385 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 9386 | task = task_new_here(); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9387 | if (!task) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9388 | SEND_ERR(strm->be, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9389 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9390 | return 0; |
| 9391 | } |
| 9392 | task->nice = 0; |
| 9393 | task->context = ctx; |
| 9394 | task->process = hlua_applet_wakeup; |
| 9395 | ctx->ctx.hlua_apphttp.task = task; |
| 9396 | |
| 9397 | /* In the execution wrappers linked with a stream, the |
| 9398 | * Lua context can be not initialized. This behavior |
| 9399 | * permits to save performances because a systematic |
| 9400 | * Lua initialization cause 5% performances loss. |
| 9401 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9402 | 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] | 9403 | 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] | 9404 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9405 | return 0; |
| 9406 | } |
| 9407 | |
| 9408 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 9409 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9410 | |
| 9411 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9412 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9413 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9414 | error = lua_tostring(hlua->T, -1); |
| 9415 | else |
| 9416 | error = "critical error"; |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9417 | SEND_ERR(strm->be, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9418 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9419 | return 0; |
| 9420 | } |
| 9421 | |
| 9422 | /* Check stack available size. */ |
| 9423 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9424 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9425 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9426 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9427 | return 0; |
| 9428 | } |
| 9429 | |
| 9430 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9431 | 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] | 9432 | |
| 9433 | /* Create and and push object stream in the stack. */ |
| 9434 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9435 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9436 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9437 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9438 | return 0; |
| 9439 | } |
| 9440 | hlua->nargs = 1; |
| 9441 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9442 | /* push keywords in the stack. */ |
| 9443 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 9444 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9445 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9446 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9447 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9448 | return 0; |
| 9449 | } |
| 9450 | lua_pushstring(hlua->T, *arg); |
| 9451 | hlua->nargs++; |
| 9452 | } |
| 9453 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9454 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9455 | |
| 9456 | /* Wakeup the applet when data is ready for read. */ |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 9457 | cs_cant_get(cs); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9458 | |
| 9459 | return 1; |
| 9460 | } |
| 9461 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 9462 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9463 | { |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 9464 | struct conn_stream *cs = ctx->owner; |
| 9465 | struct stream *strm = __cs_strm(cs); |
| 9466 | struct channel *req = cs_oc(cs); |
| 9467 | struct channel *res = cs_ic(cs); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9468 | struct act_rule *rule = ctx->rule; |
| 9469 | struct proxy *px = strm->be; |
| 9470 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 9471 | struct htx *req_htx, *res_htx; |
| 9472 | |
| 9473 | res_htx = htx_from_buf(&res->buf); |
| 9474 | |
| 9475 | /* If the stream is disconnect or closed, ldo nothing. */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 9476 | if (unlikely(cs->state == CS_ST_DIS || cs->state == CS_ST_CLO)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9477 | goto out; |
| 9478 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9479 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9480 | if (!b_size(&res->buf)) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 9481 | cs_rx_room_blk(cs); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9482 | goto out; |
| 9483 | } |
| 9484 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9485 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9486 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 9487 | |
| 9488 | /* Set the currently running flag. */ |
| 9489 | if (!HLUA_IS_RUNNING(hlua) && |
| 9490 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 9491 | if (!co_data(req)) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 9492 | cs_cant_get(cs); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9493 | goto out; |
| 9494 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9495 | } |
| 9496 | |
| 9497 | /* Executes The applet if it is not done. */ |
| 9498 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 9499 | |
| 9500 | /* Execute the function. */ |
| 9501 | switch (hlua_ctx_resume(hlua, 1)) { |
| 9502 | /* finished. */ |
| 9503 | case HLUA_E_OK: |
| 9504 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 9505 | break; |
| 9506 | |
| 9507 | /* yield. */ |
| 9508 | case HLUA_E_AGAIN: |
| 9509 | if (hlua->wake_time != TICK_ETERNITY) |
| 9510 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9511 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9512 | |
| 9513 | /* finished with error. */ |
| 9514 | case HLUA_E_ERRMSG: |
| 9515 | /* Display log. */ |
| 9516 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9517 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9518 | lua_pop(hlua->T, 1); |
| 9519 | goto error; |
| 9520 | |
| 9521 | case HLUA_E_ETMOUT: |
| 9522 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9523 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9524 | goto error; |
| 9525 | |
| 9526 | case HLUA_E_NOMEM: |
| 9527 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9528 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9529 | goto error; |
| 9530 | |
| 9531 | case HLUA_E_YIELD: /* unexpected */ |
| 9532 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9533 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9534 | goto error; |
| 9535 | |
| 9536 | case HLUA_E_ERR: |
| 9537 | /* Display log. */ |
| 9538 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9539 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9540 | goto error; |
| 9541 | |
| 9542 | default: |
| 9543 | goto error; |
| 9544 | } |
| 9545 | } |
| 9546 | |
| 9547 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9548 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 9549 | goto done; |
| 9550 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9551 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 9552 | goto error; |
| 9553 | |
Christopher Faulet | f89af9c | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 9554 | /* no more data are expected. If the response buffer is empty |
| 9555 | * for a chunked message, be sure to add something (EOT block in |
| 9556 | * this case) to have something to send. It is important to be |
| 9557 | * sure the EOM flags will be handled by the endpoint. |
| 9558 | */ |
| 9559 | if (htx_is_empty(res_htx) && (strm->txn->rsp.flags & (HTTP_MSGF_XFER_LEN|HTTP_MSGF_CNT_LEN)) == HTTP_MSGF_XFER_LEN) { |
| 9560 | if (!htx_add_endof(res_htx, HTX_BLK_EOT)) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 9561 | cs_rx_room_blk(cs); |
Christopher Faulet | f89af9c | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 9562 | goto out; |
| 9563 | } |
| 9564 | channel_add_input(res, 1); |
| 9565 | } |
| 9566 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 9567 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 9568 | cs->endp->flags |= CS_EP_EOI; |
Christopher Faulet | d8d2708 | 2022-03-07 15:50:54 +0100 | [diff] [blame] | 9569 | res->flags |= CF_EOI; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9570 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 9571 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9572 | } |
| 9573 | |
| 9574 | done: |
| 9575 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9576 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9577 | res->flags |= CF_READ_NULL; |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame] | 9578 | cs_shutr(cs); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9579 | } |
| 9580 | |
| 9581 | /* eat the whole request */ |
| 9582 | if (co_data(req)) { |
| 9583 | req_htx = htx_from_buf(&req->buf); |
| 9584 | co_htx_skip(req, req_htx, co_data(req)); |
| 9585 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9586 | } |
| 9587 | } |
| 9588 | |
| 9589 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9590 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9591 | return; |
| 9592 | |
| 9593 | error: |
| 9594 | |
| 9595 | /* If we are in HTTP mode, and we are not send any |
| 9596 | * data, return a 500 server error in best effort: |
| 9597 | * if there is no room available in the buffer, |
| 9598 | * just close the connection. |
| 9599 | */ |
| 9600 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 9601 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9602 | |
| 9603 | channel_erase(res); |
| 9604 | res->buf.data = b_data(err); |
| 9605 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 9606 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 9607 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9608 | } |
| 9609 | if (!(strm->flags & SF_ERR_MASK)) |
| 9610 | strm->flags |= SF_ERR_RESOURCE; |
| 9611 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 9612 | goto done; |
| 9613 | } |
| 9614 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9615 | static void hlua_applet_http_release(struct appctx *ctx) |
| 9616 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 9617 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9618 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9619 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9620 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9621 | } |
| 9622 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9623 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9624 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9625 | * |
| 9626 | * This function can fail with an abort() due to an Lua critical error. |
| 9627 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 9628 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9629 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9630 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 9631 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9632 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9633 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9634 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9635 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9636 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9637 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9638 | if (!rule->arg.hlua_rule) { |
| 9639 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9640 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9641 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9642 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9643 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 9644 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 9645 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9646 | if (!rule->arg.hlua_rule->args) { |
| 9647 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9648 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9649 | } |
| 9650 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9651 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9652 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9653 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9654 | /* Expect some arguments */ |
| 9655 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9656 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9657 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9658 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9659 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9660 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9661 | if (!rule->arg.hlua_rule->args[i]) { |
| 9662 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9663 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9664 | } |
| 9665 | (*cur_arg)++; |
| 9666 | } |
| 9667 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9668 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 9669 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9670 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9671 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9672 | |
| 9673 | error: |
| 9674 | if (rule->arg.hlua_rule) { |
| 9675 | if (rule->arg.hlua_rule->args) { |
| 9676 | for (i = 0; i < fcn->nargs; i++) |
| 9677 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 9678 | ha_free(&rule->arg.hlua_rule->args); |
| 9679 | } |
| 9680 | ha_free(&rule->arg.hlua_rule); |
| 9681 | } |
| 9682 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9683 | } |
| 9684 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9685 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 9686 | struct act_rule *rule, char **err) |
| 9687 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9688 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9689 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9690 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9691 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9692 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9693 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9694 | * the call of this analyzer. |
| 9695 | */ |
| 9696 | if (rule->from != ACT_F_HTTP_REQ) { |
| 9697 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 9698 | return ACT_RET_PRS_ERR; |
| 9699 | } |
| 9700 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9701 | /* Memory for the rule. */ |
| 9702 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 9703 | if (!rule->arg.hlua_rule) { |
| 9704 | memprintf(err, "out of memory error"); |
| 9705 | return ACT_RET_PRS_ERR; |
| 9706 | } |
| 9707 | |
| 9708 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9709 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9710 | |
| 9711 | /* TODO: later accept arguments. */ |
| 9712 | rule->arg.hlua_rule->args = NULL; |
| 9713 | |
| 9714 | /* Add applet pointer in the rule. */ |
| 9715 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 9716 | rule->applet.name = fcn->name; |
| 9717 | rule->applet.init = hlua_applet_http_init; |
| 9718 | rule->applet.fct = hlua_applet_http_fct; |
| 9719 | rule->applet.release = hlua_applet_http_release; |
| 9720 | rule->applet.timeout = hlua_timeout_applet; |
| 9721 | |
| 9722 | return ACT_RET_PRS_OK; |
| 9723 | } |
| 9724 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9725 | /* This function is an LUA binding used for registering |
| 9726 | * "sample-conv" functions. It expects a converter name used |
| 9727 | * in the haproxy configuration file, and an LUA function. |
| 9728 | */ |
| 9729 | __LJMP static int hlua_register_action(lua_State *L) |
| 9730 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9731 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9732 | const char *name; |
| 9733 | int ref; |
| 9734 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9735 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9736 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9737 | struct buffer *trash; |
| 9738 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9739 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9740 | /* Initialise the number of expected arguments at 0. */ |
| 9741 | nargs = 0; |
| 9742 | |
| 9743 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 9744 | 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] | 9745 | |
| 9746 | /* First argument : converter name. */ |
| 9747 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9748 | |
| 9749 | /* Second argument : environment. */ |
| 9750 | if (lua_type(L, 2) != LUA_TTABLE) |
| 9751 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9752 | |
| 9753 | /* Third argument : lua function. */ |
| 9754 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 9755 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9756 | /* 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] | 9757 | if (lua_gettop(L) >= 4) |
| 9758 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 9759 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9760 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9761 | lua_pushnil(L); |
| 9762 | while (lua_next(L, 2) != 0) { |
| 9763 | if (lua_type(L, -1) != LUA_TSTRING) |
| 9764 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9765 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9766 | /* Check if action exists */ |
| 9767 | trash = get_trash_chunk(); |
| 9768 | chunk_printf(trash, "lua.%s", name); |
| 9769 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 9770 | akw = tcp_req_cont_action(trash->area); |
| 9771 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 9772 | akw = tcp_res_cont_action(trash->area); |
| 9773 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 9774 | akw = action_http_req_custom(trash->area); |
| 9775 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 9776 | akw = action_http_res_custom(trash->area); |
| 9777 | } else { |
| 9778 | akw = NULL; |
| 9779 | } |
| 9780 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9781 | fcn = akw->private; |
| 9782 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9783 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 9784 | "This will become a hard error in version 2.5.\n", name); |
| 9785 | } |
| 9786 | fcn->function_ref[hlua_state_id] = ref; |
| 9787 | |
| 9788 | /* pop the environment string. */ |
| 9789 | lua_pop(L, 1); |
| 9790 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9791 | } |
| 9792 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9793 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 9794 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9795 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9796 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9797 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9798 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9799 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9800 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9801 | |
| 9802 | /* Fill fcn. */ |
| 9803 | fcn->name = strdup(name); |
| 9804 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9805 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9806 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9807 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 9808 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9809 | fcn->nargs = nargs; |
| 9810 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9811 | /* List head */ |
| 9812 | akl->list.n = akl->list.p = NULL; |
| 9813 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9814 | /* action keyword. */ |
| 9815 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9816 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9817 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9818 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9819 | |
| 9820 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 9821 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 9822 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9823 | akl->kw[0].private = fcn; |
| 9824 | akl->kw[0].parse = action_register_lua; |
| 9825 | |
| 9826 | /* select the action registering point. */ |
| 9827 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 9828 | tcp_req_cont_keywords_register(akl); |
| 9829 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 9830 | tcp_res_cont_keywords_register(akl); |
| 9831 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 9832 | http_req_keywords_register(akl); |
| 9833 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 9834 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9835 | else { |
| 9836 | release_hlua_function(fcn); |
| 9837 | if (akl) |
| 9838 | ha_free((char **)&(akl->kw[0].kw)); |
| 9839 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 9840 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9841 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 9842 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9843 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9844 | |
| 9845 | /* pop the environment string. */ |
| 9846 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9847 | |
| 9848 | /* reset for next loop */ |
| 9849 | akl = NULL; |
| 9850 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9851 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9852 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9853 | |
| 9854 | alloc_error: |
| 9855 | release_hlua_function(fcn); |
| 9856 | ha_free(&akl); |
| 9857 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9858 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9859 | } |
| 9860 | |
| 9861 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 9862 | struct act_rule *rule, char **err) |
| 9863 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9864 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9865 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 9866 | if (px->mode == PR_MODE_HTTP) { |
| 9867 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 9868 | return ACT_RET_PRS_ERR; |
| 9869 | } |
| 9870 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9871 | /* Memory for the rule. */ |
| 9872 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 9873 | if (!rule->arg.hlua_rule) { |
| 9874 | memprintf(err, "out of memory error"); |
| 9875 | return ACT_RET_PRS_ERR; |
| 9876 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9877 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9878 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9879 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9880 | |
| 9881 | /* TODO: later accept arguments. */ |
| 9882 | rule->arg.hlua_rule->args = NULL; |
| 9883 | |
| 9884 | /* Add applet pointer in the rule. */ |
| 9885 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 9886 | rule->applet.name = fcn->name; |
| 9887 | rule->applet.init = hlua_applet_tcp_init; |
| 9888 | rule->applet.fct = hlua_applet_tcp_fct; |
| 9889 | rule->applet.release = hlua_applet_tcp_release; |
| 9890 | rule->applet.timeout = hlua_timeout_applet; |
| 9891 | |
| 9892 | return 0; |
| 9893 | } |
| 9894 | |
| 9895 | /* This function is an LUA binding used for registering |
| 9896 | * "sample-conv" functions. It expects a converter name used |
| 9897 | * in the haproxy configuration file, and an LUA function. |
| 9898 | */ |
| 9899 | __LJMP static int hlua_register_service(lua_State *L) |
| 9900 | { |
| 9901 | struct action_kw_list *akl; |
| 9902 | const char *name; |
| 9903 | const char *env; |
| 9904 | int ref; |
| 9905 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9906 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9907 | struct buffer *trash; |
| 9908 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9909 | |
| 9910 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 9911 | |
| 9912 | /* First argument : converter name. */ |
| 9913 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9914 | |
| 9915 | /* Second argument : environment. */ |
| 9916 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 9917 | |
| 9918 | /* Third argument : lua function. */ |
| 9919 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 9920 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9921 | /* Check for service already registered */ |
| 9922 | trash = get_trash_chunk(); |
| 9923 | chunk_printf(trash, "lua.%s", name); |
| 9924 | akw = service_find(trash->area); |
| 9925 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9926 | fcn = akw->private; |
| 9927 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9928 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 9929 | "This will become a hard error in version 2.5.\n", name); |
| 9930 | } |
| 9931 | fcn->function_ref[hlua_state_id] = ref; |
| 9932 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9933 | } |
| 9934 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9935 | /* Allocate and fill the sample fetch keyword struct. */ |
| 9936 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 9937 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9938 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9939 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9940 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9941 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9942 | |
| 9943 | /* Fill fcn. */ |
| 9944 | len = strlen("<lua.>") + strlen(name) + 1; |
| 9945 | fcn->name = calloc(1, len); |
| 9946 | if (!fcn->name) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9947 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9948 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9949 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9950 | |
| 9951 | /* List head */ |
| 9952 | akl->list.n = akl->list.p = NULL; |
| 9953 | |
| 9954 | /* converter keyword. */ |
| 9955 | len = strlen("lua.") + strlen(name) + 1; |
| 9956 | akl->kw[0].kw = calloc(1, len); |
| 9957 | if (!akl->kw[0].kw) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9958 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9959 | |
| 9960 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 9961 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 9962 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9963 | if (strcmp(env, "tcp") == 0) |
| 9964 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9965 | else if (strcmp(env, "http") == 0) |
| 9966 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9967 | else { |
| 9968 | release_hlua_function(fcn); |
| 9969 | if (akl) |
| 9970 | ha_free((char **)&(akl->kw[0].kw)); |
| 9971 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 9972 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 9973 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9974 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9975 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 9976 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9977 | akl->kw[0].private = fcn; |
| 9978 | |
| 9979 | /* End of array. */ |
| 9980 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 9981 | |
| 9982 | /* Register this new converter */ |
| 9983 | service_keywords_register(akl); |
| 9984 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9985 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9986 | |
| 9987 | alloc_error: |
| 9988 | release_hlua_function(fcn); |
| 9989 | ha_free(&akl); |
| 9990 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9991 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9992 | } |
| 9993 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9994 | /* This function initialises Lua cli handler. It copies the |
| 9995 | * arguments in the Lua stack and create channel IO objects. |
| 9996 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 9997 | 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] | 9998 | { |
| 9999 | struct hlua *hlua; |
| 10000 | struct hlua_function *fcn; |
| 10001 | int i; |
| 10002 | const char *error; |
| 10003 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10004 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10005 | appctx->ctx.hlua_cli.fcn = private; |
| 10006 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10007 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10008 | if (!hlua) { |
| 10009 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10010 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10011 | } |
| 10012 | HLUA_INIT(hlua); |
| 10013 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10014 | |
| 10015 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 10016 | * 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] | 10017 | * applet_http. It is absolutely compatible. |
| 10018 | */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 10019 | appctx->ctx.hlua_cli.task = task_new_here(); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10020 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 10021 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10022 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10023 | } |
| 10024 | appctx->ctx.hlua_cli.task->nice = 0; |
| 10025 | appctx->ctx.hlua_cli.task->context = appctx; |
| 10026 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 10027 | |
| 10028 | /* Initialises the Lua context */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10029 | if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(fcn), appctx->ctx.hlua_cli.task, 0)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10030 | 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] | 10031 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10032 | } |
| 10033 | |
| 10034 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10035 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10036 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 10037 | error = lua_tostring(hlua->T, -1); |
| 10038 | else |
| 10039 | error = "critical error"; |
| 10040 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 10041 | goto error; |
| 10042 | } |
| 10043 | |
| 10044 | /* Check stack available size. */ |
| 10045 | if (!lua_checkstack(hlua->T, 2)) { |
| 10046 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10047 | goto error; |
| 10048 | } |
| 10049 | |
| 10050 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10051 | 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] | 10052 | |
| 10053 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 10054 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10055 | */ |
| 10056 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 10057 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10058 | goto error; |
| 10059 | } |
| 10060 | hlua->nargs = 1; |
| 10061 | |
| 10062 | /* push keywords in the stack. */ |
| 10063 | for (i = 0; *args[i]; i++) { |
| 10064 | /* Check stack available size. */ |
| 10065 | if (!lua_checkstack(hlua->T, 1)) { |
| 10066 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10067 | goto error; |
| 10068 | } |
| 10069 | lua_pushstring(hlua->T, args[i]); |
| 10070 | hlua->nargs++; |
| 10071 | } |
| 10072 | |
| 10073 | /* We must initialize the execution timeouts. */ |
| 10074 | hlua->max_time = hlua_timeout_session; |
| 10075 | |
| 10076 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10077 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10078 | |
| 10079 | /* It's ok */ |
| 10080 | return 0; |
| 10081 | |
| 10082 | /* It's not ok. */ |
| 10083 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10084 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10085 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10086 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10087 | return 1; |
| 10088 | } |
| 10089 | |
| 10090 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 10091 | { |
| 10092 | struct hlua *hlua; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 10093 | struct conn_stream *cs; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10094 | struct hlua_function *fcn; |
| 10095 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10096 | hlua = appctx->ctx.hlua_cli.hlua; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 10097 | cs = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 10098 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10099 | |
| 10100 | /* If the stream is disconnect or closed, ldo nothing. */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 10101 | 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] | 10102 | return 1; |
| 10103 | |
| 10104 | /* Execute the function. */ |
| 10105 | switch (hlua_ctx_resume(hlua, 1)) { |
| 10106 | |
| 10107 | /* finished. */ |
| 10108 | case HLUA_E_OK: |
| 10109 | return 1; |
| 10110 | |
| 10111 | /* yield. */ |
| 10112 | case HLUA_E_AGAIN: |
| 10113 | /* We want write. */ |
| 10114 | if (HLUA_IS_WAKERESWR(hlua)) |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 10115 | cs_rx_room_blk(cs); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10116 | /* Set the timeout. */ |
| 10117 | if (hlua->wake_time != TICK_ETERNITY) |
| 10118 | task_schedule(hlua->task, hlua->wake_time); |
| 10119 | return 0; |
| 10120 | |
| 10121 | /* finished with error. */ |
| 10122 | case HLUA_E_ERRMSG: |
| 10123 | /* Display log. */ |
| 10124 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 10125 | fcn->name, lua_tostring(hlua->T, -1)); |
| 10126 | lua_pop(hlua->T, 1); |
| 10127 | return 1; |
| 10128 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10129 | case HLUA_E_ETMOUT: |
| 10130 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 10131 | fcn->name); |
| 10132 | return 1; |
| 10133 | |
| 10134 | case HLUA_E_NOMEM: |
| 10135 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 10136 | fcn->name); |
| 10137 | return 1; |
| 10138 | |
| 10139 | case HLUA_E_YIELD: /* unexpected */ |
| 10140 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 10141 | fcn->name); |
| 10142 | return 1; |
| 10143 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10144 | case HLUA_E_ERR: |
| 10145 | /* Display log. */ |
| 10146 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 10147 | fcn->name); |
| 10148 | return 1; |
| 10149 | |
| 10150 | default: |
| 10151 | return 1; |
| 10152 | } |
| 10153 | |
| 10154 | return 1; |
| 10155 | } |
| 10156 | |
| 10157 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 10158 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10159 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10160 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10161 | } |
| 10162 | |
| 10163 | /* This function is an LUA binding used for registering |
| 10164 | * new keywords in the cli. It expects a list of keywords |
| 10165 | * which are the "path". It is limited to 5 keywords. A |
| 10166 | * description of the command, a function to be executed |
| 10167 | * for the parsing and a function for io handlers. |
| 10168 | */ |
| 10169 | __LJMP static int hlua_register_cli(lua_State *L) |
| 10170 | { |
| 10171 | struct cli_kw_list *cli_kws; |
| 10172 | const char *message; |
| 10173 | int ref_io; |
| 10174 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10175 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10176 | int index; |
| 10177 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10178 | struct buffer *trash; |
| 10179 | const char *kw[5]; |
| 10180 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10181 | const char *errmsg; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10182 | |
| 10183 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 10184 | |
| 10185 | /* First argument : an array of maximum 5 keywords. */ |
| 10186 | if (!lua_istable(L, 1)) |
| 10187 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 10188 | |
| 10189 | /* Second argument : string with contextual message. */ |
| 10190 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 10191 | |
| 10192 | /* Third and fourth argument : lua function. */ |
| 10193 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 10194 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10195 | /* Check for CLI service already registered */ |
| 10196 | trash = get_trash_chunk(); |
| 10197 | index = 0; |
| 10198 | lua_pushnil(L); |
| 10199 | memset(kw, 0, sizeof(kw)); |
| 10200 | while (lua_next(L, 1) != 0) { |
| 10201 | if (index >= CLI_PREFIX_KW_NB) |
| 10202 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 10203 | if (lua_type(L, -1) != LUA_TSTRING) |
| 10204 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 10205 | kw[index] = lua_tostring(L, -1); |
| 10206 | if (index == 0) |
| 10207 | chunk_printf(trash, "%s", kw[index]); |
| 10208 | else |
| 10209 | chunk_appendf(trash, " %s", kw[index]); |
| 10210 | index++; |
| 10211 | lua_pop(L, 1); |
| 10212 | } |
| 10213 | cli_kw = cli_find_kw_exact((char **)kw); |
| 10214 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10215 | fcn = cli_kw->private; |
| 10216 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 10217 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 10218 | "This will become a hard error in version 2.5.\n", trash->area); |
| 10219 | } |
| 10220 | fcn->function_ref[hlua_state_id] = ref_io; |
| 10221 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10222 | } |
| 10223 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10224 | /* Allocate and fill the sample fetch keyword struct. */ |
| 10225 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10226 | if (!cli_kws) { |
| 10227 | errmsg = "Lua out of memory error."; |
| 10228 | goto error; |
| 10229 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10230 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10231 | if (!fcn) { |
| 10232 | errmsg = "Lua out of memory error."; |
| 10233 | goto error; |
| 10234 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10235 | |
| 10236 | /* Fill path. */ |
| 10237 | index = 0; |
| 10238 | lua_pushnil(L); |
| 10239 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10240 | if (index >= 5) { |
| 10241 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 10242 | goto error; |
| 10243 | } |
| 10244 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 10245 | errmsg = "1st argument must be a table filled with strings"; |
| 10246 | goto error; |
| 10247 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10248 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10249 | if (!cli_kws->kw[0].str_kw[index]) { |
| 10250 | errmsg = "Lua out of memory error."; |
| 10251 | goto error; |
| 10252 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10253 | index++; |
| 10254 | lua_pop(L, 1); |
| 10255 | } |
| 10256 | |
| 10257 | /* Copy help message. */ |
| 10258 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10259 | if (!cli_kws->kw[0].usage) { |
| 10260 | errmsg = "Lua out of memory error."; |
| 10261 | goto error; |
| 10262 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10263 | |
| 10264 | /* Fill fcn io handler. */ |
| 10265 | len = strlen("<lua.cli>") + 1; |
| 10266 | for (i = 0; i < index; i++) |
| 10267 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 10268 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10269 | if (!fcn->name) { |
| 10270 | errmsg = "Lua out of memory error."; |
| 10271 | goto error; |
| 10272 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10273 | strncat((char *)fcn->name, "<lua.cli", len); |
| 10274 | for (i = 0; i < index; i++) { |
| 10275 | strncat((char *)fcn->name, ".", len); |
| 10276 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 10277 | } |
| 10278 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10279 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10280 | |
| 10281 | /* Fill last entries. */ |
| 10282 | cli_kws->kw[0].private = fcn; |
| 10283 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 10284 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 10285 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 10286 | |
| 10287 | /* Register this new converter */ |
| 10288 | cli_register_kw(cli_kws); |
| 10289 | |
| 10290 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10291 | |
| 10292 | error: |
| 10293 | release_hlua_function(fcn); |
| 10294 | if (cli_kws) { |
| 10295 | for (i = 0; i < index; i++) |
| 10296 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 10297 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 10298 | } |
| 10299 | ha_free(&cli_kws); |
| 10300 | WILL_LJMP(luaL_error(L, errmsg)); |
| 10301 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10302 | } |
| 10303 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10304 | static int hlua_filter_init_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 10305 | { |
| 10306 | struct hlua_flt_config *conf = fconf->conf; |
| 10307 | lua_State *L; |
| 10308 | int error, pos, state_id, flt_ref; |
| 10309 | |
| 10310 | state_id = reg_flt_to_stack_id(conf->reg); |
| 10311 | L = hlua_states[state_id]; |
| 10312 | pos = lua_gettop(L); |
| 10313 | |
| 10314 | /* The filter parsing function */ |
| 10315 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->fun_ref[state_id]); |
| 10316 | |
| 10317 | /* Push the filter class on the stack and resolve all callbacks */ |
| 10318 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->flt_ref[state_id]); |
| 10319 | |
| 10320 | /* Duplicate the filter class so each filter will have its own copy */ |
| 10321 | lua_newtable(L); |
| 10322 | lua_pushnil(L); |
| 10323 | |
| 10324 | while (lua_next(L, pos+2)) { |
| 10325 | lua_pushvalue(L, -2); |
| 10326 | lua_insert(L, -2); |
| 10327 | lua_settable(L, -4); |
| 10328 | } |
| 10329 | flt_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
| 10330 | |
| 10331 | /* Remove the original lua filter class from the stack */ |
| 10332 | lua_pop(L, 1); |
| 10333 | |
| 10334 | /* Push the copy on the stack */ |
| 10335 | lua_rawgeti(L, LUA_REGISTRYINDEX, flt_ref); |
| 10336 | |
| 10337 | /* extra args are pushed in a table */ |
| 10338 | lua_newtable(L); |
| 10339 | for (pos = 0; conf->args[pos]; pos++) { |
| 10340 | /* Check stack available size. */ |
| 10341 | if (!lua_checkstack(L, 1)) { |
| 10342 | ha_alert("Lua filter '%s' : Lua error : full stack.", conf->reg->name); |
| 10343 | goto error; |
| 10344 | } |
| 10345 | lua_pushstring(L, conf->args[pos]); |
| 10346 | lua_rawseti(L, -2, lua_rawlen(L, -2) + 1); |
| 10347 | } |
| 10348 | |
| 10349 | error = lua_pcall(L, 2, LUA_MULTRET, 0); |
| 10350 | switch (error) { |
| 10351 | case LUA_OK: |
| 10352 | /* replace the filter ref */ |
| 10353 | conf->ref[state_id] = flt_ref; |
| 10354 | break; |
| 10355 | case LUA_ERRRUN: |
| 10356 | ha_alert("Lua filter '%s' : runtime error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10357 | goto error; |
| 10358 | case LUA_ERRMEM: |
| 10359 | ha_alert("Lua filter '%s' : out of memory error", conf->reg->name); |
| 10360 | goto error; |
| 10361 | case LUA_ERRERR: |
| 10362 | ha_alert("Lua filter '%s' : message handler error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10363 | goto error; |
| 10364 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
| 10365 | case LUA_ERRGCMM: |
| 10366 | ha_alert("Lua filter '%s' : garbage collector error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10367 | goto error; |
| 10368 | #endif |
| 10369 | default: |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 10370 | 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] | 10371 | goto error; |
| 10372 | } |
| 10373 | |
| 10374 | lua_settop(L, 0); |
| 10375 | return 0; |
| 10376 | |
| 10377 | error: |
| 10378 | lua_settop(L, 0); |
| 10379 | return -1; |
| 10380 | } |
| 10381 | |
| 10382 | static void hlua_filter_deinit_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 10383 | { |
| 10384 | struct hlua_flt_config *conf = fconf->conf; |
| 10385 | lua_State *L; |
| 10386 | int state_id; |
| 10387 | |
| 10388 | if (!conf) |
| 10389 | return; |
| 10390 | |
| 10391 | state_id = reg_flt_to_stack_id(conf->reg); |
| 10392 | L = hlua_states[state_id]; |
| 10393 | luaL_unref(L, LUA_REGISTRYINDEX, conf->ref[state_id]); |
| 10394 | } |
| 10395 | |
| 10396 | static int hlua_filter_init(struct proxy *px, struct flt_conf *fconf) |
| 10397 | { |
| 10398 | struct hlua_flt_config *conf = fconf->conf; |
| 10399 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 10400 | |
| 10401 | /* Rely on per-thread init for global scripts */ |
| 10402 | if (!state_id) |
| 10403 | return hlua_filter_init_per_thread(px, fconf); |
| 10404 | return 0; |
| 10405 | } |
| 10406 | |
| 10407 | static void hlua_filter_deinit(struct proxy *px, struct flt_conf *fconf) |
| 10408 | { |
| 10409 | |
| 10410 | if (fconf->conf) { |
| 10411 | struct hlua_flt_config *conf = fconf->conf; |
| 10412 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 10413 | int pos; |
| 10414 | |
| 10415 | /* Rely on per-thread deinit for global scripts */ |
| 10416 | if (!state_id) |
| 10417 | hlua_filter_deinit_per_thread(px, fconf); |
| 10418 | |
| 10419 | for (pos = 0; conf->args[pos]; pos++) |
| 10420 | free(conf->args[pos]); |
| 10421 | free(conf->args); |
| 10422 | } |
| 10423 | ha_free(&fconf->conf); |
| 10424 | ha_free((char **)&fconf->id); |
| 10425 | ha_free(&fconf->ops); |
| 10426 | } |
| 10427 | |
| 10428 | static int hlua_filter_new(struct stream *s, struct filter *filter) |
| 10429 | { |
| 10430 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 10431 | struct hlua_flt_ctx *flt_ctx = NULL; |
| 10432 | int ret = 1; |
| 10433 | |
| 10434 | /* In the execution wrappers linked with a stream, the |
| 10435 | * Lua context can be not initialized. This behavior |
| 10436 | * permits to save performances because a systematic |
| 10437 | * Lua initialization cause 5% performances loss. |
| 10438 | */ |
| 10439 | if (!s->hlua) { |
| 10440 | struct hlua *hlua; |
| 10441 | |
| 10442 | hlua = pool_alloc(pool_head_hlua); |
| 10443 | if (!hlua) { |
| 10444 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 10445 | conf->reg->name); |
| 10446 | ret = 0; |
| 10447 | goto end; |
| 10448 | } |
| 10449 | HLUA_INIT(hlua); |
| 10450 | s->hlua = hlua; |
| 10451 | if (!hlua_ctx_init(s->hlua, reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 10452 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 10453 | conf->reg->name); |
| 10454 | ret = 0; |
| 10455 | goto end; |
| 10456 | } |
| 10457 | } |
| 10458 | |
| 10459 | flt_ctx = pool_zalloc(pool_head_hlua_flt_ctx); |
| 10460 | if (!flt_ctx) { |
| 10461 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10462 | conf->reg->name); |
| 10463 | ret = 0; |
| 10464 | goto end; |
| 10465 | } |
| 10466 | flt_ctx->hlua[0] = pool_alloc(pool_head_hlua); |
| 10467 | flt_ctx->hlua[1] = pool_alloc(pool_head_hlua); |
| 10468 | if (!flt_ctx->hlua[0] || !flt_ctx->hlua[1]) { |
| 10469 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10470 | conf->reg->name); |
| 10471 | ret = 0; |
| 10472 | goto end; |
| 10473 | } |
| 10474 | HLUA_INIT(flt_ctx->hlua[0]); |
| 10475 | HLUA_INIT(flt_ctx->hlua[1]); |
| 10476 | if (!hlua_ctx_init(flt_ctx->hlua[0], reg_flt_to_stack_id(conf->reg), s->task, 0) || |
| 10477 | !hlua_ctx_init(flt_ctx->hlua[1], reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 10478 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10479 | conf->reg->name); |
| 10480 | ret = 0; |
| 10481 | goto end; |
| 10482 | } |
| 10483 | |
| 10484 | if (!HLUA_IS_RUNNING(s->hlua)) { |
| 10485 | /* The following Lua calls can fail. */ |
| 10486 | if (!SET_SAFE_LJMP(s->hlua)) { |
| 10487 | const char *error; |
| 10488 | |
| 10489 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 10490 | error = lua_tostring(s->hlua->T, -1); |
| 10491 | else |
| 10492 | error = "critical error"; |
| 10493 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 10494 | ret = 0; |
| 10495 | goto end; |
| 10496 | } |
| 10497 | |
| 10498 | /* Check stack size. */ |
| 10499 | if (!lua_checkstack(s->hlua->T, 1)) { |
| 10500 | 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] | 10501 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10502 | ret = 0; |
| 10503 | goto end; |
| 10504 | } |
| 10505 | |
| 10506 | lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, conf->ref[s->hlua->state_id]); |
| 10507 | if (lua_getfield(s->hlua->T, -1, "new") != LUA_TFUNCTION) { |
| 10508 | SEND_ERR(s->be, "Lua filter '%s': 'new' field is not a function.\n", |
| 10509 | conf->reg->name); |
| 10510 | RESET_SAFE_LJMP(s->hlua); |
| 10511 | ret = 0; |
| 10512 | goto end; |
| 10513 | } |
| 10514 | lua_insert(s->hlua->T, -2); |
| 10515 | |
| 10516 | /* Push the copy on the stack */ |
| 10517 | s->hlua->nargs = 1; |
| 10518 | |
| 10519 | /* We must initialize the execution timeouts. */ |
| 10520 | s->hlua->max_time = hlua_timeout_session; |
| 10521 | |
| 10522 | /* At this point the execution is safe. */ |
| 10523 | RESET_SAFE_LJMP(s->hlua); |
| 10524 | } |
| 10525 | |
| 10526 | switch (hlua_ctx_resume(s->hlua, 0)) { |
| 10527 | case HLUA_E_OK: |
| 10528 | /* Nothing returned or not a table, ignore the filter for current stream */ |
| 10529 | if (!lua_gettop(s->hlua->T) || !lua_istable(s->hlua->T, 1)) { |
| 10530 | ret = 0; |
| 10531 | goto end; |
| 10532 | } |
| 10533 | |
| 10534 | /* Attached the filter pointer to the ctx */ |
| 10535 | lua_pushstring(s->hlua->T, "__filter"); |
| 10536 | lua_pushlightuserdata(s->hlua->T, filter); |
| 10537 | lua_settable(s->hlua->T, -3); |
| 10538 | |
| 10539 | /* Save a ref on the filter ctx */ |
| 10540 | lua_pushvalue(s->hlua->T, 1); |
| 10541 | flt_ctx->ref = luaL_ref(s->hlua->T, LUA_REGISTRYINDEX); |
| 10542 | filter->ctx = flt_ctx; |
| 10543 | break; |
| 10544 | case HLUA_E_ERRMSG: |
| 10545 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(s->hlua->T, -1)); |
| 10546 | ret = -1; |
| 10547 | goto end; |
| 10548 | case HLUA_E_ETMOUT: |
| 10549 | SEND_ERR(s->be, "Lua filter '%s' : 'new' execution timeout.\n", conf->reg->name); |
| 10550 | ret = 0; |
| 10551 | goto end; |
| 10552 | case HLUA_E_NOMEM: |
| 10553 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10554 | ret = 0; |
| 10555 | goto end; |
| 10556 | case HLUA_E_AGAIN: |
| 10557 | case HLUA_E_YIELD: |
| 10558 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10559 | " are not allowed from 'new' function.\n", conf->reg->name); |
| 10560 | ret = 0; |
| 10561 | goto end; |
| 10562 | case HLUA_E_ERR: |
| 10563 | SEND_ERR(s->be, "Lua filter '%s': 'new' returns an unknown error.\n", conf->reg->name); |
| 10564 | ret = 0; |
| 10565 | goto end; |
| 10566 | default: |
| 10567 | ret = 0; |
| 10568 | goto end; |
| 10569 | } |
| 10570 | |
| 10571 | end: |
| 10572 | if (s->hlua) |
| 10573 | lua_settop(s->hlua->T, 0); |
| 10574 | if (ret <= 0) { |
| 10575 | if (flt_ctx) { |
| 10576 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10577 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10578 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10579 | } |
| 10580 | } |
| 10581 | return ret; |
| 10582 | } |
| 10583 | |
| 10584 | static void hlua_filter_delete(struct stream *s, struct filter *filter) |
| 10585 | { |
| 10586 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10587 | |
| 10588 | luaL_unref(s->hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 10589 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10590 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10591 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10592 | filter->ctx = NULL; |
| 10593 | } |
| 10594 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 10595 | static int hlua_filter_from_payload(struct filter *filter) |
| 10596 | { |
| 10597 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10598 | |
| 10599 | return (flt_ctx && !!(flt_ctx->flags & HLUA_FLT_CTX_FL_PAYLOAD)); |
| 10600 | } |
| 10601 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10602 | static int hlua_filter_callback(struct stream *s, struct filter *filter, const char *fun, |
| 10603 | int dir, unsigned int flags) |
| 10604 | { |
| 10605 | struct hlua *flt_hlua; |
| 10606 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 10607 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10608 | unsigned int hflags = HLUA_TXN_FLT_CTX; |
| 10609 | int ret = 1; |
| 10610 | |
| 10611 | flt_hlua = flt_ctx->hlua[(dir == SMP_OPT_DIR_REQ ? 0 : 1)]; |
| 10612 | if (!flt_hlua) |
| 10613 | goto end; |
| 10614 | |
| 10615 | if (!HLUA_IS_RUNNING(flt_hlua)) { |
| 10616 | int extra_idx = lua_gettop(flt_hlua->T); |
| 10617 | |
| 10618 | /* The following Lua calls can fail. */ |
| 10619 | if (!SET_SAFE_LJMP(flt_hlua)) { |
| 10620 | const char *error; |
| 10621 | |
| 10622 | if (lua_type(flt_hlua->T, -1) == LUA_TSTRING) |
| 10623 | error = lua_tostring(flt_hlua->T, -1); |
| 10624 | else |
| 10625 | error = "critical error"; |
| 10626 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 10627 | goto end; |
| 10628 | } |
| 10629 | |
| 10630 | /* Check stack size. */ |
| 10631 | if (!lua_checkstack(flt_hlua->T, 3)) { |
| 10632 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10633 | RESET_SAFE_LJMP(flt_hlua); |
| 10634 | goto end; |
| 10635 | } |
| 10636 | |
| 10637 | lua_rawgeti(flt_hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 10638 | if (lua_getfield(flt_hlua->T, -1, fun) != LUA_TFUNCTION) { |
| 10639 | RESET_SAFE_LJMP(flt_hlua); |
| 10640 | goto end; |
| 10641 | } |
| 10642 | lua_insert(flt_hlua->T, -2); |
| 10643 | |
| 10644 | if (!hlua_txn_new(flt_hlua->T, s, s->be, dir, hflags)) { |
| 10645 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10646 | RESET_SAFE_LJMP(flt_hlua); |
| 10647 | goto end; |
| 10648 | } |
| 10649 | flt_hlua->nargs = 2; |
| 10650 | |
| 10651 | if (flags & HLUA_FLT_CB_ARG_CHN) { |
| 10652 | if (dir == SMP_OPT_DIR_REQ) |
| 10653 | lua_getfield(flt_hlua->T, -1, "req"); |
| 10654 | else |
| 10655 | lua_getfield(flt_hlua->T, -1, "res"); |
| 10656 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10657 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10658 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10659 | lua_settable(flt_hlua->T, -3); |
| 10660 | } |
| 10661 | flt_hlua->nargs++; |
| 10662 | } |
| 10663 | else if (flags & HLUA_FLT_CB_ARG_HTTP_MSG) { |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10664 | if (dir == SMP_OPT_DIR_REQ) |
| 10665 | lua_getfield(flt_hlua->T, -1, "http_req"); |
| 10666 | else |
| 10667 | lua_getfield(flt_hlua->T, -1, "http_res"); |
| 10668 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10669 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10670 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10671 | lua_settable(flt_hlua->T, -3); |
| 10672 | } |
| 10673 | flt_hlua->nargs++; |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10674 | } |
| 10675 | |
| 10676 | /* Check stack size. */ |
| 10677 | if (!lua_checkstack(flt_hlua->T, 1)) { |
| 10678 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10679 | RESET_SAFE_LJMP(flt_hlua); |
| 10680 | goto end; |
| 10681 | } |
| 10682 | |
| 10683 | while (extra_idx--) { |
| 10684 | lua_pushvalue(flt_hlua->T, 1); |
| 10685 | lua_remove(flt_hlua->T, 1); |
| 10686 | flt_hlua->nargs++; |
| 10687 | } |
| 10688 | |
| 10689 | /* We must initialize the execution timeouts. */ |
| 10690 | flt_hlua->max_time = hlua_timeout_session; |
| 10691 | |
| 10692 | /* At this point the execution is safe. */ |
| 10693 | RESET_SAFE_LJMP(flt_hlua); |
| 10694 | } |
| 10695 | |
| 10696 | switch (hlua_ctx_resume(flt_hlua, !(flags & HLUA_FLT_CB_FINAL))) { |
| 10697 | case HLUA_E_OK: |
| 10698 | /* Catch the return value if it required */ |
| 10699 | if ((flags & HLUA_FLT_CB_RETVAL) && lua_gettop(flt_hlua->T) > 0) { |
| 10700 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10701 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10702 | } |
| 10703 | |
| 10704 | /* Set timeout in the required channel. */ |
| 10705 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10706 | if (dir == SMP_OPT_DIR_REQ) |
| 10707 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10708 | else |
| 10709 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10710 | } |
| 10711 | break; |
| 10712 | case HLUA_E_AGAIN: |
| 10713 | /* Set timeout in the required channel. */ |
| 10714 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10715 | if (dir == SMP_OPT_DIR_REQ) |
| 10716 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10717 | else |
| 10718 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10719 | } |
| 10720 | /* Some actions can be wake up when a "write" event |
| 10721 | * is detected on a response channel. This is useful |
| 10722 | * only for actions targeted on the requests. |
| 10723 | */ |
| 10724 | if (HLUA_IS_WAKERESWR(flt_hlua)) |
| 10725 | s->res.flags |= CF_WAKE_WRITE; |
| 10726 | if (HLUA_IS_WAKEREQWR(flt_hlua)) |
| 10727 | s->req.flags |= CF_WAKE_WRITE; |
| 10728 | ret = 0; |
| 10729 | goto end; |
| 10730 | case HLUA_E_ERRMSG: |
| 10731 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(flt_hlua->T, -1)); |
| 10732 | ret = -1; |
| 10733 | goto end; |
| 10734 | case HLUA_E_ETMOUT: |
| 10735 | SEND_ERR(s->be, "Lua filter '%s' : '%s' callback execution timeout.\n", conf->reg->name, fun); |
| 10736 | goto end; |
| 10737 | case HLUA_E_NOMEM: |
| 10738 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10739 | goto end; |
| 10740 | case HLUA_E_YIELD: |
| 10741 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10742 | " are not allowed from '%s' callback.\n", conf->reg->name, fun); |
| 10743 | goto end; |
| 10744 | case HLUA_E_ERR: |
| 10745 | SEND_ERR(s->be, "Lua filter '%s': '%s' returns an unknown error.\n", conf->reg->name, fun); |
| 10746 | goto end; |
| 10747 | default: |
| 10748 | goto end; |
| 10749 | } |
| 10750 | |
| 10751 | |
| 10752 | end: |
| 10753 | return ret; |
| 10754 | } |
| 10755 | |
| 10756 | static int hlua_filter_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 10757 | { |
| 10758 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10759 | |
| 10760 | flt_ctx->flags = 0; |
| 10761 | return hlua_filter_callback(s, filter, "start_analyze", |
| 10762 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10763 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 10764 | } |
| 10765 | |
| 10766 | static int hlua_filter_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 10767 | { |
| 10768 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10769 | |
| 10770 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10771 | return hlua_filter_callback(s, filter, "end_analyze", |
| 10772 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10773 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 10774 | } |
| 10775 | |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10776 | static int hlua_filter_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 10777 | { |
| 10778 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10779 | |
| 10780 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10781 | return hlua_filter_callback(s, filter, "http_headers", |
| 10782 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10783 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10784 | } |
| 10785 | |
| 10786 | static int hlua_filter_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg, |
| 10787 | unsigned int offset, unsigned int len) |
| 10788 | { |
| 10789 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10790 | struct hlua *flt_hlua; |
| 10791 | int dir = (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 10792 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 10793 | int ret; |
| 10794 | |
| 10795 | flt_hlua = flt_ctx->hlua[idx]; |
| 10796 | flt_ctx->cur_off[idx] = offset; |
| 10797 | flt_ctx->cur_len[idx] = len; |
| 10798 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 10799 | ret = hlua_filter_callback(s, filter, "http_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10800 | if (ret != -1) { |
| 10801 | ret = flt_ctx->cur_len[idx]; |
| 10802 | if (lua_gettop(flt_hlua->T) > 0) { |
| 10803 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10804 | if (ret > flt_ctx->cur_len[idx]) |
| 10805 | ret = flt_ctx->cur_len[idx]; |
| 10806 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10807 | } |
| 10808 | } |
| 10809 | return ret; |
| 10810 | } |
| 10811 | |
| 10812 | static int hlua_filter_http_end(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 10813 | { |
| 10814 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10815 | |
| 10816 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10817 | return hlua_filter_callback(s, filter, "http_end", |
| 10818 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10819 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10820 | } |
| 10821 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10822 | static int hlua_filter_tcp_payload(struct stream *s, struct filter *filter, struct channel *chn, |
| 10823 | unsigned int offset, unsigned int len) |
| 10824 | { |
| 10825 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10826 | struct hlua *flt_hlua; |
| 10827 | int dir = (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 10828 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 10829 | int ret; |
| 10830 | |
| 10831 | flt_hlua = flt_ctx->hlua[idx]; |
| 10832 | flt_ctx->cur_off[idx] = offset; |
| 10833 | flt_ctx->cur_len[idx] = len; |
| 10834 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 10835 | ret = hlua_filter_callback(s, filter, "tcp_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_CHN)); |
| 10836 | if (ret != -1) { |
| 10837 | ret = flt_ctx->cur_len[idx]; |
| 10838 | if (lua_gettop(flt_hlua->T) > 0) { |
| 10839 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10840 | if (ret > flt_ctx->cur_len[idx]) |
| 10841 | ret = flt_ctx->cur_len[idx]; |
| 10842 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10843 | } |
| 10844 | } |
| 10845 | return ret; |
| 10846 | } |
| 10847 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10848 | static int hlua_filter_parse_fct(char **args, int *cur_arg, struct proxy *px, |
| 10849 | struct flt_conf *fconf, char **err, void *private) |
| 10850 | { |
| 10851 | struct hlua_reg_filter *reg_flt = private; |
| 10852 | lua_State *L; |
| 10853 | struct hlua_flt_config *conf = NULL; |
| 10854 | const char *flt_id = NULL; |
| 10855 | int state_id, pos, flt_flags = 0; |
| 10856 | struct flt_ops *hlua_flt_ops = NULL; |
| 10857 | |
| 10858 | state_id = reg_flt_to_stack_id(reg_flt); |
| 10859 | L = hlua_states[state_id]; |
| 10860 | |
| 10861 | /* Initialize the filter ops with default callbacks */ |
| 10862 | hlua_flt_ops = calloc(1, sizeof(*hlua_flt_ops)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10863 | if (!hlua_flt_ops) |
| 10864 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10865 | hlua_flt_ops->init = hlua_filter_init; |
| 10866 | hlua_flt_ops->deinit = hlua_filter_deinit; |
| 10867 | if (state_id) { |
| 10868 | /* Set per-thread callback if script is loaded per-thread */ |
| 10869 | hlua_flt_ops->init_per_thread = hlua_filter_init_per_thread; |
| 10870 | hlua_flt_ops->deinit_per_thread = hlua_filter_deinit_per_thread; |
| 10871 | } |
| 10872 | hlua_flt_ops->attach = hlua_filter_new; |
| 10873 | hlua_flt_ops->detach = hlua_filter_delete; |
| 10874 | |
| 10875 | /* Push the filter class on the stack and resolve all callbacks */ |
| 10876 | lua_rawgeti(L, LUA_REGISTRYINDEX, reg_flt->flt_ref[state_id]); |
| 10877 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10878 | if (lua_getfield(L, -1, "start_analyze") == LUA_TFUNCTION) |
| 10879 | hlua_flt_ops->channel_start_analyze = hlua_filter_start_analyze; |
| 10880 | lua_pop(L, 1); |
| 10881 | if (lua_getfield(L, -1, "end_analyze") == LUA_TFUNCTION) |
| 10882 | hlua_flt_ops->channel_end_analyze = hlua_filter_end_analyze; |
| 10883 | lua_pop(L, 1); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10884 | if (lua_getfield(L, -1, "http_headers") == LUA_TFUNCTION) |
| 10885 | hlua_flt_ops->http_headers = hlua_filter_http_headers; |
| 10886 | lua_pop(L, 1); |
| 10887 | if (lua_getfield(L, -1, "http_payload") == LUA_TFUNCTION) |
| 10888 | hlua_flt_ops->http_payload = hlua_filter_http_payload; |
| 10889 | lua_pop(L, 1); |
| 10890 | if (lua_getfield(L, -1, "http_end") == LUA_TFUNCTION) |
| 10891 | hlua_flt_ops->http_end = hlua_filter_http_end; |
| 10892 | lua_pop(L, 1); |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10893 | if (lua_getfield(L, -1, "tcp_payload") == LUA_TFUNCTION) |
| 10894 | hlua_flt_ops->tcp_payload = hlua_filter_tcp_payload; |
| 10895 | lua_pop(L, 1); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10896 | |
| 10897 | /* Get id and flags of the filter class */ |
| 10898 | if (lua_getfield(L, -1, "id") == LUA_TSTRING) |
| 10899 | flt_id = lua_tostring(L, -1); |
| 10900 | lua_pop(L, 1); |
| 10901 | if (lua_getfield(L, -1, "flags") == LUA_TNUMBER) |
| 10902 | flt_flags = lua_tointeger(L, -1); |
| 10903 | lua_pop(L, 1); |
| 10904 | |
| 10905 | /* Create the filter config */ |
| 10906 | conf = calloc(1, sizeof(*conf)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10907 | if (!conf) |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10908 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10909 | conf->reg = reg_flt; |
| 10910 | |
| 10911 | /* duplicate args */ |
| 10912 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++); |
| 10913 | conf->args = calloc(pos + 1, sizeof(*conf->args)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10914 | if (!conf->args) |
| 10915 | goto error; |
| 10916 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++) { |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10917 | conf->args[pos] = strdup(args[*cur_arg + 1 + pos]); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10918 | if (!conf->args[pos]) |
| 10919 | goto error; |
| 10920 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10921 | conf->args[pos] = NULL; |
| 10922 | *cur_arg += pos + 1; |
| 10923 | |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10924 | if (flt_id) { |
| 10925 | fconf->id = strdup(flt_id); |
| 10926 | if (!fconf->id) |
| 10927 | goto error; |
| 10928 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10929 | fconf->flags = flt_flags; |
| 10930 | fconf->conf = conf; |
| 10931 | fconf->ops = hlua_flt_ops; |
| 10932 | |
| 10933 | lua_settop(L, 0); |
| 10934 | return 0; |
| 10935 | |
| 10936 | error: |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10937 | 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] | 10938 | free(hlua_flt_ops); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10939 | if (conf && conf->args) { |
| 10940 | for (pos = 0; conf->args[pos]; pos++) |
| 10941 | free(conf->args[pos]); |
| 10942 | free(conf->args); |
| 10943 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10944 | free(conf); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10945 | free((char *)fconf->id); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10946 | lua_settop(L, 0); |
| 10947 | return -1; |
| 10948 | } |
| 10949 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10950 | __LJMP static int hlua_register_data_filter(lua_State *L) |
| 10951 | { |
| 10952 | struct filter *filter; |
| 10953 | struct channel *chn; |
| 10954 | |
| 10955 | MAY_LJMP(check_args(L, 2, "register_data_filter")); |
| 10956 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 10957 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 10958 | |
| 10959 | lua_getfield(L, 1, "__filter"); |
| 10960 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 10961 | filter = lua_touserdata (L, -1); |
| 10962 | lua_pop(L, 1); |
| 10963 | |
| 10964 | register_data_filter(chn_strm(chn), chn, filter); |
| 10965 | return 1; |
| 10966 | } |
| 10967 | |
| 10968 | __LJMP static int hlua_unregister_data_filter(lua_State *L) |
| 10969 | { |
| 10970 | struct filter *filter; |
| 10971 | struct channel *chn; |
| 10972 | |
| 10973 | MAY_LJMP(check_args(L, 2, "unregister_data_filter")); |
| 10974 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 10975 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 10976 | |
| 10977 | lua_getfield(L, 1, "__filter"); |
| 10978 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 10979 | filter = lua_touserdata (L, -1); |
| 10980 | lua_pop(L, 1); |
| 10981 | |
| 10982 | unregister_data_filter(chn_strm(chn), chn, filter); |
| 10983 | return 1; |
| 10984 | } |
| 10985 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10986 | /* 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] | 10987 | * 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] | 10988 | * parse configuration arguments. |
| 10989 | */ |
| 10990 | __LJMP static int hlua_register_filter(lua_State *L) |
| 10991 | { |
| 10992 | struct buffer *trash; |
| 10993 | struct flt_kw_list *fkl; |
| 10994 | struct flt_kw *fkw; |
| 10995 | const char *name; |
| 10996 | struct hlua_reg_filter *reg_flt= NULL; |
| 10997 | int flt_ref, fun_ref; |
| 10998 | int len; |
| 10999 | |
| 11000 | MAY_LJMP(check_args(L, 3, "register_filter")); |
| 11001 | |
| 11002 | /* First argument : filter name. */ |
| 11003 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 11004 | |
| 11005 | /* Second argument : The filter class */ |
| 11006 | flt_ref = MAY_LJMP(hlua_checktable(L, 2)); |
| 11007 | |
| 11008 | /* Third argument : lua function. */ |
| 11009 | fun_ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 11010 | |
| 11011 | trash = get_trash_chunk(); |
| 11012 | chunk_printf(trash, "lua.%s", name); |
| 11013 | fkw = flt_find_kw(trash->area); |
| 11014 | if (fkw != NULL) { |
| 11015 | reg_flt = fkw->private; |
| 11016 | if (reg_flt->flt_ref[hlua_state_id] != -1 || reg_flt->fun_ref[hlua_state_id] != -1) { |
| 11017 | ha_warning("Trying to register filter 'lua.%s' more than once. " |
| 11018 | "This will become a hard error in version 2.5.\n", name); |
| 11019 | } |
| 11020 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 11021 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 11022 | return 0; |
| 11023 | } |
| 11024 | |
| 11025 | fkl = calloc(1, sizeof(*fkl) + sizeof(struct flt_kw) * 2); |
| 11026 | if (!fkl) |
| 11027 | goto alloc_error; |
| 11028 | fkl->scope = "HLUA"; |
| 11029 | |
| 11030 | reg_flt = new_hlua_reg_filter(name); |
| 11031 | if (!reg_flt) |
| 11032 | goto alloc_error; |
| 11033 | |
| 11034 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 11035 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 11036 | |
| 11037 | /* The filter keyword */ |
| 11038 | len = strlen("lua.") + strlen(name) + 1; |
| 11039 | fkl->kw[0].kw = calloc(1, len); |
| 11040 | if (!fkl->kw[0].kw) |
| 11041 | goto alloc_error; |
| 11042 | |
| 11043 | snprintf((char *)fkl->kw[0].kw, len, "lua.%s", name); |
| 11044 | |
| 11045 | fkl->kw[0].parse = hlua_filter_parse_fct; |
| 11046 | fkl->kw[0].private = reg_flt; |
| 11047 | memset(&fkl->kw[1], 0, sizeof(*fkl->kw)); |
| 11048 | |
| 11049 | /* Register this new filter */ |
| 11050 | flt_register_keywords(fkl); |
| 11051 | |
| 11052 | return 0; |
| 11053 | |
| 11054 | alloc_error: |
| 11055 | release_hlua_reg_filter(reg_flt); |
| 11056 | ha_free(&fkl); |
| 11057 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 11058 | return 0; /* Never reached */ |
| 11059 | } |
| 11060 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11061 | 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] | 11062 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11063 | char **err, unsigned int *timeout) |
| 11064 | { |
| 11065 | const char *error; |
| 11066 | |
| 11067 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 11068 | if (error == PARSE_TIME_OVER) { |
| 11069 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 11070 | args[1], args[0]); |
| 11071 | return -1; |
| 11072 | } |
| 11073 | else if (error == PARSE_TIME_UNDER) { |
| 11074 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 11075 | args[1], args[0]); |
| 11076 | return -1; |
| 11077 | } |
| 11078 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11079 | memprintf(err, "%s: invalid timeout", args[0]); |
| 11080 | return -1; |
| 11081 | } |
| 11082 | return 0; |
| 11083 | } |
| 11084 | |
| 11085 | 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] | 11086 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11087 | char **err) |
| 11088 | { |
| 11089 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11090 | file, line, err, &hlua_timeout_session); |
| 11091 | } |
| 11092 | |
| 11093 | 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] | 11094 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11095 | char **err) |
| 11096 | { |
| 11097 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11098 | file, line, err, &hlua_timeout_task); |
| 11099 | } |
| 11100 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11101 | 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] | 11102 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11103 | char **err) |
| 11104 | { |
| 11105 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11106 | file, line, err, &hlua_timeout_applet); |
| 11107 | } |
| 11108 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11109 | 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] | 11110 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11111 | char **err) |
| 11112 | { |
| 11113 | char *error; |
| 11114 | |
| 11115 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 11116 | if (*error != '\0') { |
| 11117 | memprintf(err, "%s: invalid number", args[0]); |
| 11118 | return -1; |
| 11119 | } |
| 11120 | return 0; |
| 11121 | } |
| 11122 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11123 | 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] | 11124 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11125 | char **err) |
| 11126 | { |
| 11127 | char *error; |
| 11128 | |
| 11129 | if (*(args[1]) == 0) { |
| 11130 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 11131 | return -1; |
| 11132 | } |
| 11133 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 11134 | if (*error != '\0') { |
| 11135 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 11136 | return -1; |
| 11137 | } |
| 11138 | return 0; |
| 11139 | } |
| 11140 | |
| 11141 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11142 | /* This function is called by the main configuration key "lua-load". It loads and |
| 11143 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 11144 | * the main lua entry point. |
| 11145 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 11146 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 11147 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11148 | * |
| 11149 | * In some error case, LUA set an error message in top of the stack. This function |
| 11150 | * 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] | 11151 | * |
| 11152 | * This function can fail with an abort() due to an Lua critical error. |
| 11153 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 11154 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11155 | */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11156 | static int hlua_load_state(char *filename, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11157 | { |
| 11158 | int error; |
| 11159 | |
| 11160 | /* Just load and compile the file. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11161 | error = luaL_loadfile(L, filename); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11162 | if (error) { |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11163 | memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1)); |
| 11164 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11165 | return -1; |
| 11166 | } |
| 11167 | |
| 11168 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11169 | error = lua_pcall(L, 0, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11170 | switch (error) { |
| 11171 | case LUA_OK: |
| 11172 | break; |
| 11173 | case LUA_ERRRUN: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11174 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1)); |
| 11175 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11176 | return -1; |
| 11177 | case LUA_ERRMEM: |
Thierry Fournier | de6145f | 2020-11-29 00:55:53 +0100 | [diff] [blame] | 11178 | memprintf(err, "Lua out of memory error\n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11179 | return -1; |
| 11180 | case LUA_ERRERR: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11181 | memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1)); |
| 11182 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11183 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 11184 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11185 | case LUA_ERRGCMM: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11186 | memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1)); |
| 11187 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11188 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 11189 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11190 | default: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11191 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1)); |
| 11192 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11193 | return -1; |
| 11194 | } |
| 11195 | |
| 11196 | return 0; |
| 11197 | } |
| 11198 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11199 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 11200 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11201 | char **err) |
| 11202 | { |
| 11203 | if (*(args[1]) == 0) { |
| 11204 | memprintf(err, "'%s' expects a file name as parameter.\n", args[0]); |
| 11205 | return -1; |
| 11206 | } |
| 11207 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11208 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11209 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11210 | ha_set_thread(NULL); |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 11211 | return hlua_load_state(args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11212 | } |
| 11213 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11214 | 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] | 11215 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11216 | char **err) |
| 11217 | { |
| 11218 | int len; |
| 11219 | |
| 11220 | if (*(args[1]) == 0) { |
| 11221 | memprintf(err, "'%s' expects a file as parameter.\n", args[0]); |
| 11222 | return -1; |
| 11223 | } |
| 11224 | |
| 11225 | if (per_thread_load == NULL) { |
| 11226 | /* allocate the first entry large enough to store the final NULL */ |
| 11227 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 11228 | if (per_thread_load == NULL) { |
| 11229 | memprintf(err, "out of memory error"); |
| 11230 | return -1; |
| 11231 | } |
| 11232 | } |
| 11233 | |
| 11234 | /* count used entries */ |
| 11235 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 11236 | ; |
| 11237 | |
| 11238 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 11239 | if (per_thread_load == NULL) { |
| 11240 | memprintf(err, "out of memory error"); |
| 11241 | return -1; |
| 11242 | } |
| 11243 | |
| 11244 | per_thread_load[len] = strdup(args[1]); |
| 11245 | per_thread_load[len + 1] = NULL; |
| 11246 | |
| 11247 | if (per_thread_load[len] == NULL) { |
| 11248 | memprintf(err, "out of memory error"); |
| 11249 | return -1; |
| 11250 | } |
| 11251 | |
| 11252 | /* loading for thread 1 only */ |
| 11253 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11254 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11255 | return hlua_load_state(args[1], hlua_states[1], err); |
| 11256 | } |
| 11257 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11258 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 11259 | * in the given <ctx>. |
| 11260 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 11261 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11262 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 11263 | lua_getglobal(L, "package"); /* push package variable */ |
| 11264 | lua_pushstring(L, path); /* push given path */ |
| 11265 | lua_pushstring(L, ";"); /* push semicolon */ |
| 11266 | lua_getfield(L, -3, type); /* push old path */ |
| 11267 | lua_concat(L, 3); /* concatenate to new path */ |
| 11268 | lua_setfield(L, -2, type); /* store new path */ |
| 11269 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11270 | |
| 11271 | return 0; |
| 11272 | } |
| 11273 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11274 | 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] | 11275 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11276 | char **err) |
| 11277 | { |
| 11278 | char *path; |
| 11279 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11280 | struct prepend_path *p = NULL; |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 11281 | size_t i; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11282 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11283 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11284 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11285 | } |
| 11286 | |
| 11287 | if (!(*args[1])) { |
| 11288 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11289 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11290 | } |
| 11291 | path = args[1]; |
| 11292 | |
| 11293 | if (*args[2]) { |
| 11294 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 11295 | 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] | 11296 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11297 | } |
| 11298 | type = args[2]; |
| 11299 | } |
| 11300 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11301 | p = calloc(1, sizeof(*p)); |
| 11302 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11303 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11304 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11305 | } |
| 11306 | p->path = strdup(path); |
| 11307 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11308 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11309 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11310 | } |
| 11311 | p->type = strdup(type); |
| 11312 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11313 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11314 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11315 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 11316 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11317 | |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 11318 | /* Handle the global state and the per-thread state for the first |
| 11319 | * thread. The remaining threads will be initialized based on |
| 11320 | * prepend_path_list. |
| 11321 | */ |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 11322 | for (i = 0; i < 2; i++) { |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 11323 | lua_State *L = hlua_states[i]; |
| 11324 | const char *error; |
| 11325 | |
| 11326 | if (setjmp(safe_ljmp_env) != 0) { |
| 11327 | lua_atpanic(L, hlua_panic_safe); |
| 11328 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11329 | error = lua_tostring(L, -1); |
| 11330 | else |
| 11331 | error = "critical error"; |
| 11332 | fprintf(stderr, "lua-prepend-path: %s.\n", error); |
| 11333 | exit(1); |
| 11334 | } else { |
| 11335 | lua_atpanic(L, hlua_panic_ljmp); |
| 11336 | } |
| 11337 | |
| 11338 | hlua_prepend_path(L, type, path); |
| 11339 | |
| 11340 | lua_atpanic(L, hlua_panic_safe); |
| 11341 | } |
| 11342 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11343 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11344 | |
| 11345 | err2: |
| 11346 | free(p->type); |
| 11347 | free(p->path); |
| 11348 | err: |
| 11349 | free(p); |
| 11350 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11351 | } |
| 11352 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11353 | /* configuration keywords declaration */ |
| 11354 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11355 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11356 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11357 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11358 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 11359 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 11360 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11361 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11362 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11363 | { 0, NULL, NULL }, |
| 11364 | }}; |
| 11365 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 11366 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 11367 | |
William Lallemand | 5213918 | 2022-03-30 15:05:42 +0200 | [diff] [blame] | 11368 | #ifdef USE_OPENSSL |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 11369 | |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11370 | /* |
| 11371 | * This function replace a ckch_store by another one, and rebuild the ckch_inst and all its dependencies. |
| 11372 | * It does the sam as "cli_io_handler_commit_cert" but for lua, the major |
| 11373 | * difference is that the yield in lua and for the CLI is not handled the same |
| 11374 | * way. |
| 11375 | */ |
| 11376 | __LJMP static int hlua_ckch_commit_yield(lua_State *L, int status, lua_KContext ctx) |
| 11377 | { |
| 11378 | struct ckch_inst **lua_ckchi = lua_touserdata(L, -1); |
| 11379 | struct ckch_store **lua_ckchs = lua_touserdata(L, -2); |
| 11380 | struct ckch_inst *ckchi = *lua_ckchi; |
| 11381 | struct ckch_store *old_ckchs = lua_ckchs[0]; |
| 11382 | struct ckch_store *new_ckchs = lua_ckchs[1]; |
| 11383 | struct hlua *hlua; |
| 11384 | char *err = NULL; |
| 11385 | int y = 1; |
| 11386 | |
| 11387 | hlua = hlua_gethlua(L); |
| 11388 | |
| 11389 | /* get the first ckchi to copy */ |
| 11390 | if (ckchi == NULL) |
| 11391 | ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs); |
| 11392 | |
| 11393 | /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */ |
| 11394 | list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) { |
| 11395 | struct ckch_inst *new_inst; |
| 11396 | |
| 11397 | /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */ |
| 11398 | if (y % 10 == 0) { |
| 11399 | |
| 11400 | *lua_ckchi = ckchi; |
| 11401 | |
| 11402 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 11403 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_ckch_commit_yield, TICK_ETERNITY, 0)); |
| 11404 | } |
| 11405 | |
| 11406 | if (ckch_inst_rebuild(new_ckchs, ckchi, &new_inst, &err)) |
| 11407 | goto error; |
| 11408 | |
| 11409 | /* link the new ckch_inst to the duplicate */ |
| 11410 | LIST_APPEND(&new_ckchs->ckch_inst, &new_inst->by_ckchs); |
| 11411 | y++; |
| 11412 | } |
| 11413 | |
| 11414 | /* The generation is finished, we can insert everything */ |
| 11415 | ckch_store_replace(old_ckchs, new_ckchs); |
| 11416 | |
| 11417 | lua_pop(L, 2); /* pop the lua_ckchs and ckchi */ |
| 11418 | |
| 11419 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11420 | |
| 11421 | return 0; |
| 11422 | |
| 11423 | error: |
| 11424 | ckch_store_free(new_ckchs); |
| 11425 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11426 | WILL_LJMP(luaL_error(L, "%s", err)); |
| 11427 | free(err); |
| 11428 | |
| 11429 | return 0; |
| 11430 | } |
| 11431 | |
| 11432 | /* |
| 11433 | * Replace a ckch_store <filename> in the ckchs_tree with a ckch_store created |
| 11434 | * from the table in parameter. |
| 11435 | * |
| 11436 | * This is equivalent to "set ssl cert" + "commit ssl cert" over the CLI, which |
| 11437 | * means it does not need to have a transaction since everything is done in the |
| 11438 | * same function. |
| 11439 | * |
| 11440 | * CertCache.set{filename="", crt="", key="", sctl="", ocsp="", issuer=""} |
| 11441 | * |
| 11442 | */ |
| 11443 | __LJMP static int hlua_ckch_set(lua_State *L) |
| 11444 | { |
| 11445 | struct hlua *hlua; |
| 11446 | struct ckch_inst **lua_ckchi; |
| 11447 | struct ckch_store **lua_ckchs; |
| 11448 | struct ckch_store *old_ckchs = NULL; |
| 11449 | struct ckch_store *new_ckchs = NULL; |
| 11450 | int errcode = 0; |
| 11451 | char *err = NULL; |
| 11452 | struct cert_exts *cert_ext = NULL; |
| 11453 | char *filename; |
| 11454 | struct cert_key_and_chain *ckch; |
| 11455 | int ret; |
| 11456 | |
| 11457 | if (lua_type(L, -1) != LUA_TTABLE) |
| 11458 | WILL_LJMP(luaL_error(L, "'CertCache.set' needs a table as argument")); |
| 11459 | |
| 11460 | hlua = hlua_gethlua(L); |
| 11461 | |
| 11462 | /* FIXME: this should not return an error but should come back later */ |
| 11463 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 11464 | WILL_LJMP(luaL_error(L, "CertCache already under lock")); |
| 11465 | |
| 11466 | ret = lua_getfield(L, -1, "filename"); |
| 11467 | if (ret != LUA_TSTRING) { |
| 11468 | memprintf(&err, "%sNo filename specified!\n", err ? err : ""); |
| 11469 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11470 | goto end; |
| 11471 | } |
| 11472 | filename = (char *)lua_tostring(L, -1); |
| 11473 | |
| 11474 | |
| 11475 | /* look for the filename in the tree */ |
| 11476 | old_ckchs = ckchs_lookup(filename); |
| 11477 | if (!old_ckchs) { |
| 11478 | memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n", err ? err : ""); |
| 11479 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11480 | goto end; |
| 11481 | } |
| 11482 | /* TODO: handle extra_files_noext */ |
| 11483 | |
| 11484 | new_ckchs = ckchs_dup(old_ckchs); |
| 11485 | if (!new_ckchs) { |
| 11486 | memprintf(&err, "%sCannot allocate memory!\n", err ? err : ""); |
| 11487 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11488 | goto end; |
| 11489 | } |
| 11490 | |
| 11491 | ckch = new_ckchs->ckch; |
| 11492 | |
| 11493 | /* loop on the field in the table, which have the same name as the |
| 11494 | * possible extensions of files */ |
| 11495 | lua_pushnil(L); |
| 11496 | while (lua_next(L, 1)) { |
| 11497 | int i; |
| 11498 | const char *field = lua_tostring(L, -2); |
| 11499 | char *payload = (char *)lua_tostring(L, -1); |
| 11500 | |
| 11501 | if (!field || strcmp(field, "filename") == 0) { |
| 11502 | lua_pop(L, 1); |
| 11503 | continue; |
| 11504 | } |
| 11505 | |
| 11506 | for (i = 0; field && cert_exts[i].ext != NULL; i++) { |
| 11507 | if (strcmp(field, cert_exts[i].ext) == 0) { |
| 11508 | cert_ext = &cert_exts[i]; |
| 11509 | break; |
| 11510 | } |
| 11511 | } |
| 11512 | |
| 11513 | /* this is the default type, the field is not supported */ |
| 11514 | if (cert_ext == NULL) { |
| 11515 | memprintf(&err, "%sUnsupported field '%s'\n", err ? err : "", field); |
| 11516 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11517 | goto end; |
| 11518 | } |
| 11519 | |
| 11520 | /* appply the change on the duplicate */ |
| 11521 | if (cert_exts->load(filename, payload, ckch, &err) != 0) { |
| 11522 | memprintf(&err, "%sCan't load the payload\n", err ? err : ""); |
| 11523 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11524 | goto end; |
| 11525 | } |
| 11526 | lua_pop(L, 1); |
| 11527 | } |
| 11528 | |
| 11529 | /* store the pointers on the lua stack */ |
| 11530 | lua_ckchs = lua_newuserdata(L, sizeof(struct ckch_store *) * 2); |
| 11531 | lua_ckchs[0] = old_ckchs; |
| 11532 | lua_ckchs[1] = new_ckchs; |
| 11533 | lua_ckchi = lua_newuserdata(L, sizeof(struct ckch_inst *)); |
| 11534 | *lua_ckchi = NULL; |
| 11535 | |
| 11536 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 11537 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_ckch_commit_yield, TICK_ETERNITY, 0)); |
| 11538 | |
| 11539 | end: |
| 11540 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11541 | |
| 11542 | if (errcode & ERR_CODE) { |
| 11543 | ckch_store_free(new_ckchs); |
| 11544 | WILL_LJMP(luaL_error(L, "%s", err)); |
| 11545 | } |
| 11546 | free(err); |
| 11547 | |
| 11548 | return 0; |
| 11549 | } |
| 11550 | |
William Lallemand | 5213918 | 2022-03-30 15:05:42 +0200 | [diff] [blame] | 11551 | #else |
| 11552 | |
| 11553 | __LJMP static int hlua_ckch_set(lua_State *L) |
| 11554 | { |
| 11555 | WILL_LJMP(luaL_error(L, "'CertCache.set' needs an HAProxy built with OpenSSL")); |
| 11556 | |
| 11557 | return 0; |
| 11558 | } |
| 11559 | #endif /* ! USE_OPENSSL */ |
| 11560 | |
| 11561 | |
| 11562 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 11563 | /* This function can fail with an abort() due to an Lua critical error. |
| 11564 | * We are in the initialisation process of HAProxy, this abort() is |
| 11565 | * tolerated. |
| 11566 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11567 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11568 | { |
| 11569 | struct hlua_init_function *init; |
| 11570 | const char *msg; |
| 11571 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 11572 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11573 | const char *kind; |
| 11574 | const char *trace; |
| 11575 | int return_status = 1; |
| 11576 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 11577 | int nres; |
| 11578 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11579 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11580 | /* disable memory limit checks if limit is not set */ |
| 11581 | if (!hlua_global_allocator.limit) |
| 11582 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 11583 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 11584 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11585 | if (setjmp(safe_ljmp_env) != 0) { |
| 11586 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11587 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11588 | error = lua_tostring(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11589 | else |
| 11590 | error = "critical error"; |
| 11591 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 11592 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11593 | } else { |
| 11594 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11595 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 11596 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11597 | hlua_fcn_post_init(L); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11598 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11599 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11600 | lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11601 | |
| 11602 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11603 | ret = lua_resume(L, L, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11604 | #else |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11605 | ret = lua_resume(L, L, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11606 | #endif |
| 11607 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11608 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11609 | |
| 11610 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11611 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 11612 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11613 | |
| 11614 | case LUA_ERRERR: |
| 11615 | kind = "message handler error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11616 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11617 | case LUA_ERRRUN: |
| 11618 | if (!kind) |
| 11619 | kind = "runtime error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11620 | msg = lua_tostring(L, -1); |
| 11621 | lua_settop(L, 0); /* Empty the stack. */ |
| 11622 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 11623 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11624 | if (msg) |
| 11625 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 11626 | else |
| 11627 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
| 11628 | return_status = 0; |
| 11629 | break; |
| 11630 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11631 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11632 | /* Unknown error */ |
| 11633 | kind = "Unknown error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11634 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11635 | case LUA_YIELD: |
| 11636 | /* yield is not configured at this step, this state doesn't happen */ |
| 11637 | if (!kind) |
| 11638 | kind = "yield not allowed"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11639 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11640 | case LUA_ERRMEM: |
| 11641 | if (!kind) |
| 11642 | kind = "out of memory error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11643 | lua_settop(L, 0); |
| 11644 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 11645 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11646 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 11647 | return_status = 0; |
| 11648 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11649 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11650 | if (!return_status) |
| 11651 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11652 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11653 | |
| 11654 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11655 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11656 | } |
| 11657 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11658 | int hlua_post_init() |
| 11659 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11660 | int ret; |
| 11661 | int i; |
| 11662 | int errors; |
| 11663 | char *err = NULL; |
| 11664 | struct hlua_function *fcn; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11665 | struct hlua_reg_filter *reg_flt; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11666 | |
Willy Tarreau | b131049 | 2021-08-30 09:35:18 +0200 | [diff] [blame] | 11667 | #if defined(USE_OPENSSL) |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11668 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11669 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11670 | int saved_used_backed = global.ssl_used_backend; |
| 11671 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11672 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11673 | global.ssl_used_backend = saved_used_backed; |
| 11674 | } |
| 11675 | #endif |
| 11676 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11677 | /* Perform post init of common thread */ |
| 11678 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11679 | ha_set_thread(&ha_thread_info[0]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11680 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 11681 | if (ret == 0) |
| 11682 | return 0; |
| 11683 | |
| 11684 | /* init remaining lua states and load files */ |
| 11685 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 11686 | |
| 11687 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11688 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11689 | |
| 11690 | /* Init lua state */ |
| 11691 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 11692 | |
| 11693 | /* Load lua files */ |
| 11694 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 11695 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 11696 | if (ret != 0) { |
| 11697 | ha_alert("Lua init: %s\n", err); |
| 11698 | return 0; |
| 11699 | } |
| 11700 | } |
| 11701 | } |
| 11702 | |
| 11703 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11704 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11705 | |
| 11706 | /* Execute post init for all states */ |
| 11707 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 11708 | |
| 11709 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11710 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11711 | |
| 11712 | /* run post init */ |
| 11713 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 11714 | if (ret == 0) |
| 11715 | return 0; |
| 11716 | } |
| 11717 | |
| 11718 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11719 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11720 | |
| 11721 | /* control functions registering. Each function must have: |
| 11722 | * - only the function_ref[0] set positive and all other to -1 |
| 11723 | * - only the function_ref[0] set to -1 and all other positive |
| 11724 | * This ensure a same reference is not used both in shared |
| 11725 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11726 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11727 | * complicated to found for the end user. |
| 11728 | */ |
| 11729 | errors = 0; |
| 11730 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 11731 | ret = 0; |
| 11732 | for (i = 1; i < global.nbthread + 1; i++) { |
| 11733 | if (fcn->function_ref[i] == -1) |
| 11734 | ret--; |
| 11735 | else |
| 11736 | ret++; |
| 11737 | } |
| 11738 | if (abs(ret) != global.nbthread) { |
| 11739 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 11740 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 11741 | errors++; |
| 11742 | continue; |
| 11743 | } |
| 11744 | |
| 11745 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11746 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 11747 | "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] | 11748 | "exclusive.\n", fcn->name); |
| 11749 | errors++; |
| 11750 | } |
| 11751 | } |
| 11752 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11753 | /* Do the same with registered filters */ |
| 11754 | list_for_each_entry(reg_flt, &referenced_filters, l) { |
| 11755 | ret = 0; |
| 11756 | for (i = 1; i < global.nbthread + 1; i++) { |
| 11757 | if (reg_flt->flt_ref[i] == -1) |
| 11758 | ret--; |
| 11759 | else |
| 11760 | ret++; |
| 11761 | } |
| 11762 | if (abs(ret) != global.nbthread) { |
| 11763 | ha_alert("Lua filter '%s' is not referenced in all thread. " |
| 11764 | "Expect function in all thread or in none thread.\n", reg_flt->name); |
| 11765 | errors++; |
| 11766 | continue; |
| 11767 | } |
| 11768 | |
| 11769 | if ((reg_flt->flt_ref[0] == -1) == (ret < 0)) { |
| 11770 | ha_alert("Lua filter '%s' is referenced both ins shared Lua context (through lua-load) " |
| 11771 | "and per-thread Lua context (through lua-load-per-thread). these two context " |
| 11772 | "exclusive.\n", fcn->name); |
| 11773 | errors++; |
| 11774 | } |
| 11775 | } |
| 11776 | |
| 11777 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11778 | if (errors > 0) |
| 11779 | return 0; |
| 11780 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11781 | /* 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] | 11782 | * -1 in order to have probably a segfault if someone use it |
| 11783 | */ |
| 11784 | hlua_state_id = -1; |
| 11785 | |
| 11786 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11787 | } |
| 11788 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11789 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 11790 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 11791 | * 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] | 11792 | * allocation. <nsize> is the requested new size. A new allocation is |
| 11793 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11794 | * zero. This one verifies that the limits are respected but is optimized |
| 11795 | * 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] | 11796 | * |
| 11797 | * Warning: while this API ressembles glibc's realloc() a lot, glibc surpasses |
| 11798 | * POSIX by making realloc(ptr,0) an effective free(), but others do not do |
| 11799 | * that and will simply allocate zero as if it were the result of malloc(0), |
| 11800 | * so mapping this onto realloc() will lead to memory leaks on non-glibc |
| 11801 | * systems. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11802 | */ |
| 11803 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 11804 | { |
| 11805 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11806 | size_t limit, old, new; |
| 11807 | |
| 11808 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 11809 | * for accounting anymore. |
| 11810 | */ |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 11811 | if (likely(~zone->limit == 0)) { |
| 11812 | if (!nsize) |
| 11813 | ha_free(&ptr); |
| 11814 | else |
| 11815 | ptr = realloc(ptr, nsize); |
| 11816 | return ptr; |
| 11817 | } |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11818 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 11819 | if (!ptr) |
| 11820 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11821 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11822 | /* enforce strict limits across all threads */ |
| 11823 | limit = zone->limit; |
| 11824 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 11825 | do { |
| 11826 | new = old + nsize - osize; |
| 11827 | if (unlikely(nsize && limit && new > limit)) |
| 11828 | return NULL; |
| 11829 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11830 | |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 11831 | if (!nsize) |
| 11832 | ha_free(&ptr); |
| 11833 | else |
| 11834 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11835 | |
| 11836 | if (unlikely(!ptr && nsize)) // failed |
| 11837 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 11838 | |
| 11839 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11840 | return ptr; |
| 11841 | } |
| 11842 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11843 | /* 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] | 11844 | * We are in the initialisation process of HAProxy, this abort() is |
| 11845 | * tolerated. |
| 11846 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11847 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 11848 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11849 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11850 | int idx; |
| 11851 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11852 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11853 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 11854 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11855 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11856 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11857 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11858 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11859 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11860 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11861 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11862 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11863 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11864 | *context = NULL; |
| 11865 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 11866 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 11867 | * the Lua function can fail with an abort. We are in the initialisation |
| 11868 | * process of HAProxy, this abort() is tolerated. |
| 11869 | */ |
| 11870 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11871 | /* Call post initialisation function in safe environment. */ |
| 11872 | if (setjmp(safe_ljmp_env) != 0) { |
| 11873 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11874 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11875 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 11876 | else |
| 11877 | error_msg = "critical error"; |
| 11878 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 11879 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11880 | } else { |
| 11881 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 11882 | } |
| 11883 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11884 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11885 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 11886 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 11887 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 11888 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11889 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 11890 | #endif |
| 11891 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11892 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 11893 | #endif |
| 11894 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 11895 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11896 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11897 | /* Apply configured prepend path */ |
| 11898 | list_for_each_entry(pp, &prepend_path_list, l) |
| 11899 | hlua_prepend_path(L, pp->type, pp->path); |
| 11900 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11901 | /* |
| 11902 | * |
| 11903 | * Create "core" object. |
| 11904 | * |
| 11905 | */ |
| 11906 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 11907 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11908 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11909 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11910 | /* set the thread id */ |
| 11911 | hlua_class_const_int(L, "thread", thread_num); |
| 11912 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11913 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 11914 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11915 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11916 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11917 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11918 | hlua_class_function(L, "register_init", hlua_register_init); |
| 11919 | hlua_class_function(L, "register_task", hlua_register_task); |
| 11920 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 11921 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 11922 | hlua_class_function(L, "register_action", hlua_register_action); |
| 11923 | hlua_class_function(L, "register_service", hlua_register_service); |
| 11924 | hlua_class_function(L, "register_cli", hlua_register_cli); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11925 | hlua_class_function(L, "register_filter", hlua_register_filter); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11926 | hlua_class_function(L, "yield", hlua_yield); |
| 11927 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 11928 | hlua_class_function(L, "sleep", hlua_sleep); |
| 11929 | hlua_class_function(L, "msleep", hlua_msleep); |
| 11930 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 11931 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 11932 | hlua_class_function(L, "set_map", hlua_set_map); |
| 11933 | hlua_class_function(L, "del_map", hlua_del_map); |
| 11934 | hlua_class_function(L, "tcp", hlua_socket_new); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 11935 | hlua_class_function(L, "httpclient", hlua_httpclient_new); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11936 | hlua_class_function(L, "log", hlua_log); |
| 11937 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 11938 | hlua_class_function(L, "Info", hlua_log_info); |
| 11939 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 11940 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 11941 | hlua_class_function(L, "done", hlua_done); |
| 11942 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11943 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11944 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 11945 | |
| 11946 | /* |
| 11947 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11948 | * Create "act" object. |
| 11949 | * |
| 11950 | */ |
| 11951 | |
| 11952 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11953 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11954 | |
| 11955 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11956 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 11957 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 11958 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 11959 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 11960 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 11961 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 11962 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 11963 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11964 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11965 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 11966 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11967 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11968 | |
| 11969 | /* |
| 11970 | * |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11971 | * Create "Filter" object. |
| 11972 | * |
| 11973 | */ |
| 11974 | |
| 11975 | /* This table entry is the object "filter" base. */ |
| 11976 | lua_newtable(L); |
| 11977 | |
| 11978 | /* push flags and constants */ |
| 11979 | hlua_class_const_int(L, "CONTINUE", 1); |
| 11980 | hlua_class_const_int(L, "WAIT", 0); |
| 11981 | hlua_class_const_int(L, "ERROR", -1); |
| 11982 | |
| 11983 | hlua_class_const_int(L, "FLT_CFG_FL_HTX", FLT_CFG_FL_HTX); |
| 11984 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11985 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
| 11986 | hlua_class_function(L, "register_data_filter", hlua_register_data_filter); |
| 11987 | hlua_class_function(L, "unregister_data_filter", hlua_unregister_data_filter); |
| 11988 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11989 | lua_setglobal(L, "filter"); |
| 11990 | |
| 11991 | /* |
| 11992 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11993 | * Register class Map |
| 11994 | * |
| 11995 | */ |
| 11996 | |
| 11997 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11998 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11999 | |
| 12000 | /* register pattern types. */ |
| 12001 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12002 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 12003 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12004 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12005 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 12006 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12007 | |
| 12008 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12009 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12010 | |
| 12011 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12012 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12013 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12014 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12015 | lua_pushstring(L, "__index"); |
| 12016 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12017 | |
| 12018 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12019 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 12020 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12021 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12022 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12023 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 12024 | /* Register previous table in the registry with reference and named entry. |
| 12025 | * The function hlua_register_metatable() pops the stack, so we |
| 12026 | * previously create a copy of the table. |
| 12027 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12028 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 12029 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12030 | |
| 12031 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12032 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12033 | |
| 12034 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12035 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12036 | |
| 12037 | /* |
| 12038 | * |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12039 | * Register "CertCache" class |
| 12040 | * |
| 12041 | */ |
| 12042 | |
| 12043 | /* Create and fill the metatable. */ |
| 12044 | lua_newtable(L); |
| 12045 | /* Register */ |
| 12046 | hlua_class_function(L, "set", hlua_ckch_set); |
| 12047 | lua_setglobal(L, CLASS_CERTCACHE); /* Create global object called Regex */ |
| 12048 | |
| 12049 | /* |
| 12050 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12051 | * Register class Channel |
| 12052 | * |
| 12053 | */ |
| 12054 | |
| 12055 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12056 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12057 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12058 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12059 | lua_pushstring(L, "__index"); |
| 12060 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12061 | |
| 12062 | /* Register . */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12063 | hlua_class_function(L, "data", hlua_channel_get_data); |
| 12064 | hlua_class_function(L, "line", hlua_channel_get_line); |
| 12065 | hlua_class_function(L, "set", hlua_channel_set_data); |
| 12066 | hlua_class_function(L, "remove", hlua_channel_del_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12067 | hlua_class_function(L, "append", hlua_channel_append); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12068 | hlua_class_function(L, "prepend", hlua_channel_prepend); |
| 12069 | hlua_class_function(L, "insert", hlua_channel_insert_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12070 | hlua_class_function(L, "send", hlua_channel_send); |
| 12071 | hlua_class_function(L, "forward", hlua_channel_forward); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12072 | hlua_class_function(L, "input", hlua_channel_get_in_len); |
| 12073 | hlua_class_function(L, "output", hlua_channel_get_out_len); |
| 12074 | hlua_class_function(L, "may_recv", hlua_channel_may_recv); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12075 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 12076 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12077 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12078 | /* Deprecated API */ |
| 12079 | hlua_class_function(L, "get", hlua_channel_get); |
| 12080 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 12081 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 12082 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 12083 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 12084 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12085 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12086 | |
| 12087 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12088 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12089 | |
| 12090 | /* |
| 12091 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12092 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12093 | * |
| 12094 | */ |
| 12095 | |
| 12096 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12097 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12098 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12099 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12100 | lua_pushstring(L, "__index"); |
| 12101 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12102 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12103 | /* Browse existing fetches and create the associated |
| 12104 | * object method. |
| 12105 | */ |
| 12106 | sf = NULL; |
| 12107 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12108 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 12109 | * by an underscore. |
| 12110 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 12111 | strlcpy2(trash.area, sf->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12112 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12113 | if (*p == '.' || *p == '-' || *p == '+') |
| 12114 | *p = '_'; |
| 12115 | |
| 12116 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12117 | lua_pushstring(L, trash.area); |
| 12118 | lua_pushlightuserdata(L, sf); |
| 12119 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 12120 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12121 | } |
| 12122 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12123 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12124 | |
| 12125 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12126 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12127 | |
| 12128 | /* |
| 12129 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12130 | * Register class Converters |
| 12131 | * |
| 12132 | */ |
| 12133 | |
| 12134 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12135 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12136 | |
| 12137 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12138 | lua_pushstring(L, "__index"); |
| 12139 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12140 | |
| 12141 | /* Browse existing converters and create the associated |
| 12142 | * object method. |
| 12143 | */ |
| 12144 | sc = NULL; |
| 12145 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12146 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 12147 | * by an underscore. |
| 12148 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 12149 | strlcpy2(trash.area, sc->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12150 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12151 | if (*p == '.' || *p == '-' || *p == '+') |
| 12152 | *p = '_'; |
| 12153 | |
| 12154 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12155 | lua_pushstring(L, trash.area); |
| 12156 | lua_pushlightuserdata(L, sc); |
| 12157 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 12158 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12159 | } |
| 12160 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12161 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12162 | |
| 12163 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12164 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12165 | |
| 12166 | /* |
| 12167 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12168 | * Register class HTTP |
| 12169 | * |
| 12170 | */ |
| 12171 | |
| 12172 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12173 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12174 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12175 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12176 | lua_pushstring(L, "__index"); |
| 12177 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12178 | |
| 12179 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12180 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 12181 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 12182 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 12183 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 12184 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 12185 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 12186 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 12187 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 12188 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 12189 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12190 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12191 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 12192 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 12193 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 12194 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 12195 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 12196 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 12197 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12198 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12199 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12200 | |
| 12201 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12202 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12203 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 12204 | /* |
| 12205 | * |
| 12206 | * Register class HTTPMessage |
| 12207 | * |
| 12208 | */ |
| 12209 | |
| 12210 | /* Create and fill the metatable. */ |
| 12211 | lua_newtable(L); |
| 12212 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 12213 | /* Create and fill the __index entry. */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 12214 | lua_pushstring(L, "__index"); |
| 12215 | lua_newtable(L); |
| 12216 | |
| 12217 | /* Register Lua functions. */ |
| 12218 | hlua_class_function(L, "is_resp", hlua_http_msg_is_resp); |
| 12219 | hlua_class_function(L, "get_stline", hlua_http_msg_get_stline); |
| 12220 | hlua_class_function(L, "get_headers", hlua_http_msg_get_headers); |
| 12221 | hlua_class_function(L, "del_header", hlua_http_msg_del_hdr); |
| 12222 | hlua_class_function(L, "rep_header", hlua_http_msg_rep_hdr); |
| 12223 | hlua_class_function(L, "rep_value", hlua_http_msg_rep_val); |
| 12224 | hlua_class_function(L, "add_header", hlua_http_msg_add_hdr); |
| 12225 | hlua_class_function(L, "set_header", hlua_http_msg_set_hdr); |
| 12226 | hlua_class_function(L, "set_method", hlua_http_msg_set_meth); |
| 12227 | hlua_class_function(L, "set_path", hlua_http_msg_set_path); |
| 12228 | hlua_class_function(L, "set_query", hlua_http_msg_set_query); |
| 12229 | hlua_class_function(L, "set_uri", hlua_http_msg_set_uri); |
| 12230 | hlua_class_function(L, "set_status", hlua_http_msg_set_status); |
| 12231 | hlua_class_function(L, "is_full", hlua_http_msg_is_full); |
| 12232 | hlua_class_function(L, "may_recv", hlua_http_msg_may_recv); |
| 12233 | hlua_class_function(L, "eom", hlua_http_msg_is_eom); |
| 12234 | hlua_class_function(L, "input", hlua_http_msg_get_in_len); |
| 12235 | hlua_class_function(L, "output", hlua_http_msg_get_out_len); |
| 12236 | |
| 12237 | hlua_class_function(L, "body", hlua_http_msg_get_body); |
| 12238 | hlua_class_function(L, "set", hlua_http_msg_set_data); |
| 12239 | hlua_class_function(L, "remove", hlua_http_msg_del_data); |
| 12240 | hlua_class_function(L, "append", hlua_http_msg_append); |
| 12241 | hlua_class_function(L, "prepend", hlua_http_msg_prepend); |
| 12242 | hlua_class_function(L, "insert", hlua_http_msg_insert_data); |
| 12243 | hlua_class_function(L, "set_eom", hlua_http_msg_set_eom); |
| 12244 | hlua_class_function(L, "unset_eom", hlua_http_msg_unset_eom); |
| 12245 | |
| 12246 | hlua_class_function(L, "send", hlua_http_msg_send); |
| 12247 | hlua_class_function(L, "forward", hlua_http_msg_forward); |
| 12248 | |
| 12249 | lua_rawset(L, -3); |
| 12250 | |
| 12251 | /* Register previous table in the registry with reference and named entry. */ |
| 12252 | class_http_msg_ref = hlua_register_metatable(L, CLASS_HTTP_MSG); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12253 | |
| 12254 | /* |
| 12255 | * |
| 12256 | * Register class HTTPClient |
| 12257 | * |
| 12258 | */ |
| 12259 | |
| 12260 | /* Create and fill the metatable. */ |
| 12261 | lua_newtable(L); |
| 12262 | lua_pushstring(L, "__index"); |
| 12263 | lua_newtable(L); |
| 12264 | hlua_class_function(L, "get", hlua_httpclient_get); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 12265 | hlua_class_function(L, "head", hlua_httpclient_head); |
| 12266 | hlua_class_function(L, "put", hlua_httpclient_put); |
| 12267 | hlua_class_function(L, "post", hlua_httpclient_post); |
| 12268 | hlua_class_function(L, "delete", hlua_httpclient_delete); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12269 | lua_settable(L, -3); /* Sets the __index entry. */ |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 12270 | /* Register the garbage collector entry. */ |
| 12271 | lua_pushstring(L, "__gc"); |
| 12272 | lua_pushcclosure(L, hlua_httpclient_gc, 0); |
| 12273 | lua_settable(L, -3); /* Push the last 2 entries in the table at index -3 */ |
| 12274 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12275 | |
| 12276 | |
| 12277 | class_httpclient_ref = hlua_register_metatable(L, CLASS_HTTPCLIENT); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12278 | /* |
| 12279 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12280 | * Register class AppletTCP |
| 12281 | * |
| 12282 | */ |
| 12283 | |
| 12284 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12285 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12286 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12287 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12288 | lua_pushstring(L, "__index"); |
| 12289 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12290 | |
| 12291 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12292 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 12293 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 12294 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 12295 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 12296 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 12297 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 12298 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 12299 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12300 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12301 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12302 | |
| 12303 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12304 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12305 | |
| 12306 | /* |
| 12307 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12308 | * Register class AppletHTTP |
| 12309 | * |
| 12310 | */ |
| 12311 | |
| 12312 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12313 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12314 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12315 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12316 | lua_pushstring(L, "__index"); |
| 12317 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12318 | |
| 12319 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12320 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 12321 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 12322 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 12323 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 12324 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 12325 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 12326 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 12327 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 12328 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 12329 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 12330 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12331 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12332 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12333 | |
| 12334 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12335 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12336 | |
| 12337 | /* |
| 12338 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12339 | * Register class TXN |
| 12340 | * |
| 12341 | */ |
| 12342 | |
| 12343 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12344 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12345 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12346 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12347 | lua_pushstring(L, "__index"); |
| 12348 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12349 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 12350 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12351 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 12352 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 12353 | hlua_class_function(L, "set_var", hlua_set_var); |
| 12354 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 12355 | hlua_class_function(L, "get_var", hlua_get_var); |
| 12356 | hlua_class_function(L, "done", hlua_txn_done); |
| 12357 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 12358 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 12359 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 12360 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 12361 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 12362 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 12363 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 12364 | hlua_class_function(L, "log", hlua_txn_log); |
| 12365 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 12366 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 12367 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 12368 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 12369 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12370 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12371 | |
| 12372 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12373 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12374 | |
| 12375 | /* |
| 12376 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 12377 | * Register class reply |
| 12378 | * |
| 12379 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12380 | lua_newtable(L); |
| 12381 | lua_pushstring(L, "__index"); |
| 12382 | lua_newtable(L); |
| 12383 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 12384 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 12385 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 12386 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 12387 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 12388 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 12389 | |
| 12390 | |
| 12391 | /* |
| 12392 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12393 | * Register class Socket |
| 12394 | * |
| 12395 | */ |
| 12396 | |
| 12397 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12398 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12399 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12400 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12401 | lua_pushstring(L, "__index"); |
| 12402 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12403 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 12404 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12405 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 12406 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12407 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 12408 | hlua_class_function(L, "send", hlua_socket_send); |
| 12409 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 12410 | hlua_class_function(L, "close", hlua_socket_close); |
| 12411 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 12412 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 12413 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 12414 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12415 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12416 | 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] | 12417 | |
| 12418 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12419 | lua_pushstring(L, "__gc"); |
| 12420 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 12421 | 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] | 12422 | |
| 12423 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12424 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12425 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12426 | lua_atpanic(L, hlua_panic_safe); |
| 12427 | |
| 12428 | return L; |
| 12429 | } |
| 12430 | |
| 12431 | void hlua_init(void) { |
| 12432 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 12433 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12434 | #ifdef USE_OPENSSL |
| 12435 | struct srv_kw *kw; |
| 12436 | int tmp_error; |
| 12437 | char *error; |
| 12438 | char *args[] = { /* SSL client configuration. */ |
| 12439 | "ssl", |
| 12440 | "verify", |
| 12441 | "none", |
| 12442 | NULL |
| 12443 | }; |
| 12444 | #endif |
| 12445 | |
| 12446 | /* Init post init function list head */ |
| 12447 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 12448 | LIST_INIT(&hlua_init_functions[i]); |
| 12449 | |
| 12450 | /* Init state for common/shared lua parts */ |
| 12451 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12452 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12453 | hlua_states[0] = hlua_init_state(0); |
| 12454 | |
| 12455 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 12456 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12457 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12458 | hlua_states[1] = hlua_init_state(1); |
| 12459 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12460 | /* Proxy and server configuration initialisation. */ |
William Lallemand | 6bb77b9 | 2021-07-28 15:48:16 +0200 | [diff] [blame] | 12461 | 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] | 12462 | if (!socket_proxy) { |
| 12463 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 12464 | exit(1); |
| 12465 | } |
| 12466 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12467 | |
| 12468 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12469 | socket_tcp = new_server(socket_proxy); |
| 12470 | if (!socket_tcp) { |
| 12471 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 12472 | exit(1); |
| 12473 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12474 | |
| 12475 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12476 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12477 | socket_ssl = new_server(socket_proxy); |
| 12478 | if (!socket_ssl) { |
| 12479 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 12480 | exit(1); |
| 12481 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12482 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12483 | socket_ssl->use_ssl = 1; |
| 12484 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12485 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 12486 | for (i = 0; args[i] != NULL; i++) { |
| 12487 | 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] | 12488 | /* |
| 12489 | * |
| 12490 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 12491 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12492 | * features like client certificates and ssl_verify. |
| 12493 | * |
| 12494 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12495 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12496 | if (tmp_error != 0) { |
| 12497 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 12498 | abort(); /* This must be never arrives because the command line |
| 12499 | not editable by the user. */ |
| 12500 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 12501 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12502 | } |
| 12503 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12504 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 12505 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 12506 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12507 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 12508 | static void hlua_deinit() |
| 12509 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 12510 | int thr; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12511 | struct hlua_reg_filter *reg_flt, *reg_flt_bck; |
| 12512 | |
| 12513 | list_for_each_entry_safe(reg_flt, reg_flt_bck, &referenced_filters, l) |
| 12514 | release_hlua_reg_filter(reg_flt); |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 12515 | |
| 12516 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 12517 | if (hlua_states[thr]) |
| 12518 | lua_close(hlua_states[thr]); |
| 12519 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 12520 | |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 12521 | srv_drop(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 12522 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 12523 | #ifdef USE_OPENSSL |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 12524 | srv_drop(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 12525 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 12526 | |
| 12527 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 12528 | } |
| 12529 | |
| 12530 | REGISTER_POST_DEINIT(hlua_deinit); |
| 12531 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12532 | static void hlua_register_build_options(void) |
| 12533 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12534 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12535 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12536 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 12537 | hap_register_build_opts(ptr, 1); |
| 12538 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12539 | |
| 12540 | INITCALL0(STG_REGISTER, hlua_register_build_options); |