Thierry Fournier | e726b14 | 2016-02-11 17:57:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Lua unsafe core engine |
| 3 | * |
| 4 | * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Bertrand Jacquin | f4c12d4 | 2021-01-21 21:14:07 +0000 | [diff] [blame] | 13 | #define _GNU_SOURCE |
| 14 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 15 | #include <ctype.h> |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 16 | #include <setjmp.h> |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 17 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 18 | #include <lauxlib.h> |
| 19 | #include <lua.h> |
| 20 | #include <lualib.h> |
| 21 | |
Thierry FOURNIER | 463119c | 2015-03-10 00:35:36 +0100 | [diff] [blame] | 22 | #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503 |
| 23 | #error "Requires Lua 5.3 or later." |
Cyril Bonté | dc0306e | 2015-03-02 00:08:40 +0100 | [diff] [blame] | 24 | #endif |
| 25 | |
Willy Tarreau | 8d2b777 | 2020-05-27 10:58:19 +0200 | [diff] [blame] | 26 | #include <import/ebpttree.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 27 | |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 28 | #include <haproxy/api.h> |
| 29 | #include <haproxy/applet.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 30 | #include <haproxy/arg.h> |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 31 | #include <haproxy/auth.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 32 | #include <haproxy/cfgparse.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 33 | #include <haproxy/channel.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 34 | #include <haproxy/cli.h> |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 35 | #include <haproxy/clock.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 36 | #include <haproxy/connection.h> |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 37 | #include <haproxy/conn_stream.h> |
| 38 | #include <haproxy/cs_utils.h> |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 39 | #include <haproxy/filters.h> |
Willy Tarreau | 5413a87 | 2020-06-02 19:33:08 +0200 | [diff] [blame] | 40 | #include <haproxy/h1.h> |
Willy Tarreau | 8641605 | 2020-06-04 09:20:54 +0200 | [diff] [blame] | 41 | #include <haproxy/hlua.h> |
Willy Tarreau | 8c79400 | 2020-06-04 10:05:25 +0200 | [diff] [blame] | 42 | #include <haproxy/hlua_fcn.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 43 | #include <haproxy/http_ana.h> |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 44 | #include <haproxy/http_client.h> |
Willy Tarreau | 126ba3a | 2020-06-04 18:26:43 +0200 | [diff] [blame] | 45 | #include <haproxy/http_fetch.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 46 | #include <haproxy/http_htx.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 47 | #include <haproxy/http_rules.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 48 | #include <haproxy/log.h> |
Willy Tarreau | 2cd5809 | 2020-06-04 15:10:43 +0200 | [diff] [blame] | 49 | #include <haproxy/map.h> |
Willy Tarreau | 8efbdfb | 2020-06-04 11:29:21 +0200 | [diff] [blame] | 50 | #include <haproxy/obj_type.h> |
Willy Tarreau | 225a90a | 2020-06-04 15:06:28 +0200 | [diff] [blame] | 51 | #include <haproxy/pattern.h> |
Willy Tarreau | 469509b | 2020-06-04 15:13:30 +0200 | [diff] [blame] | 52 | #include <haproxy/payload.h> |
Willy Tarreau | 3d6ee40 | 2021-05-08 20:28:07 +0200 | [diff] [blame] | 53 | #include <haproxy/proxy.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 54 | #include <haproxy/regex.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 55 | #include <haproxy/sample.h> |
Willy Tarreau | 198e92a | 2021-03-05 10:23:32 +0100 | [diff] [blame] | 56 | #include <haproxy/server.h> |
Willy Tarreau | 48d25b3 | 2020-06-04 18:58:52 +0200 | [diff] [blame] | 57 | #include <haproxy/session.h> |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 58 | #include <haproxy/ssl_ckch.h> |
| 59 | #include <haproxy/ssl_sock.h> |
Willy Tarreau | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 60 | #include <haproxy/stats-t.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 61 | #include <haproxy/stream.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 62 | #include <haproxy/task.h> |
Willy Tarreau | 8b550af | 2020-06-04 17:42:48 +0200 | [diff] [blame] | 63 | #include <haproxy/tcp_rules.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 64 | #include <haproxy/thread.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 65 | #include <haproxy/tools.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 66 | #include <haproxy/vars.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 67 | #include <haproxy/xref.h> |
| 68 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 69 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 70 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 71 | * macro is used only for identifying the function that can |
| 72 | * not return because a longjmp is executed. |
| 73 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 74 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 75 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 76 | */ |
| 77 | #define __LJMP |
Willy Tarreau | 4e7cc33 | 2018-10-20 17:45:48 +0200 | [diff] [blame] | 78 | #define WILL_LJMP(func) do { func; my_unreachable(); } while(0) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 79 | #define MAY_LJMP(func) func |
| 80 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 81 | /* This couple of function executes securely some Lua calls outside of |
| 82 | * the lua runtime environment. Each Lua call can return a longjmp |
| 83 | * if it encounter a memory error. |
| 84 | * |
| 85 | * Lua documentation extract: |
| 86 | * |
| 87 | * If an error happens outside any protected environment, Lua calls |
| 88 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 89 | * exiting the host application. Your panic function can avoid this |
| 90 | * exit by never returning (e.g., doing a long jump to your own |
| 91 | * recovery point outside Lua). |
| 92 | * |
| 93 | * The panic function runs as if it were a message handler (see |
Willy Tarreau | 3dfb7da | 2022-03-02 22:33:39 +0100 | [diff] [blame] | 94 | * #2.3); in particular, the error message is at the top of the |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 95 | * stack. However, there is no guarantee about stack space. To push |
| 96 | * anything on the stack, the panic function must first check the |
Willy Tarreau | 3dfb7da | 2022-03-02 22:33:39 +0100 | [diff] [blame] | 97 | * available space (see #4.2). |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 98 | * |
| 99 | * We must check all the Lua entry point. This includes: |
| 100 | * - The include/proto/hlua.h exported functions |
| 101 | * - the task wrapper function |
| 102 | * - The action wrapper function |
| 103 | * - The converters wrapper function |
| 104 | * - The sample-fetch wrapper functions |
| 105 | * |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 106 | * It is tolerated that the initialisation function returns an abort. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 107 | * Before each Lua abort, an error message is written on stderr. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 108 | * |
| 109 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 110 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 111 | * because they must be exists in the program stack when the longjmp |
| 112 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 113 | * |
| 114 | * Note that the Lua processing is not really thread safe. It provides |
| 115 | * heavy system which consists to add our own lock function in the Lua |
| 116 | * code and recompile the library. This system will probably not accepted |
| 117 | * by maintainers of various distribs. |
| 118 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 119 | * Our main execution point of the Lua is the function lua_resume(). A |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 120 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 121 | * of function and a lua_unlock() at the end of the function. So I |
| 122 | * conclude that the Lua thread safe mode just perform a mutex around |
| 123 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 124 | * easier for distro maintainers. |
| 125 | * |
| 126 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 127 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 128 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 129 | */ |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 130 | __decl_spinlock(hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 131 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 132 | static int hlua_panic_safe(lua_State *L) { return 0; } |
Willy Tarreau | 6a51090 | 2021-07-14 19:41:25 +0200 | [diff] [blame] | 133 | static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); return 0; } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 134 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 135 | /* This is the chained list of struct hlua_function referenced |
| 136 | * for haproxy action, sample-fetches, converters, cli and |
| 137 | * applet bindings. It is used for a post-initialisation control. |
| 138 | */ |
| 139 | static struct list referenced_functions = LIST_HEAD_INIT(referenced_functions); |
| 140 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 141 | /* This variable is used only during initialization to identify the Lua state |
| 142 | * currently being initialized. 0 is the common lua state, 1 to n are the Lua |
| 143 | * states dedicated to each thread (in this case hlua_state_id==tid+1). |
| 144 | */ |
| 145 | static int hlua_state_id; |
| 146 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 147 | /* This is a NULL-terminated list of lua file which are referenced to load per thread */ |
| 148 | static char **per_thread_load = NULL; |
| 149 | |
| 150 | lua_State *hlua_init_state(int thread_id); |
| 151 | |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 152 | /* This function takes the Lua global lock. Keep this function's visibility |
| 153 | * global so that it can appear in stack dumps and performance profiles! |
| 154 | */ |
| 155 | void lua_take_global_lock() |
| 156 | { |
| 157 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
| 158 | } |
| 159 | |
| 160 | static inline void lua_drop_global_lock() |
| 161 | { |
| 162 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
| 163 | } |
| 164 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 165 | #define SET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 166 | ({ \ |
| 167 | int ret; \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 168 | if ((__HLUA)->state_id == 0) \ |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 169 | lua_take_global_lock(); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 170 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 171 | lua_atpanic(__L, hlua_panic_safe); \ |
| 172 | ret = 0; \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 173 | if ((__HLUA)->state_id == 0) \ |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 174 | lua_drop_global_lock(); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 175 | } else { \ |
| 176 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 177 | ret = 1; \ |
| 178 | } \ |
| 179 | ret; \ |
| 180 | }) |
| 181 | |
| 182 | /* If we are the last function catching Lua errors, we |
| 183 | * must reset the panic function. |
| 184 | */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 185 | #define RESET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 186 | do { \ |
| 187 | lua_atpanic(__L, hlua_panic_safe); \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 188 | if ((__HLUA)->state_id == 0) \ |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 189 | lua_drop_global_lock(); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 190 | } while(0) |
| 191 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 192 | #define SET_SAFE_LJMP(__HLUA) \ |
| 193 | SET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 194 | |
| 195 | #define RESET_SAFE_LJMP(__HLUA) \ |
| 196 | RESET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 197 | |
| 198 | #define SET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 199 | SET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 200 | |
| 201 | #define RESET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 202 | RESET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 203 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 204 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 205 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 206 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 207 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 208 | /* unused: 0x08, 0x10 */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 209 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 210 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 211 | |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 212 | /* The main Lua execution context. The 0 index is the |
| 213 | * common state shared by all threads. |
| 214 | */ |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 215 | static lua_State *hlua_states[MAX_THREADS + 1]; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 216 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 217 | #define HLUA_FLT_CB_FINAL 0x00000001 |
| 218 | #define HLUA_FLT_CB_RETVAL 0x00000002 |
| 219 | #define HLUA_FLT_CB_ARG_CHN 0x00000004 |
| 220 | #define HLUA_FLT_CB_ARG_HTTP_MSG 0x00000008 |
| 221 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 222 | #define HLUA_FLT_CTX_FL_PAYLOAD 0x00000001 |
| 223 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 224 | struct hlua_reg_filter { |
| 225 | char *name; |
| 226 | int flt_ref[MAX_THREADS + 1]; |
| 227 | int fun_ref[MAX_THREADS + 1]; |
| 228 | struct list l; |
| 229 | }; |
| 230 | |
| 231 | struct hlua_flt_config { |
| 232 | struct hlua_reg_filter *reg; |
| 233 | int ref[MAX_THREADS + 1]; |
| 234 | char **args; |
| 235 | }; |
| 236 | |
| 237 | struct hlua_flt_ctx { |
| 238 | int ref; /* ref to the filter lua object */ |
| 239 | struct hlua *hlua[2]; /* lua runtime context (0: request, 1: response) */ |
| 240 | unsigned int cur_off[2]; /* current offset (0: request, 1: response) */ |
| 241 | unsigned int cur_len[2]; /* current forwardable length (0: request, 1: response) */ |
| 242 | unsigned int flags; /* HLUA_FLT_CTX_FL_* */ |
| 243 | }; |
| 244 | |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 245 | /* used by registered CLI keywords */ |
| 246 | struct hlua_cli_ctx { |
| 247 | struct hlua *hlua; |
| 248 | struct task *task; |
| 249 | struct hlua_function *fcn; |
| 250 | }; |
| 251 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 252 | DECLARE_STATIC_POOL(pool_head_hlua_flt_ctx, "hlua_flt_ctx", sizeof(struct hlua_flt_ctx)); |
| 253 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 254 | static int hlua_filter_from_payload(struct filter *filter); |
| 255 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 256 | /* This is the chained list of struct hlua_flt referenced |
| 257 | * for haproxy filters. It is used for a post-initialisation control. |
| 258 | */ |
| 259 | static struct list referenced_filters = LIST_HEAD_INIT(referenced_filters); |
| 260 | |
| 261 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 262 | /* This is the memory pool containing struct lua for applets |
| 263 | * (including cli). |
| 264 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 265 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 266 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 267 | /* Used for Socket connection. */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 268 | static struct proxy *socket_proxy; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 269 | static struct server *socket_tcp; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 270 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 271 | static struct server *socket_ssl; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 272 | #endif |
| 273 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 274 | /* List head of the function called at the initialisation time. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 275 | struct list hlua_init_functions[MAX_THREADS + 1]; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 276 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 277 | /* The following variables contains the reference of the different |
| 278 | * Lua classes. These references are useful for identify metadata |
| 279 | * associated with an object. |
| 280 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 281 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 282 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 283 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 284 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 285 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 286 | static int class_http_ref; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 287 | static int class_http_msg_ref; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 288 | static int class_httpclient_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 289 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 290 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 291 | static int class_applet_http_ref; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 292 | static int class_txn_reply_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 293 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 294 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 295 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 296 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 297 | * because a task may remain alive during all the haproxy execution. |
| 298 | */ |
| 299 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 300 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 301 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 302 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 303 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 304 | * it is used for preventing infinite loops. |
| 305 | * |
| 306 | * I test the scheer with an infinite loop containing one incrementation |
| 307 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 308 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 309 | * to one interrupt each 10 000 instructions. |
| 310 | * |
| 311 | * configured | Number of |
| 312 | * instructions | loops executed |
| 313 | * between two | in milions |
| 314 | * forced yields | |
| 315 | * ---------------+--------------- |
| 316 | * 10 | 160 |
| 317 | * 500 | 670 |
| 318 | * 1000 | 680 |
| 319 | * 5000 | 700 |
| 320 | * 7000 | 700 |
| 321 | * 8000 | 700 |
| 322 | * 9000 | 710 <- ceil |
| 323 | * 10000 | 710 |
| 324 | * 100000 | 710 |
| 325 | * 1000000 | 710 |
| 326 | * |
| 327 | */ |
| 328 | static unsigned int hlua_nb_instruction = 10000; |
| 329 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 330 | /* Descriptor for the memory allocation state. The limit is pre-initialised to |
| 331 | * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it |
| 332 | * is replaced with ~0 during post_init after everything was loaded. This way |
| 333 | * it is guaranteed that if limit is ~0 the boot is complete and that if it's |
| 334 | * zero it's not yet limited and proper accounting is required. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 335 | */ |
| 336 | struct hlua_mem_allocator { |
| 337 | size_t allocated; |
| 338 | size_t limit; |
| 339 | }; |
| 340 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 341 | static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 342 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 343 | /* These functions converts types between HAProxy internal args or |
| 344 | * sample and LUA types. Another function permits to check if the |
| 345 | * LUA stack contains arguments according with an required ARG_T |
| 346 | * format. |
| 347 | */ |
| 348 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 349 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 350 | __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] | 351 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 352 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 353 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 354 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 355 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 356 | __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] | 357 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 358 | struct prepend_path { |
| 359 | struct list l; |
| 360 | char *type; |
| 361 | char *path; |
| 362 | }; |
| 363 | |
| 364 | static struct list prepend_path_list = LIST_HEAD_INIT(prepend_path_list); |
| 365 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 366 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 367 | do { \ |
| 368 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 369 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 370 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 371 | } while (0) |
| 372 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 373 | static inline struct hlua_function *new_hlua_function() |
| 374 | { |
| 375 | struct hlua_function *fcn; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 376 | int i; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 377 | |
| 378 | fcn = calloc(1, sizeof(*fcn)); |
| 379 | if (!fcn) |
| 380 | return NULL; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 381 | LIST_APPEND(&referenced_functions, &fcn->l); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 382 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 383 | fcn->function_ref[i] = -1; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 384 | return fcn; |
| 385 | } |
| 386 | |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 387 | static inline void release_hlua_function(struct hlua_function *fcn) |
| 388 | { |
| 389 | if (!fcn) |
| 390 | return; |
| 391 | if (fcn->name) |
| 392 | ha_free(&fcn->name); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 393 | LIST_DELETE(&fcn->l); |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 394 | ha_free(&fcn); |
| 395 | } |
| 396 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 397 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 398 | static inline int fcn_ref_to_stack_id(struct hlua_function *fcn) |
| 399 | { |
| 400 | if (fcn->function_ref[0] == -1) |
| 401 | return tid + 1; |
| 402 | return 0; |
| 403 | } |
| 404 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 405 | /* Create a new registered filter. Only its name is filled */ |
| 406 | static inline struct hlua_reg_filter *new_hlua_reg_filter(const char *name) |
| 407 | { |
| 408 | struct hlua_reg_filter *reg_flt; |
| 409 | int i; |
| 410 | |
| 411 | reg_flt = calloc(1, sizeof(*reg_flt)); |
| 412 | if (!reg_flt) |
| 413 | return NULL; |
| 414 | reg_flt->name = strdup(name); |
| 415 | if (!reg_flt->name) { |
| 416 | free(reg_flt); |
| 417 | return NULL; |
| 418 | } |
| 419 | LIST_APPEND(&referenced_filters, ®_flt->l); |
| 420 | for (i = 0; i < MAX_THREADS + 1; i++) { |
| 421 | reg_flt->flt_ref[i] = -1; |
| 422 | reg_flt->fun_ref[i] = -1; |
| 423 | } |
| 424 | return reg_flt; |
| 425 | } |
| 426 | |
| 427 | /* Release a registered filter */ |
| 428 | static inline void release_hlua_reg_filter(struct hlua_reg_filter *reg_flt) |
| 429 | { |
| 430 | if (!reg_flt) |
| 431 | return; |
| 432 | if (reg_flt->name) |
| 433 | ha_free(®_flt->name); |
| 434 | LIST_DELETE(®_flt->l); |
| 435 | ha_free(®_flt); |
| 436 | } |
| 437 | |
| 438 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 439 | static inline int reg_flt_to_stack_id(struct hlua_reg_filter *reg_flt) |
| 440 | { |
| 441 | if (reg_flt->fun_ref[0] == -1) |
| 442 | return tid + 1; |
| 443 | return 0; |
| 444 | } |
| 445 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 446 | /* Used to check an Lua function type in the stack. It creates and |
| 447 | * returns a reference of the function. This function throws an |
| 448 | * error if the rgument is not a "function". |
| 449 | */ |
| 450 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 451 | { |
| 452 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 453 | 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] | 454 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 455 | } |
| 456 | lua_pushvalue(L, argno); |
| 457 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 458 | } |
| 459 | |
Christopher Faulet | ba9e21d | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 460 | /* Used to check an Lua table type in the stack. It creates and |
| 461 | * returns a reference of the table. This function throws an |
| 462 | * error if the rgument is not a "table". |
| 463 | */ |
| 464 | __LJMP unsigned int hlua_checktable(lua_State *L, int argno) |
| 465 | { |
| 466 | if (!lua_istable(L, argno)) { |
| 467 | const char *msg = lua_pushfstring(L, "table expected, got %s", luaL_typename(L, argno)); |
| 468 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 469 | } |
| 470 | lua_pushvalue(L, argno); |
| 471 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 472 | } |
| 473 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 474 | /* Return the string that is of the top of the stack. */ |
| 475 | const char *hlua_get_top_error_string(lua_State *L) |
| 476 | { |
| 477 | if (lua_gettop(L) < 1) |
| 478 | return "unknown error"; |
| 479 | if (lua_type(L, -1) != LUA_TSTRING) |
| 480 | return "unknown error"; |
| 481 | return lua_tostring(L, -1); |
| 482 | } |
| 483 | |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 484 | __LJMP const char *hlua_traceback(lua_State *L, const char* sep) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 485 | { |
| 486 | lua_Debug ar; |
| 487 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 488 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 489 | |
| 490 | while (lua_getstack(L, level++, &ar)) { |
| 491 | |
| 492 | /* Add separator */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 493 | if (b_data(msg)) |
| 494 | chunk_appendf(msg, "%s", sep); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 495 | |
| 496 | /* Fill fields: |
| 497 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 498 | * 'l': fills in the field currentline; |
| 499 | * 'n': fills in the field name and namewhat; |
| 500 | * 't': fills in the field istailcall; |
| 501 | */ |
| 502 | lua_getinfo(L, "Slnt", &ar); |
| 503 | |
| 504 | /* Append code localisation */ |
| 505 | if (ar.currentline > 0) |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 506 | chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 507 | else |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 508 | chunk_appendf(msg, "%s: ", ar.short_src); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 509 | |
| 510 | /* |
| 511 | * Get function name |
| 512 | * |
| 513 | * if namewhat is no empty, name is defined. |
| 514 | * what contains "Lua" for Lua function, "C" for C function, |
| 515 | * or "main" for main code. |
| 516 | */ |
| 517 | 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] | 518 | chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */ |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 519 | |
| 520 | 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] | 521 | chunk_appendf(msg, "in main chunk"); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 522 | |
| 523 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 524 | chunk_appendf(msg, "in function line %d", ar.linedefined); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 525 | |
| 526 | else /* nothing left... */ |
| 527 | chunk_appendf(msg, "?"); |
| 528 | |
| 529 | |
| 530 | /* Display tailed call */ |
| 531 | if (ar.istailcall) |
| 532 | chunk_appendf(msg, " ..."); |
| 533 | } |
| 534 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 535 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 539 | /* This function check the number of arguments available in the |
| 540 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 541 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 542 | */ |
| 543 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 544 | { |
| 545 | if (lua_gettop(L) == nb) |
| 546 | return; |
| 547 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 548 | } |
| 549 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 550 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 551 | * and the line number where the error is encountered. |
| 552 | */ |
| 553 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 554 | { |
| 555 | va_list argp; |
| 556 | va_start(argp, fmt); |
| 557 | luaL_where(L, 1); |
| 558 | lua_pushvfstring(L, fmt, argp); |
| 559 | va_end(argp); |
| 560 | lua_concat(L, 2); |
| 561 | return 1; |
| 562 | } |
| 563 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 564 | /* This functions is used with sample fetch and converters. It |
| 565 | * converts the HAProxy configuration argument in a lua stack |
| 566 | * values. |
| 567 | * |
| 568 | * It takes an array of "arg", and each entry of the array is |
| 569 | * converted and pushed in the LUA stack. |
| 570 | */ |
| 571 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 572 | { |
| 573 | switch (arg->type) { |
| 574 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 575 | case ARGT_TIME: |
| 576 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 577 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 578 | break; |
| 579 | |
| 580 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 581 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 582 | break; |
| 583 | |
| 584 | case ARGT_IPV4: |
| 585 | case ARGT_IPV6: |
| 586 | case ARGT_MSK4: |
| 587 | case ARGT_MSK6: |
| 588 | case ARGT_FE: |
| 589 | case ARGT_BE: |
| 590 | case ARGT_TAB: |
| 591 | case ARGT_SRV: |
| 592 | case ARGT_USR: |
| 593 | case ARGT_MAP: |
| 594 | default: |
| 595 | lua_pushnil(L); |
| 596 | break; |
| 597 | } |
| 598 | return 1; |
| 599 | } |
| 600 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 601 | /* 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] | 602 | * 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] | 603 | * with sample fetch wrappers. The input arguments are given to the |
| 604 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 605 | */ |
| 606 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 607 | { |
| 608 | switch (lua_type(L, ud)) { |
| 609 | |
| 610 | case LUA_TNUMBER: |
| 611 | case LUA_TBOOLEAN: |
| 612 | arg->type = ARGT_SINT; |
| 613 | arg->data.sint = lua_tointeger(L, ud); |
| 614 | break; |
| 615 | |
| 616 | case LUA_TSTRING: |
| 617 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 618 | 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] | 619 | /* 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] | 620 | 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] | 621 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 622 | break; |
| 623 | |
| 624 | case LUA_TUSERDATA: |
| 625 | case LUA_TNIL: |
| 626 | case LUA_TTABLE: |
| 627 | case LUA_TFUNCTION: |
| 628 | case LUA_TTHREAD: |
| 629 | case LUA_TLIGHTUSERDATA: |
| 630 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 631 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 632 | break; |
| 633 | } |
| 634 | return 1; |
| 635 | } |
| 636 | |
| 637 | /* the following functions are used to convert a struct sample |
| 638 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 639 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 640 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 641 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 642 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 643 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 644 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 645 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 646 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 647 | break; |
| 648 | |
| 649 | case SMP_T_BIN: |
| 650 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 651 | 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] | 652 | break; |
| 653 | |
| 654 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 655 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 656 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 657 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 658 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 659 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 660 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 661 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 662 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 663 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 664 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 665 | 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] | 666 | break; |
| 667 | default: |
| 668 | lua_pushnil(L); |
| 669 | break; |
| 670 | } |
| 671 | break; |
| 672 | |
| 673 | case SMP_T_IPV4: |
| 674 | case SMP_T_IPV6: |
| 675 | 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] | 676 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 677 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 678 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 679 | else |
| 680 | lua_pushnil(L); |
| 681 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 682 | default: |
| 683 | lua_pushnil(L); |
| 684 | break; |
| 685 | } |
| 686 | return 1; |
| 687 | } |
| 688 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 689 | /* the following functions are used to convert a struct sample |
| 690 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 691 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 692 | */ |
| 693 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 694 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 695 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 696 | |
| 697 | case SMP_T_BIN: |
| 698 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 699 | 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] | 700 | break; |
| 701 | |
| 702 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 703 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 704 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 705 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 706 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 707 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 708 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 709 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 710 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 711 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 712 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 713 | 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] | 714 | break; |
| 715 | default: |
| 716 | lua_pushstring(L, ""); |
| 717 | break; |
| 718 | } |
| 719 | break; |
| 720 | |
| 721 | case SMP_T_SINT: |
| 722 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 723 | case SMP_T_IPV4: |
| 724 | case SMP_T_IPV6: |
| 725 | 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] | 726 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 727 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 728 | 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] | 729 | else |
| 730 | lua_pushstring(L, ""); |
| 731 | break; |
| 732 | default: |
| 733 | lua_pushstring(L, ""); |
| 734 | break; |
| 735 | } |
| 736 | return 1; |
| 737 | } |
| 738 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 739 | /* the following functions are used to convert an Lua type in a |
| 740 | * struct sample. This is useful to provide data from a converter |
| 741 | * to the LUA code. |
| 742 | */ |
| 743 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 744 | { |
| 745 | switch (lua_type(L, ud)) { |
| 746 | |
| 747 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 748 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 749 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 750 | break; |
| 751 | |
| 752 | |
| 753 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 754 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 755 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 756 | break; |
| 757 | |
| 758 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 759 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 760 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 761 | 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] | 762 | /* 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] | 763 | 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] | 764 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 765 | break; |
| 766 | |
| 767 | case LUA_TUSERDATA: |
| 768 | case LUA_TNIL: |
| 769 | case LUA_TTABLE: |
| 770 | case LUA_TFUNCTION: |
| 771 | case LUA_TTHREAD: |
| 772 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 773 | case LUA_TNONE: |
| 774 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 775 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 776 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 777 | break; |
| 778 | } |
| 779 | return 1; |
| 780 | } |
| 781 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 782 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 783 | * 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] | 784 | * 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] | 785 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 786 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 787 | * 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] | 788 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 789 | __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] | 790 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 791 | { |
| 792 | int min_arg; |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 793 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 794 | struct proxy *px; |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 795 | struct userlist *ul; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 796 | struct my_regex *reg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 797 | const char *msg = NULL; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 798 | char *sname, *pname, *err = NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 799 | |
| 800 | idx = 0; |
| 801 | min_arg = ARGM(mask); |
| 802 | mask >>= ARGM_BITS; |
| 803 | |
| 804 | while (1) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 805 | struct buffer tmp = BUF_NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 806 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 807 | /* Check for mandatory arguments. */ |
| 808 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 809 | if (idx < min_arg) { |
| 810 | |
| 811 | /* If miss other argument than the first one, we return an error. */ |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 812 | if (idx > 0) { |
| 813 | msg = "Mandatory argument expected"; |
| 814 | goto error; |
| 815 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 816 | |
| 817 | /* If first argument have a certain type, some default values |
| 818 | * may be used. See the function smp_resolve_args(). |
| 819 | */ |
| 820 | switch (mask & ARGT_MASK) { |
| 821 | |
| 822 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 823 | if (!(p->cap & PR_CAP_FE)) { |
| 824 | msg = "Mandatory argument expected"; |
| 825 | goto error; |
| 826 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 827 | argp[idx].data.prx = p; |
| 828 | argp[idx].type = ARGT_FE; |
| 829 | argp[idx+1].type = ARGT_STOP; |
| 830 | break; |
| 831 | |
| 832 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 833 | if (!(p->cap & PR_CAP_BE)) { |
| 834 | msg = "Mandatory argument expected"; |
| 835 | goto error; |
| 836 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 837 | argp[idx].data.prx = p; |
| 838 | argp[idx].type = ARGT_BE; |
| 839 | argp[idx+1].type = ARGT_STOP; |
| 840 | break; |
| 841 | |
| 842 | case ARGT_TAB: |
| 843 | argp[idx].data.prx = p; |
| 844 | argp[idx].type = ARGT_TAB; |
| 845 | argp[idx+1].type = ARGT_STOP; |
| 846 | break; |
| 847 | |
| 848 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 849 | msg = "Mandatory argument expected"; |
| 850 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 851 | break; |
| 852 | } |
| 853 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 854 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 855 | } |
| 856 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 857 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 858 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 859 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 860 | msg = "Last argument expected"; |
| 861 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 865 | argp[idx].type == ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 866 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 867 | } |
| 868 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 869 | /* Convert some argument types. All string in argp[] are for not |
| 870 | * duplicated yet. |
| 871 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 872 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 873 | case ARGT_SINT: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 874 | if (argp[idx].type != ARGT_SINT) { |
| 875 | msg = "integer expected"; |
| 876 | goto error; |
| 877 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 878 | argp[idx].type = ARGT_SINT; |
| 879 | break; |
| 880 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 881 | case ARGT_TIME: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 882 | if (argp[idx].type != ARGT_SINT) { |
| 883 | msg = "integer expected"; |
| 884 | goto error; |
| 885 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 886 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 887 | break; |
| 888 | |
| 889 | case ARGT_SIZE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 890 | if (argp[idx].type != ARGT_SINT) { |
| 891 | msg = "integer expected"; |
| 892 | goto error; |
| 893 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 894 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 895 | break; |
| 896 | |
| 897 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 898 | if (argp[idx].type != ARGT_STR) { |
| 899 | msg = "string expected"; |
| 900 | goto error; |
| 901 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 902 | argp[idx].data.prx = proxy_fe_by_name(argp[idx].data.str.area); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 903 | if (!argp[idx].data.prx) { |
| 904 | msg = "frontend doesn't exist"; |
| 905 | goto error; |
| 906 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 907 | argp[idx].type = ARGT_FE; |
| 908 | break; |
| 909 | |
| 910 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 911 | if (argp[idx].type != ARGT_STR) { |
| 912 | msg = "string expected"; |
| 913 | goto error; |
| 914 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 915 | 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] | 916 | if (!argp[idx].data.prx) { |
| 917 | msg = "backend doesn't exist"; |
| 918 | goto error; |
| 919 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 920 | argp[idx].type = ARGT_BE; |
| 921 | break; |
| 922 | |
| 923 | case ARGT_TAB: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 924 | if (argp[idx].type != ARGT_STR) { |
| 925 | msg = "string expected"; |
| 926 | goto error; |
| 927 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 928 | 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] | 929 | if (!argp[idx].data.t) { |
| 930 | msg = "table doesn't exist"; |
| 931 | goto error; |
| 932 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 933 | argp[idx].type = ARGT_TAB; |
| 934 | break; |
| 935 | |
| 936 | case ARGT_SRV: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 937 | if (argp[idx].type != ARGT_STR) { |
| 938 | msg = "string expected"; |
| 939 | goto error; |
| 940 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 941 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 942 | if (sname) { |
| 943 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 944 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 945 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 946 | if (!px) { |
| 947 | msg = "backend doesn't exist"; |
| 948 | goto error; |
| 949 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 950 | } |
| 951 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 952 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 953 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 954 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 955 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 956 | if (!argp[idx].data.srv) { |
| 957 | msg = "server doesn't exist"; |
| 958 | goto error; |
| 959 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 960 | argp[idx].type = ARGT_SRV; |
| 961 | break; |
| 962 | |
| 963 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 964 | if (argp[idx].type != ARGT_STR) { |
| 965 | msg = "string expected"; |
| 966 | goto error; |
| 967 | } |
| 968 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 969 | msg = "invalid IPv4 address"; |
| 970 | goto error; |
| 971 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 972 | argp[idx].type = ARGT_IPV4; |
| 973 | break; |
| 974 | |
| 975 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 976 | if (argp[idx].type == ARGT_SINT) |
| 977 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 978 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 979 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 980 | msg = "invalid IPv4 mask"; |
| 981 | goto error; |
| 982 | } |
| 983 | } |
| 984 | else { |
| 985 | msg = "integer or string expected"; |
| 986 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 987 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 988 | argp[idx].type = ARGT_MSK4; |
| 989 | break; |
| 990 | |
| 991 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 992 | if (argp[idx].type != ARGT_STR) { |
| 993 | msg = "string expected"; |
| 994 | goto error; |
| 995 | } |
| 996 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 997 | msg = "invalid IPv6 address"; |
| 998 | goto error; |
| 999 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1000 | argp[idx].type = ARGT_IPV6; |
| 1001 | break; |
| 1002 | |
| 1003 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1004 | if (argp[idx].type == ARGT_SINT) |
| 1005 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 1006 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1007 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 1008 | msg = "invalid IPv6 mask"; |
| 1009 | goto error; |
| 1010 | } |
| 1011 | } |
| 1012 | else { |
| 1013 | msg = "integer or string expected"; |
| 1014 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1015 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 1016 | argp[idx].type = ARGT_MSK6; |
| 1017 | break; |
| 1018 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 1019 | case ARGT_REG: |
| 1020 | if (argp[idx].type != ARGT_STR) { |
| 1021 | msg = "string expected"; |
| 1022 | goto error; |
| 1023 | } |
| 1024 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 1025 | if (!reg) { |
| 1026 | msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'", |
| 1027 | argp[idx].data.str.area, err); |
| 1028 | free(err); |
| 1029 | goto error; |
| 1030 | } |
| 1031 | argp[idx].type = ARGT_REG; |
| 1032 | argp[idx].data.reg = reg; |
| 1033 | break; |
| 1034 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1035 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1036 | if (argp[idx].type != ARGT_STR) { |
| 1037 | msg = "string expected"; |
| 1038 | goto error; |
| 1039 | } |
| 1040 | if (p->uri_auth && p->uri_auth->userlist && |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1041 | strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0) |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1042 | ul = p->uri_auth->userlist; |
| 1043 | else |
| 1044 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 1045 | |
| 1046 | if (!ul) { |
| 1047 | msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area); |
| 1048 | goto error; |
| 1049 | } |
| 1050 | argp[idx].type = ARGT_USR; |
| 1051 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1052 | break; |
| 1053 | |
| 1054 | case ARGT_STR: |
| 1055 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 1056 | msg = "unable to duplicate string arg"; |
| 1057 | goto error; |
| 1058 | } |
| 1059 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1060 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1061 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1062 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1063 | msg = "type not yet supported"; |
| 1064 | goto error; |
| 1065 | break; |
| 1066 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | /* Check for type of argument. */ |
| 1070 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1071 | msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 1072 | arg_type_names[(mask & ARGT_MASK)], |
| 1073 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 1074 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | /* Next argument. */ |
| 1078 | mask >>= ARGT_BITS; |
| 1079 | idx++; |
| 1080 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1081 | return 0; |
| 1082 | |
| 1083 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 1084 | free_args(argp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1085 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 1086 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1087 | } |
| 1088 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1089 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1090 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1091 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1092 | * |
| 1093 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1094 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 1095 | */ |
| 1096 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 1097 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1098 | struct hlua **hlua = lua_getextraspace(L); |
| 1099 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1100 | } |
| 1101 | static inline void hlua_sethlua(struct hlua *hlua) |
| 1102 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1103 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 1104 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1105 | } |
| 1106 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1107 | /* This function is used to send logs. It try to send on screen (stderr) |
| 1108 | * and on the default syslog server. |
| 1109 | */ |
| 1110 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 1111 | { |
| 1112 | struct tm tm; |
| 1113 | char *p; |
| 1114 | |
| 1115 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1116 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1117 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1118 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 1119 | /* Break the message if exceed the buffer size. */ |
| 1120 | *(p-4) = ' '; |
| 1121 | *(p-3) = '.'; |
| 1122 | *(p-2) = '.'; |
| 1123 | *(p-1) = '.'; |
| 1124 | break; |
| 1125 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1126 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1127 | *p = *msg; |
| 1128 | else |
| 1129 | *p = '.'; |
| 1130 | } |
| 1131 | *p = '\0'; |
| 1132 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1133 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1134 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 1135 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 1136 | return; |
| 1137 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 1138 | get_localtime(date.tv_sec, &tm); |
| 1139 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1140 | 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] | 1141 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1142 | fflush(stderr); |
| 1143 | } |
| 1144 | } |
| 1145 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1146 | /* This function just ensure that the yield will be always |
| 1147 | * returned with a timeout and permit to set some flags |
| 1148 | */ |
| 1149 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1150 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1151 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1152 | struct hlua *hlua; |
| 1153 | |
| 1154 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1155 | hlua = hlua_gethlua(L); |
| 1156 | if (!hlua) { |
| 1157 | return; |
| 1158 | } |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1159 | |
| 1160 | /* Set the wake timeout. If timeout is required, we set |
| 1161 | * the expiration time. |
| 1162 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1163 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1164 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1165 | hlua->flags |= flags; |
| 1166 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1167 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1168 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1169 | } |
| 1170 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1171 | /* This function initialises the Lua environment stored in the stream. |
| 1172 | * 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] | 1173 | * 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] | 1174 | * |
| 1175 | * This function is particular. it initialises a new Lua thread. If the |
| 1176 | * initialisation fails (example: out of memory error), the lua function |
| 1177 | * throws an error (longjmp). |
| 1178 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1179 | * 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] | 1180 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1181 | * threads appear, the safe environment set a lock to ensure only one |
| 1182 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1183 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1184 | * |
| 1185 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1186 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1187 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1188 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1189 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1190 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1191 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1192 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1193 | 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] | 1194 | { |
| 1195 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1196 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1197 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1198 | lua->wake_time = TICK_ETERNITY; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1199 | lua->state_id = state_id; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1200 | LIST_INIT(&lua->com); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1201 | if (!already_safe) { |
| 1202 | if (!SET_SAFE_LJMP_PARENT(lua)) { |
| 1203 | lua->Tref = LUA_REFNIL; |
| 1204 | return 0; |
| 1205 | } |
| 1206 | } |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1207 | lua->T = lua_newthread(hlua_states[state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1208 | if (!lua->T) { |
| 1209 | lua->Tref = LUA_REFNIL; |
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 0; |
| 1213 | } |
| 1214 | hlua_sethlua(lua); |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1215 | lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1216 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1217 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1218 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1219 | return 1; |
| 1220 | } |
| 1221 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1222 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1223 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 1224 | * is not freed. |
| 1225 | */ |
| 1226 | void hlua_ctx_destroy(struct hlua *lua) |
| 1227 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1228 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1229 | return; |
| 1230 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1231 | if (!lua->T) |
| 1232 | goto end; |
| 1233 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1234 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1235 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1236 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1237 | if (!SET_SAFE_LJMP(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1238 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1239 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1240 | RESET_SAFE_LJMP(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1241 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1242 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1243 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1244 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1245 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1246 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1247 | * without error, we run the GC on the thread pointer. Its freed all |
| 1248 | * the unused memory. |
| 1249 | * If the thread is finnish with an error or is currently yielded, |
| 1250 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1251 | * so e run the GC on the main thread. |
| 1252 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1253 | * the garbage collection. |
| 1254 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1255 | if (lua->gc_count) { |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1256 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1257 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1258 | lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1259 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1260 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1261 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1262 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1263 | |
| 1264 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1265 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | /* This function is used to restore the Lua context when a coroutine |
| 1269 | * fails. This function copy the common memory between old coroutine |
| 1270 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1271 | * replaced by the new coroutine. |
| 1272 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1273 | * as string error message and it is copied in the new stack. |
| 1274 | */ |
| 1275 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1276 | { |
| 1277 | lua_State *T; |
| 1278 | int new_ref; |
| 1279 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1280 | /* New Lua coroutine. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1281 | T = lua_newthread(hlua_states[lua->state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1282 | if (!T) |
| 1283 | return 0; |
| 1284 | |
| 1285 | /* Copy last error message. */ |
| 1286 | if (keep_msg) |
| 1287 | lua_xmove(lua->T, T, 1); |
| 1288 | |
| 1289 | /* Copy data between the coroutines. */ |
| 1290 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1291 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1292 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1293 | |
| 1294 | /* Destroy old data. */ |
| 1295 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1296 | |
| 1297 | /* The thread is garbage collected by Lua. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1298 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1299 | |
| 1300 | /* Fill the struct with the new coroutine values. */ |
| 1301 | lua->Mref = new_ref; |
| 1302 | lua->T = T; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1303 | lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1304 | |
| 1305 | /* Set context. */ |
| 1306 | hlua_sethlua(lua); |
| 1307 | |
| 1308 | return 1; |
| 1309 | } |
| 1310 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1311 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1312 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1313 | struct hlua *hlua; |
| 1314 | |
| 1315 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1316 | hlua = hlua_gethlua(L); |
| 1317 | if (!hlua) |
| 1318 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1319 | |
| 1320 | /* Lua cannot yield when its returning from a function, |
| 1321 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1322 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1323 | */ |
| 1324 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1325 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1326 | return; |
| 1327 | } |
| 1328 | |
| 1329 | /* restore the interrupt condition. */ |
| 1330 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1331 | |
| 1332 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1333 | * If the state is not yieldable, trying yield causes an error. |
| 1334 | */ |
| 1335 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1336 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1337 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1338 | /* If we cannot yield, update the clock and check the timeout. */ |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 1339 | clock_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1340 | hlua->run_time += now_ms - hlua->start_time; |
| 1341 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1342 | lua_pushfstring(L, "execution timeout"); |
| 1343 | WILL_LJMP(lua_error(L)); |
| 1344 | } |
| 1345 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1346 | /* Update the start time. */ |
| 1347 | hlua->start_time = now_ms; |
| 1348 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1349 | /* Try to interrupt the process at the end of the current |
| 1350 | * unyieldable function. |
| 1351 | */ |
| 1352 | 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] | 1353 | } |
| 1354 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1355 | /* This function start or resumes the Lua stack execution. If the flag |
| 1356 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1357 | * The function return an error. |
| 1358 | * |
| 1359 | * The function can returns 4 values: |
| 1360 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1361 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1362 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1363 | * - 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] | 1364 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1365 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1366 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1367 | * 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] | 1368 | * LUA code. |
| 1369 | */ |
| 1370 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1371 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1372 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1373 | int nres; |
| 1374 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1375 | int ret; |
| 1376 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1377 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1378 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1379 | /* Initialise run time counter. */ |
| 1380 | if (!HLUA_IS_RUNNING(lua)) |
| 1381 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1382 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1383 | /* Lock the whole Lua execution. This lock must be before the |
| 1384 | * label "resume_execution". |
| 1385 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1386 | if (lua->state_id == 0) |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 1387 | lua_take_global_lock(); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1388 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1389 | resume_execution: |
| 1390 | |
| 1391 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1392 | * instructions. it is used for preventing infinite loops. |
| 1393 | */ |
| 1394 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1395 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1396 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1397 | HLUA_SET_RUN(lua); |
| 1398 | HLUA_CLR_CTRLYIELD(lua); |
| 1399 | HLUA_CLR_WAKERESWR(lua); |
| 1400 | HLUA_CLR_WAKEREQWR(lua); |
Christopher Faulet | 1f43a34 | 2021-08-04 17:58:21 +0200 | [diff] [blame] | 1401 | HLUA_CLR_NOYIELD(lua); |
| 1402 | if (!yield_allowed) |
| 1403 | HLUA_SET_NOYIELD(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1404 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1405 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1406 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1407 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1408 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1409 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1410 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1411 | 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] | 1412 | #else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1413 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1414 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1415 | switch (ret) { |
| 1416 | |
| 1417 | case LUA_OK: |
| 1418 | ret = HLUA_E_OK; |
| 1419 | break; |
| 1420 | |
| 1421 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1422 | /* Check if the execution timeout is expired. It it is the case, we |
| 1423 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1424 | */ |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 1425 | clock_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1426 | lua->run_time += now_ms - lua->start_time; |
| 1427 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1428 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1429 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1430 | break; |
| 1431 | } |
| 1432 | /* Process the forced yield. if the general yield is not allowed or |
| 1433 | * if no task were associated this the current Lua execution |
| 1434 | * coroutine, we resume the execution. Else we want to return in the |
| 1435 | * scheduler and we want to be waked up again, to continue the |
| 1436 | * current Lua execution. So we schedule our own task. |
| 1437 | */ |
| 1438 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1439 | if (!yield_allowed || !lua->task) |
| 1440 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1441 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1442 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1443 | if (!yield_allowed) { |
| 1444 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1445 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1446 | break; |
| 1447 | } |
| 1448 | ret = HLUA_E_AGAIN; |
| 1449 | break; |
| 1450 | |
| 1451 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1452 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1453 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1454 | * because the errors ares the only one mean to return immediately |
| 1455 | * from and lua execution. |
| 1456 | */ |
| 1457 | if (lua->flags & HLUA_EXIT) { |
| 1458 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1459 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1460 | break; |
| 1461 | } |
| 1462 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1463 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1464 | if (!lua_checkstack(lua->T, 1)) { |
| 1465 | ret = HLUA_E_ERR; |
| 1466 | break; |
| 1467 | } |
| 1468 | msg = lua_tostring(lua->T, -1); |
| 1469 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1470 | lua_pop(lua->T, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 1471 | trace = hlua_traceback(lua->T, ", "); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1472 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1473 | 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] | 1474 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1475 | 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] | 1476 | ret = HLUA_E_ERRMSG; |
| 1477 | break; |
| 1478 | |
| 1479 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1480 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1481 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1482 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1483 | break; |
| 1484 | |
| 1485 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1486 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1487 | if (!lua_checkstack(lua->T, 1)) { |
| 1488 | ret = HLUA_E_ERR; |
| 1489 | break; |
| 1490 | } |
| 1491 | msg = lua_tostring(lua->T, -1); |
| 1492 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1493 | lua_pop(lua->T, 1); |
| 1494 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1495 | 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] | 1496 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1497 | 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] | 1498 | ret = HLUA_E_ERRMSG; |
| 1499 | break; |
| 1500 | |
| 1501 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1502 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1503 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1504 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1505 | break; |
| 1506 | } |
| 1507 | |
| 1508 | switch (ret) { |
| 1509 | case HLUA_E_AGAIN: |
| 1510 | break; |
| 1511 | |
| 1512 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1513 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1514 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1515 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1516 | break; |
| 1517 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1518 | case HLUA_E_ETMOUT: |
| 1519 | case HLUA_E_NOMEM: |
| 1520 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1521 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1522 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1523 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1524 | hlua_ctx_renew(lua, 0); |
| 1525 | break; |
| 1526 | |
| 1527 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1528 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1529 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1530 | break; |
| 1531 | } |
| 1532 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1533 | /* This is the main exit point, remove the Lua lock. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1534 | if (lua->state_id == 0) |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 1535 | lua_drop_global_lock(); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1536 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1537 | return ret; |
| 1538 | } |
| 1539 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1540 | /* This function exit the current code. */ |
| 1541 | __LJMP static int hlua_done(lua_State *L) |
| 1542 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1543 | struct hlua *hlua; |
| 1544 | |
| 1545 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1546 | hlua = hlua_gethlua(L); |
| 1547 | if (!hlua) |
| 1548 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1549 | |
| 1550 | hlua->flags |= HLUA_EXIT; |
| 1551 | WILL_LJMP(lua_error(L)); |
| 1552 | |
| 1553 | return 0; |
| 1554 | } |
| 1555 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1556 | /* This function is an LUA binding. It provides a function |
| 1557 | * for deleting ACL from a referenced ACL file. |
| 1558 | */ |
| 1559 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1560 | { |
| 1561 | const char *name; |
| 1562 | const char *key; |
| 1563 | struct pat_ref *ref; |
| 1564 | |
| 1565 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1566 | |
| 1567 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1568 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1569 | |
| 1570 | ref = pat_ref_lookup(name); |
| 1571 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1572 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1573 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1574 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1575 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1576 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1577 | return 0; |
| 1578 | } |
| 1579 | |
| 1580 | /* This function is an LUA binding. It provides a function |
| 1581 | * for deleting map entry from a referenced map file. |
| 1582 | */ |
| 1583 | static int hlua_del_map(lua_State *L) |
| 1584 | { |
| 1585 | const char *name; |
| 1586 | const char *key; |
| 1587 | struct pat_ref *ref; |
| 1588 | |
| 1589 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1590 | |
| 1591 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1592 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1593 | |
| 1594 | ref = pat_ref_lookup(name); |
| 1595 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1596 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1597 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1598 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1599 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1600 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1601 | return 0; |
| 1602 | } |
| 1603 | |
| 1604 | /* This function is an LUA binding. It provides a function |
| 1605 | * for adding ACL pattern from a referenced ACL file. |
| 1606 | */ |
| 1607 | static int hlua_add_acl(lua_State *L) |
| 1608 | { |
| 1609 | const char *name; |
| 1610 | const char *key; |
| 1611 | struct pat_ref *ref; |
| 1612 | |
| 1613 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1614 | |
| 1615 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1616 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1617 | |
| 1618 | ref = pat_ref_lookup(name); |
| 1619 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1620 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1621 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1622 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1623 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1624 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1625 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1626 | return 0; |
| 1627 | } |
| 1628 | |
| 1629 | /* This function is an LUA binding. It provides a function |
| 1630 | * for setting map pattern and sample from a referenced map |
| 1631 | * file. |
| 1632 | */ |
| 1633 | static int hlua_set_map(lua_State *L) |
| 1634 | { |
| 1635 | const char *name; |
| 1636 | const char *key; |
| 1637 | const char *value; |
| 1638 | struct pat_ref *ref; |
| 1639 | |
| 1640 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1641 | |
| 1642 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1643 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1644 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1645 | |
| 1646 | ref = pat_ref_lookup(name); |
| 1647 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1648 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1649 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1650 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1651 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1652 | pat_ref_set(ref, key, value, NULL); |
| 1653 | else |
| 1654 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1655 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1656 | return 0; |
| 1657 | } |
| 1658 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1659 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1660 | * 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] | 1661 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1662 | * the name of the object (_G[<name>] = <metable> ). |
| 1663 | * |
| 1664 | * A metable is a table that modify the standard behavior of a standard |
| 1665 | * access to the associated data. The entries of this new metatable are |
| 1666 | * defined as is: |
| 1667 | * |
| 1668 | * http://lua-users.org/wiki/MetatableEvents |
| 1669 | * |
| 1670 | * __index |
| 1671 | * |
| 1672 | * we access an absent field in a table, the result is nil. This is |
| 1673 | * true, but it is not the whole truth. Actually, such access triggers |
| 1674 | * the interpreter to look for an __index metamethod: If there is no |
| 1675 | * such method, as usually happens, then the access results in nil; |
| 1676 | * otherwise, the metamethod will provide the result. |
| 1677 | * |
| 1678 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1679 | * the key does not appear in the table, but the metatable has an __index |
| 1680 | * property: |
| 1681 | * |
| 1682 | * - if the value is a function, the function is called, passing in the |
| 1683 | * table and the key; the return value of that function is returned as |
| 1684 | * the result. |
| 1685 | * |
| 1686 | * - if the value is another table, the value of the key in that table is |
| 1687 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1688 | * table's metatable has an __index property, then it continues on up) |
| 1689 | * |
| 1690 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1691 | * |
| 1692 | * http://www.lua.org/pil/13.4.1.html |
| 1693 | * |
| 1694 | * __newindex |
| 1695 | * |
| 1696 | * Like __index, but control property assignment. |
| 1697 | * |
| 1698 | * __mode - Control weak references. A string value with one or both |
| 1699 | * of the characters 'k' and 'v' which specifies that the the |
| 1700 | * keys and/or values in the table are weak references. |
| 1701 | * |
| 1702 | * __call - Treat a table like a function. When a table is followed by |
| 1703 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1704 | * a __call key pointing to a function, that function is invoked |
| 1705 | * (passing any specified arguments) and the return value is |
| 1706 | * returned. |
| 1707 | * |
| 1708 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1709 | * called, if the metatable for myTable has a __metatable |
| 1710 | * key, the value of that key is returned instead of the |
| 1711 | * actual metatable. |
| 1712 | * |
| 1713 | * __tostring - Control string representation. When the builtin |
| 1714 | * "tostring( myTable )" function is called, if the metatable |
| 1715 | * for myTable has a __tostring property set to a function, |
| 1716 | * that function is invoked (passing myTable to it) and the |
| 1717 | * return value is used as the string representation. |
| 1718 | * |
| 1719 | * __len - Control table length. When the table length is requested using |
| 1720 | * the length operator ( '#' ), if the metatable for myTable has |
| 1721 | * a __len key pointing to a function, that function is invoked |
| 1722 | * (passing myTable to it) and the return value used as the value |
| 1723 | * of "#myTable". |
| 1724 | * |
| 1725 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1726 | * collected, if the metatable has a __gc field pointing to a |
| 1727 | * function, that function is first invoked, passing the userdata |
| 1728 | * to it. The __gc metamethod is not called for tables. |
| 1729 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1730 | * |
| 1731 | * Special metamethods for redefining standard operators: |
| 1732 | * http://www.lua.org/pil/13.1.html |
| 1733 | * |
| 1734 | * __add "+" |
| 1735 | * __sub "-" |
| 1736 | * __mul "*" |
| 1737 | * __div "/" |
| 1738 | * __unm "!" |
| 1739 | * __pow "^" |
| 1740 | * __concat ".." |
| 1741 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1742 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1743 | * http://www.lua.org/pil/13.2.html |
| 1744 | * |
| 1745 | * __eq "==" |
| 1746 | * __lt "<" |
| 1747 | * __le "<=" |
| 1748 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1749 | |
| 1750 | /* |
| 1751 | * |
| 1752 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1753 | * Class Map |
| 1754 | * |
| 1755 | * |
| 1756 | */ |
| 1757 | |
| 1758 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1759 | * a class session, otherwise it throws an error. |
| 1760 | */ |
| 1761 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1762 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1763 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1764 | } |
| 1765 | |
| 1766 | /* This function is the map constructor. It don't need |
| 1767 | * the class Map object. It creates and return a new Map |
| 1768 | * object. It must be called only during "body" or "init" |
| 1769 | * context because it process some filesystem accesses. |
| 1770 | */ |
| 1771 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1772 | { |
| 1773 | const char *fn; |
| 1774 | int match = PAT_MATCH_STR; |
| 1775 | struct sample_conv conv; |
| 1776 | const char *file = ""; |
| 1777 | int line = 0; |
| 1778 | lua_Debug ar; |
| 1779 | char *err = NULL; |
| 1780 | struct arg args[2]; |
| 1781 | |
| 1782 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1783 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1784 | |
| 1785 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1786 | |
| 1787 | if (lua_gettop(L) >= 2) { |
| 1788 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1789 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1790 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1791 | } |
| 1792 | |
| 1793 | /* Get Lua filename and line number. */ |
| 1794 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1795 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1796 | if (ar.currentline > 0) { /* is there info? */ |
| 1797 | file = ar.short_src; |
| 1798 | line = ar.currentline; |
| 1799 | } |
| 1800 | } |
| 1801 | |
| 1802 | /* fill fake sample_conv struct. */ |
| 1803 | conv.kw = ""; /* unused. */ |
| 1804 | conv.process = NULL; /* unused. */ |
| 1805 | conv.arg_mask = 0; /* unused. */ |
| 1806 | conv.val_args = NULL; /* unused. */ |
| 1807 | conv.out_type = SMP_T_STR; |
| 1808 | conv.private = (void *)(long)match; |
| 1809 | switch (match) { |
| 1810 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1811 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1812 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1813 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1814 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1815 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1816 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1817 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1818 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1819 | default: |
| 1820 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1821 | } |
| 1822 | |
| 1823 | /* fill fake args. */ |
| 1824 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1825 | args[0].data.str.area = strdup(fn); |
| 1826 | args[0].data.str.data = strlen(fn); |
| 1827 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1828 | args[1].type = ARGT_STOP; |
| 1829 | |
| 1830 | /* load the map. */ |
| 1831 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1832 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1833 | * free the err variable. |
| 1834 | */ |
| 1835 | luaL_where(L, 1); |
| 1836 | lua_pushfstring(L, "'new': %s.", err); |
| 1837 | lua_concat(L, 2); |
| 1838 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1839 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1840 | WILL_LJMP(lua_error(L)); |
| 1841 | } |
| 1842 | |
| 1843 | /* create the lua object. */ |
| 1844 | lua_newtable(L); |
| 1845 | lua_pushlightuserdata(L, args[0].data.map); |
| 1846 | lua_rawseti(L, -2, 0); |
| 1847 | |
| 1848 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1849 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1850 | lua_setmetatable(L, -2); |
| 1851 | |
| 1852 | |
| 1853 | return 1; |
| 1854 | } |
| 1855 | |
| 1856 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1857 | { |
| 1858 | struct map_descriptor *desc; |
| 1859 | struct pattern *pat; |
| 1860 | struct sample smp; |
| 1861 | |
| 1862 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1863 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1864 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1865 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1866 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1867 | } |
| 1868 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1869 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1870 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1871 | 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] | 1872 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1873 | } |
| 1874 | |
| 1875 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1876 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1877 | if (str) |
| 1878 | lua_pushstring(L, ""); |
| 1879 | else |
| 1880 | lua_pushnil(L); |
| 1881 | return 1; |
| 1882 | } |
| 1883 | |
| 1884 | /* 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] | 1885 | 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] | 1886 | return 1; |
| 1887 | } |
| 1888 | |
| 1889 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1890 | { |
| 1891 | return _hlua_map_lookup(L, 0); |
| 1892 | } |
| 1893 | |
| 1894 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1895 | { |
| 1896 | return _hlua_map_lookup(L, 1); |
| 1897 | } |
| 1898 | |
| 1899 | /* |
| 1900 | * |
| 1901 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1902 | * Class Socket |
| 1903 | * |
| 1904 | * |
| 1905 | */ |
| 1906 | |
| 1907 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1908 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1909 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1910 | } |
| 1911 | |
| 1912 | /* 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] | 1913 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1914 | * received. |
| 1915 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1916 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1917 | { |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1918 | struct conn_stream *cs = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1919 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1920 | if (appctx->ctx.hlua_cosocket.die) { |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame] | 1921 | cs_shutw(cs); |
| 1922 | cs_shutr(cs); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1923 | cs_ic(cs)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1924 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1925 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1926 | stream_shutdown(__cs_strm(cs), SF_ERR_KILLED); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1927 | } |
| 1928 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1929 | /* If we can't write, wakeup the pending write signals. */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1930 | if (channel_output_closed(cs_ic(cs))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1931 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1932 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1933 | /* If we can't read, wakeup the pending read signals. */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1934 | if (channel_input_closed(cs_oc(cs))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1935 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1936 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1937 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1938 | * to be notified whenever the connection completes. |
| 1939 | */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1940 | if (cs_opposite(cs)->state < CS_ST_EST) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 1941 | cs_cant_get(cs); |
| 1942 | cs_rx_conn_blk(cs); |
| 1943 | cs_rx_endp_more(cs); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1944 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1945 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1946 | |
| 1947 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1948 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1949 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1950 | /* 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] | 1951 | if (channel_may_recv(cs_ic(cs))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1952 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1953 | |
| 1954 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1955 | if (!channel_is_empty(cs_oc(cs))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1956 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1957 | |
| 1958 | /* Some data were injected in the buffer, notify the stream |
| 1959 | * interface. |
| 1960 | */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1961 | if (!channel_is_empty(cs_ic(cs))) |
Christopher Faulet | 13045f0 | 2022-04-01 14:23:38 +0200 | [diff] [blame] | 1962 | cs_update(cs); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1963 | |
| 1964 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1965 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1966 | */ |
| 1967 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 1968 | cs_rx_endp_more(cs); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1969 | } |
| 1970 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1971 | /* This function is called when the "struct stream" is destroyed. |
| 1972 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1973 | * Wake all the pending signals. |
| 1974 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1975 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1976 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1977 | struct xref *peer; |
| 1978 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1979 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1980 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1981 | if (peer) |
| 1982 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1983 | |
| 1984 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1985 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1986 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1987 | } |
| 1988 | |
| 1989 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1990 | * uses this object. If the stream does not exists, just quit. |
| 1991 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1992 | * pending signal can rest in the read and write lists. destroy |
| 1993 | * it. |
| 1994 | */ |
| 1995 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1996 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1997 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1998 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1999 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2000 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2001 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 2002 | |
| 2003 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2004 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2005 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2006 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2007 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2008 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2009 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2010 | appctx->ctx.hlua_cosocket.die = 1; |
| 2011 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2012 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2013 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2014 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2015 | return 0; |
| 2016 | } |
| 2017 | |
| 2018 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2019 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2020 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2021 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2022 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2023 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2024 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2025 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2026 | struct hlua *hlua; |
| 2027 | |
| 2028 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2029 | hlua = hlua_gethlua(L); |
| 2030 | if (!hlua) |
| 2031 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2032 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2033 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2034 | |
| 2035 | /* Check if we run on the same thread than the xreator thread. |
| 2036 | * We cannot access to the socket if the thread is different. |
| 2037 | */ |
| 2038 | if (socket->tid != tid) |
| 2039 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2040 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2041 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2042 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2043 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2044 | |
| 2045 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2046 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2047 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2048 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2049 | appctx->ctx.hlua_cosocket.die = 1; |
| 2050 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2051 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2052 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2053 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2054 | return 0; |
| 2055 | } |
| 2056 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2057 | /* The close function calls close_helper. |
| 2058 | */ |
| 2059 | __LJMP static int hlua_socket_close(lua_State *L) |
| 2060 | { |
| 2061 | MAY_LJMP(check_args(L, 1, "close")); |
| 2062 | return hlua_socket_close_helper(L); |
| 2063 | } |
| 2064 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2065 | /* This Lua function assumes that the stack contain three parameters. |
| 2066 | * 1 - USERDATA containing a struct socket |
| 2067 | * 2 - INTEGER with values of the macro defined below |
| 2068 | * If the integer is -1, we must read at most one line. |
| 2069 | * If the integer is -2, we ust read all the data until the |
| 2070 | * end of the stream. |
| 2071 | * If the integer is positive value, we must read a number of |
| 2072 | * bytes corresponding to this value. |
| 2073 | */ |
| 2074 | #define HLSR_READ_LINE (-1) |
| 2075 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2076 | __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] | 2077 | { |
| 2078 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2079 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2080 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2081 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2082 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2083 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2084 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2085 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2086 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2087 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2088 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 2089 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2090 | struct stream *s; |
| 2091 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2092 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2093 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2094 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2095 | hlua = hlua_gethlua(L); |
| 2096 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2097 | /* Check if this lua stack is schedulable. */ |
| 2098 | if (!hlua || !hlua->task) |
| 2099 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 2100 | "'frontend', 'backend' or 'task'")); |
| 2101 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2102 | /* Check if we run on the same thread than the xreator thread. |
| 2103 | * We cannot access to the socket if the thread is different. |
| 2104 | */ |
| 2105 | if (socket->tid != tid) |
| 2106 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2107 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2108 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2109 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2110 | if (!peer) |
| 2111 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2112 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2113 | s = __cs_strm(appctx->owner); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2114 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2115 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2116 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2117 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2118 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2119 | if (nblk < 0) /* Connection close. */ |
| 2120 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2121 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2122 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2123 | |
| 2124 | /* remove final \r\n. */ |
| 2125 | if (nblk == 1) { |
| 2126 | if (blk1[len1-1] == '\n') { |
| 2127 | len1--; |
| 2128 | skip_at_end++; |
| 2129 | if (blk1[len1-1] == '\r') { |
| 2130 | len1--; |
| 2131 | skip_at_end++; |
| 2132 | } |
| 2133 | } |
| 2134 | } |
| 2135 | else { |
| 2136 | if (blk2[len2-1] == '\n') { |
| 2137 | len2--; |
| 2138 | skip_at_end++; |
| 2139 | if (blk2[len2-1] == '\r') { |
| 2140 | len2--; |
| 2141 | skip_at_end++; |
| 2142 | } |
| 2143 | } |
| 2144 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2145 | } |
| 2146 | |
| 2147 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2148 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2149 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2150 | if (nblk < 0) /* Connection close. */ |
| 2151 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2152 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2153 | goto connection_empty; |
| 2154 | } |
| 2155 | |
| 2156 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2157 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2158 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2159 | if (nblk < 0) /* Connection close. */ |
| 2160 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2161 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2162 | goto connection_empty; |
| 2163 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2164 | missing_bytes = wanted - socket->b.n; |
| 2165 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2166 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2167 | len1 = missing_bytes; |
| 2168 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2169 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2170 | } |
| 2171 | |
| 2172 | len = len1; |
| 2173 | |
| 2174 | luaL_addlstring(&socket->b, blk1, len1); |
| 2175 | if (nblk == 2) { |
| 2176 | len += len2; |
| 2177 | luaL_addlstring(&socket->b, blk2, len2); |
| 2178 | } |
| 2179 | |
| 2180 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2181 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2182 | |
| 2183 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2184 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2185 | |
| 2186 | /* If the pattern reclaim to read all the data |
| 2187 | * in the connection, got out. |
| 2188 | */ |
| 2189 | if (wanted == HLSR_READ_ALL) |
| 2190 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2191 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2192 | goto connection_empty; |
| 2193 | |
| 2194 | /* Return result. */ |
| 2195 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2196 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2197 | return 1; |
| 2198 | |
| 2199 | connection_closed: |
| 2200 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2201 | xref_unlock(&socket->xref, peer); |
| 2202 | |
| 2203 | no_peer: |
| 2204 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2205 | /* If the buffer containds data. */ |
| 2206 | if (socket->b.n > 0) { |
| 2207 | luaL_pushresult(&socket->b); |
| 2208 | return 1; |
| 2209 | } |
| 2210 | lua_pushnil(L); |
| 2211 | lua_pushstring(L, "connection closed."); |
| 2212 | return 2; |
| 2213 | |
| 2214 | connection_empty: |
| 2215 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2216 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 2217 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2218 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2219 | } |
| 2220 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2221 | 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] | 2222 | return 0; |
| 2223 | } |
| 2224 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2225 | /* This Lua function gets two parameters. The first one can be string |
| 2226 | * or a number. If the string is "*l", the user requires one line. If |
| 2227 | * 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] | 2228 | * If the value is a number, the user require a number of bytes equal |
| 2229 | * to the value. The default value is "*l" (a line). |
| 2230 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2231 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2232 | * integer takes this values: |
| 2233 | * -1 : read a line |
| 2234 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2235 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2236 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2237 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2238 | * concatenated with the read data. |
| 2239 | */ |
| 2240 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2241 | { |
| 2242 | int wanted = HLSR_READ_LINE; |
| 2243 | const char *pattern; |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2244 | int lastarg, type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2245 | char *error; |
| 2246 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2247 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2248 | |
| 2249 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2250 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2251 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2252 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2253 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2254 | /* Check if we run on the same thread than the xreator thread. |
| 2255 | * We cannot access to the socket if the thread is different. |
| 2256 | */ |
| 2257 | if (socket->tid != tid) |
| 2258 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2259 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2260 | /* check for pattern. */ |
| 2261 | if (lua_gettop(L) >= 2) { |
| 2262 | type = lua_type(L, 2); |
| 2263 | if (type == LUA_TSTRING) { |
| 2264 | pattern = lua_tostring(L, 2); |
| 2265 | if (strcmp(pattern, "*a") == 0) |
| 2266 | wanted = HLSR_READ_ALL; |
| 2267 | else if (strcmp(pattern, "*l") == 0) |
| 2268 | wanted = HLSR_READ_LINE; |
| 2269 | else { |
| 2270 | wanted = strtoll(pattern, &error, 10); |
| 2271 | if (*error != '\0') |
| 2272 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2273 | } |
| 2274 | } |
| 2275 | else if (type == LUA_TNUMBER) { |
| 2276 | wanted = lua_tointeger(L, 2); |
| 2277 | if (wanted < 0) |
| 2278 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2279 | } |
| 2280 | } |
| 2281 | |
| 2282 | /* Set pattern. */ |
| 2283 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2284 | |
| 2285 | /* Check if we would replace the top by itself. */ |
| 2286 | if (lua_gettop(L) != 2) |
| 2287 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2288 | |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2289 | /* Save index of the top of the stack because since buffers are used, it |
| 2290 | * may change |
| 2291 | */ |
| 2292 | lastarg = lua_gettop(L); |
| 2293 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2294 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2295 | luaL_buffinit(L, &socket->b); |
| 2296 | |
| 2297 | /* Check prefix. */ |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2298 | if (lastarg >= 3) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2299 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2300 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2301 | pattern = lua_tolstring(L, 3, &len); |
| 2302 | luaL_addlstring(&socket->b, pattern, len); |
| 2303 | } |
| 2304 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2305 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2306 | } |
| 2307 | |
| 2308 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2309 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2310 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2311 | 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] | 2312 | { |
| 2313 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2314 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2315 | struct appctx *appctx; |
| 2316 | size_t buf_len; |
| 2317 | const char *buf; |
| 2318 | int len; |
| 2319 | int send_len; |
| 2320 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2321 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2322 | struct stream *s; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2323 | struct conn_stream *cs; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2324 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2325 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2326 | hlua = hlua_gethlua(L); |
| 2327 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2328 | /* Check if this lua stack is schedulable. */ |
| 2329 | if (!hlua || !hlua->task) |
| 2330 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2331 | "'frontend', 'backend' or 'task'")); |
| 2332 | |
| 2333 | /* Get object */ |
| 2334 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2335 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2336 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2337 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2338 | /* Check if we run on the same thread than the xreator thread. |
| 2339 | * We cannot access to the socket if the thread is different. |
| 2340 | */ |
| 2341 | if (socket->tid != tid) |
| 2342 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2343 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2344 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2345 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2346 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2347 | lua_pushinteger(L, -1); |
| 2348 | return 1; |
| 2349 | } |
| 2350 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2351 | cs = appctx->owner; |
| 2352 | s = __cs_strm(cs); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2353 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2354 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2355 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2356 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2357 | lua_pushinteger(L, -1); |
| 2358 | return 1; |
| 2359 | } |
| 2360 | |
| 2361 | /* Update the input buffer data. */ |
| 2362 | buf += sent; |
| 2363 | send_len = buf_len - sent; |
| 2364 | |
| 2365 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2366 | if (sent >= buf_len) { |
| 2367 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2368 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2369 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2370 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2371 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2372 | * the request buffer if its not required. |
| 2373 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2374 | if (s->req.buf.size == 0) { |
Christopher Faulet | 8f45eec | 2022-04-01 17:19:36 +0200 | [diff] [blame] | 2375 | if (!cs_alloc_ibuf(cs, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2376 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2377 | } |
| 2378 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2379 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2380 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2381 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2382 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2383 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2384 | |
| 2385 | /* send data */ |
| 2386 | if (len < send_len) |
| 2387 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2388 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2389 | |
| 2390 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2391 | * the data" (-2) are not expected because the available length |
| 2392 | * is tested. |
| 2393 | * Other unknown error are also not expected. |
| 2394 | */ |
| 2395 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2396 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2397 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2398 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2399 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2400 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2401 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2402 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2403 | return 1; |
| 2404 | } |
| 2405 | |
| 2406 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2407 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2408 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2409 | s->req.rex = TICK_ETERNITY; |
| 2410 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2411 | |
| 2412 | /* Update length sent. */ |
| 2413 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2414 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2415 | |
| 2416 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2417 | if (sent + len >= buf_len) { |
| 2418 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2419 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2420 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2421 | |
| 2422 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2423 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2424 | xref_unlock(&socket->xref, peer); |
| 2425 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2426 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2427 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2428 | 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] | 2429 | return 0; |
| 2430 | } |
| 2431 | |
| 2432 | /* This function initiate the send of data. It just check the input |
| 2433 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2434 | * 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] | 2435 | * "hlua_socket_write_yield" that can yield. |
| 2436 | * |
| 2437 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2438 | * the associated object. The second is a string buffer. The third is |
| 2439 | * a facultative integer that represents where is the buffer position |
| 2440 | * of the start of the data that can send. The first byte is the |
| 2441 | * position "1". The default value is "1". The fourth argument is a |
| 2442 | * facultative integer that represents where is the buffer position |
| 2443 | * of the end of the data that can send. The default is the last byte. |
| 2444 | */ |
| 2445 | static int hlua_socket_send(struct lua_State *L) |
| 2446 | { |
| 2447 | int i; |
| 2448 | int j; |
| 2449 | const char *buf; |
| 2450 | size_t buf_len; |
| 2451 | |
| 2452 | /* Check number of arguments. */ |
| 2453 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2454 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2455 | |
| 2456 | /* Get the string. */ |
| 2457 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2458 | |
| 2459 | /* Get and check j. */ |
| 2460 | if (lua_gettop(L) == 4) { |
| 2461 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2462 | if (j < 0) |
| 2463 | j = buf_len + j + 1; |
| 2464 | if (j > buf_len) |
| 2465 | j = buf_len + 1; |
| 2466 | lua_pop(L, 1); |
| 2467 | } |
| 2468 | else |
| 2469 | j = buf_len; |
| 2470 | |
| 2471 | /* Get and check i. */ |
| 2472 | if (lua_gettop(L) == 3) { |
| 2473 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2474 | if (i < 0) |
| 2475 | i = buf_len + i + 1; |
| 2476 | if (i > buf_len) |
| 2477 | i = buf_len + 1; |
| 2478 | lua_pop(L, 1); |
| 2479 | } else |
| 2480 | i = 1; |
| 2481 | |
| 2482 | /* Check bth i and j. */ |
| 2483 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2484 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2485 | return 1; |
| 2486 | } |
| 2487 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2488 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2489 | return 1; |
| 2490 | } |
| 2491 | if (i == 0) |
| 2492 | i = 1; |
| 2493 | if (j == 0) |
| 2494 | j = 1; |
| 2495 | |
| 2496 | /* Pop the string. */ |
| 2497 | lua_pop(L, 1); |
| 2498 | |
| 2499 | /* Update the buffer length. */ |
| 2500 | buf += i - 1; |
| 2501 | buf_len = j - i + 1; |
| 2502 | lua_pushlstring(L, buf, buf_len); |
| 2503 | |
| 2504 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2505 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2506 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2507 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2508 | } |
| 2509 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2510 | #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] | 2511 | __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] | 2512 | { |
| 2513 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2514 | int ret; |
| 2515 | int len; |
| 2516 | char *p; |
| 2517 | |
| 2518 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2519 | if (ret <= 0) { |
| 2520 | lua_pushnil(L); |
| 2521 | return 1; |
| 2522 | } |
| 2523 | |
| 2524 | if (ret == AF_UNIX) { |
| 2525 | lua_pushstring(L, buffer+1); |
| 2526 | return 1; |
| 2527 | } |
| 2528 | else if (ret == AF_INET6) { |
| 2529 | buffer[0] = '['; |
| 2530 | len = strlen(buffer); |
| 2531 | buffer[len] = ']'; |
| 2532 | len++; |
| 2533 | buffer[len] = ':'; |
| 2534 | len++; |
| 2535 | p = buffer; |
| 2536 | } |
| 2537 | else if (ret == AF_INET) { |
| 2538 | p = buffer + 1; |
| 2539 | len = strlen(p); |
| 2540 | p[len] = ':'; |
| 2541 | len++; |
| 2542 | } |
| 2543 | else { |
| 2544 | lua_pushnil(L); |
| 2545 | return 1; |
| 2546 | } |
| 2547 | |
| 2548 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2549 | lua_pushnil(L); |
| 2550 | return 1; |
| 2551 | } |
| 2552 | |
| 2553 | lua_pushstring(L, p); |
| 2554 | return 1; |
| 2555 | } |
| 2556 | |
| 2557 | /* Returns information about the peer of the connection. */ |
| 2558 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2559 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2560 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2561 | struct xref *peer; |
| 2562 | struct appctx *appctx; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2563 | struct conn_stream *cs; |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2564 | const struct sockaddr_storage *dst; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2565 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2566 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2567 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2568 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2569 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2570 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2571 | /* Check if we run on the same thread than the xreator thread. |
| 2572 | * We cannot access to the socket if the thread is different. |
| 2573 | */ |
| 2574 | if (socket->tid != tid) |
| 2575 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2576 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2577 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2578 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2579 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2580 | lua_pushnil(L); |
| 2581 | return 1; |
| 2582 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2583 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2584 | cs = appctx->owner; |
Christopher Faulet | 8da67aa | 2022-03-29 17:53:09 +0200 | [diff] [blame] | 2585 | dst = cs_dst(cs_opposite(cs)); |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2586 | if (!dst) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2587 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2588 | lua_pushnil(L); |
| 2589 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2590 | } |
| 2591 | |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2592 | ret = MAY_LJMP(hlua_socket_info(L, dst)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2593 | xref_unlock(&socket->xref, peer); |
| 2594 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2595 | } |
| 2596 | |
| 2597 | /* Returns information about my connection side. */ |
| 2598 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2599 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2600 | struct hlua_socket *socket; |
| 2601 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2602 | struct appctx *appctx; |
| 2603 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2604 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2605 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2606 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2607 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2608 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2609 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2610 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2611 | /* Check if we run on the same thread than the xreator thread. |
| 2612 | * We cannot access to the socket if the thread is different. |
| 2613 | */ |
| 2614 | if (socket->tid != tid) |
| 2615 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2616 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2617 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2618 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2619 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2620 | lua_pushnil(L); |
| 2621 | return 1; |
| 2622 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2623 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2624 | s = __cs_strm(appctx->owner); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2625 | |
Christopher Faulet | 95a61e8 | 2021-12-22 14:22:03 +0100 | [diff] [blame] | 2626 | conn = cs_conn(s->csb); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2627 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2628 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2629 | lua_pushnil(L); |
| 2630 | return 1; |
| 2631 | } |
| 2632 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2633 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2634 | xref_unlock(&socket->xref, peer); |
| 2635 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2636 | } |
| 2637 | |
| 2638 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2639 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2640 | .obj_type = OBJ_TYPE_APPLET, |
| 2641 | .name = "<LUA_TCP>", |
| 2642 | .fct = hlua_socket_handler, |
| 2643 | .release = hlua_socket_release, |
| 2644 | }; |
| 2645 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2646 | __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] | 2647 | { |
| 2648 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2649 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2650 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2651 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2652 | struct stream *s; |
| 2653 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2654 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2655 | hlua = hlua_gethlua(L); |
| 2656 | if (!hlua) |
| 2657 | return 0; |
| 2658 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2659 | /* Check if we run on the same thread than the xreator thread. |
| 2660 | * We cannot access to the socket if the thread is different. |
| 2661 | */ |
| 2662 | if (socket->tid != tid) |
| 2663 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2664 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2665 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2666 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2667 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2668 | lua_pushnil(L); |
| 2669 | lua_pushstring(L, "Can't connect"); |
| 2670 | return 2; |
| 2671 | } |
| 2672 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2673 | s = __cs_strm(appctx->owner); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2674 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2675 | /* Check if we run on the same thread than the xreator thread. |
| 2676 | * We cannot access to the socket if the thread is different. |
| 2677 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2678 | if (socket->tid != tid) { |
| 2679 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2680 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2681 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2682 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2683 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2684 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2685 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2686 | lua_pushnil(L); |
| 2687 | lua_pushstring(L, "Can't connect"); |
| 2688 | return 2; |
| 2689 | } |
| 2690 | |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 2691 | appctx = __cs_appctx(s->csf); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2692 | |
| 2693 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2694 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2695 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2696 | lua_pushinteger(L, 1); |
| 2697 | return 1; |
| 2698 | } |
| 2699 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2700 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2701 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2702 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2703 | } |
| 2704 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2705 | 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] | 2706 | return 0; |
| 2707 | } |
| 2708 | |
| 2709 | /* This function fail or initite the connection. */ |
| 2710 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2711 | { |
| 2712 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2713 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2714 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2715 | struct hlua *hlua; |
| 2716 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2717 | int low, high; |
| 2718 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2719 | struct xref *peer; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2720 | struct conn_stream *cs; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2721 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2722 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2723 | if (lua_gettop(L) < 2) |
| 2724 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2725 | |
| 2726 | /* Get args. */ |
| 2727 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2728 | |
| 2729 | /* Check if we run on the same thread than the xreator thread. |
| 2730 | * We cannot access to the socket if the thread is different. |
| 2731 | */ |
| 2732 | if (socket->tid != tid) |
| 2733 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2734 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2735 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2736 | if (lua_gettop(L) >= 3) { |
| 2737 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2738 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2739 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2740 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2741 | * that are not enclosed within square brackets. |
| 2742 | */ |
| 2743 | if (port > 0) { |
| 2744 | luaL_buffinit(L, &b); |
| 2745 | luaL_addstring(&b, ip); |
| 2746 | luaL_addchar(&b, ':'); |
| 2747 | luaL_pushresult(&b); |
| 2748 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2749 | } |
| 2750 | } |
| 2751 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2752 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2753 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2754 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2755 | lua_pushnil(L); |
| 2756 | return 1; |
| 2757 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2758 | |
| 2759 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2760 | 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] | 2761 | if (!addr) { |
| 2762 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2763 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2764 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2765 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2766 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2767 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2768 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2769 | if (port == -1) { |
| 2770 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2771 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2772 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2773 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2774 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2775 | if (port == -1) { |
| 2776 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2777 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2778 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2779 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2780 | } |
| 2781 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2782 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2783 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2784 | cs = appctx->owner; |
| 2785 | s = __cs_strm(cs); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2786 | |
Christopher Faulet | 8da67aa | 2022-03-29 17:53:09 +0200 | [diff] [blame] | 2787 | if (!sockaddr_alloc(&cs_opposite(cs)->dst, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2788 | xref_unlock(&socket->xref, peer); |
| 2789 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2790 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2791 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2792 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2793 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2794 | if (!hlua) |
| 2795 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2796 | |
| 2797 | /* inform the stream that we want to be notified whenever the |
| 2798 | * connection completes. |
| 2799 | */ |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 2800 | cs_cant_get(s->csf); |
| 2801 | cs_rx_endp_more(s->csf); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2802 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2803 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2804 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2805 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2806 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2807 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2808 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2809 | } |
| 2810 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2811 | |
| 2812 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2813 | /* Return yield waiting for connection. */ |
| 2814 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2815 | 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] | 2816 | |
| 2817 | return 0; |
| 2818 | } |
| 2819 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2820 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2821 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2822 | { |
| 2823 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2824 | struct xref *peer; |
| 2825 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2826 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2827 | |
| 2828 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2829 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2830 | |
| 2831 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2832 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2833 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2834 | lua_pushnil(L); |
| 2835 | return 1; |
| 2836 | } |
| 2837 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2838 | s = __cs_strm(appctx->owner); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2839 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2840 | s->target = &socket_ssl->obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2841 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2842 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2843 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2844 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2845 | |
| 2846 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2847 | { |
| 2848 | return 0; |
| 2849 | } |
| 2850 | |
| 2851 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2852 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2853 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2854 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2855 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2856 | struct xref *peer; |
| 2857 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2858 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2859 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2860 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2861 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2862 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2863 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2864 | /* convert the timeout to millis */ |
| 2865 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2866 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2867 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2868 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2869 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2870 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2871 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2872 | WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2873 | |
| 2874 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2875 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2876 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2877 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2878 | /* Check if we run on the same thread than the xreator thread. |
| 2879 | * We cannot access to the socket if the thread is different. |
| 2880 | */ |
| 2881 | if (socket->tid != tid) |
| 2882 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2883 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2884 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2885 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2886 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2887 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2888 | WILL_LJMP(lua_error(L)); |
| 2889 | return 0; |
| 2890 | } |
| 2891 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2892 | s = __cs_strm(appctx->owner); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2893 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2894 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2895 | s->req.rto = tmout; |
| 2896 | s->req.wto = tmout; |
| 2897 | s->res.rto = tmout; |
| 2898 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2899 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2900 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2901 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2902 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2903 | |
| 2904 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2905 | task_queue(s->task); |
| 2906 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2907 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2908 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2909 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2910 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2911 | } |
| 2912 | |
| 2913 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2914 | { |
| 2915 | struct hlua_socket *socket; |
| 2916 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2917 | struct session *sess; |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 2918 | struct conn_stream *cs; |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 2919 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2920 | |
| 2921 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2922 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2923 | hlua_pusherror(L, "socket: full stack"); |
| 2924 | goto out_fail_conf; |
| 2925 | } |
| 2926 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2927 | /* Create the object: obj[0] = userdata. */ |
| 2928 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2929 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2930 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2931 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2932 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2933 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2934 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2935 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2936 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2937 | goto out_fail_conf; |
| 2938 | } |
| 2939 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2940 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2941 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2942 | lua_setmetatable(L, -2); |
| 2943 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2944 | /* Create the applet context */ |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 2945 | appctx = appctx_new(&update_applet, NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2946 | if (!appctx) { |
| 2947 | hlua_pusherror(L, "socket: out of memory"); |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 2948 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2949 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2950 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2951 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2952 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2953 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2954 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2955 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2956 | /* Now create a session, task and stream for this applet */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 2957 | sess = session_new(socket_proxy, NULL, &appctx->obj_type); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2958 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2959 | hlua_pusherror(L, "socket: out of memory"); |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 2960 | goto out_fail_appctx; |
| 2961 | } |
| 2962 | |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 2963 | cs = cs_new_from_applet(appctx->endp, sess, &BUF_NULL); |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 2964 | if (!cs) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2965 | hlua_pusherror(L, "socket: out of memory"); |
Christopher Faulet | 2479e5f | 2022-01-19 14:50:11 +0100 | [diff] [blame] | 2966 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2967 | } |
| 2968 | |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 2969 | s = DISGUISE(cs_strm(cs)); |
Christopher Faulet | 2479e5f | 2022-01-19 14:50:11 +0100 | [diff] [blame] | 2970 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2971 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2972 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2973 | |
Christopher Faulet | 6b0a0fb | 2022-04-04 11:29:28 +0200 | [diff] [blame] | 2974 | /* Configure "right" conn-stream. this "si" is used to connect |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2975 | * and retrieve data from the server. The connection is initialized |
| 2976 | * with the "struct server". |
| 2977 | */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 2978 | cs_set_state(s->csb, CS_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2979 | |
| 2980 | /* Force destination server. */ |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 2981 | s->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
| 2982 | s->target = &socket_tcp->obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2983 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2984 | return 1; |
| 2985 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2986 | out_fail_sess: |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 2987 | session_free(sess); |
| 2988 | out_fail_appctx: |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2989 | appctx_free(appctx); |
| 2990 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2991 | WILL_LJMP(lua_error(L)); |
| 2992 | return 0; |
| 2993 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2994 | |
| 2995 | /* |
| 2996 | * |
| 2997 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2998 | * Class Channel |
| 2999 | * |
| 3000 | * |
| 3001 | */ |
| 3002 | |
| 3003 | /* Returns the struct hlua_channel join to the class channel in the |
| 3004 | * stack entry "ud" or throws an argument error. |
| 3005 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3006 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3007 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3008 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3009 | } |
| 3010 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3011 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 3012 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3013 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 3014 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3015 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3016 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3017 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3018 | return 0; |
| 3019 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3020 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3021 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3022 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3023 | |
| 3024 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3025 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 3026 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3027 | return 1; |
| 3028 | } |
| 3029 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3030 | /* Helper function returning a filter attached to a channel at the position <ud> |
| 3031 | * 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] | 3032 | * filter is attached, NULL is returned and <offset> and <len> are not |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3033 | * initialized. |
| 3034 | */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3035 | 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] | 3036 | { |
| 3037 | struct filter *filter = NULL; |
| 3038 | |
| 3039 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 3040 | struct hlua_flt_ctx *flt_ctx; |
| 3041 | |
| 3042 | filter = lua_touserdata (L, -1); |
| 3043 | flt_ctx = filter->ctx; |
| 3044 | if (hlua_filter_from_payload(filter)) { |
| 3045 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 3046 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 3047 | } |
| 3048 | } |
| 3049 | |
| 3050 | lua_pop(L, 1); |
| 3051 | return filter; |
| 3052 | } |
| 3053 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3054 | /* Copies <len> bytes of data present in the channel's buffer, starting at the |
| 3055 | * 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] | 3056 | * responsibility to ensure <len> and <offset> are valid. It always return the |
| 3057 | * length of the built string. <len> may be 0, in this case, an empty string is |
| 3058 | * created and 0 is returned. |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3059 | */ |
| 3060 | 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] | 3061 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3062 | size_t block1, block2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3063 | luaL_Buffer b; |
| 3064 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3065 | block1 = len; |
| 3066 | if (block1 > b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset))) |
| 3067 | block1 = b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset)); |
| 3068 | block2 = len - block1; |
| 3069 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3070 | luaL_buffinit(L, &b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3071 | luaL_addlstring(&b, b_peek(&chn->buf, offset), block1); |
| 3072 | if (block2) |
| 3073 | luaL_addlstring(&b, b_orig(&chn->buf), block2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3074 | luaL_pushresult(&b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3075 | return len; |
| 3076 | } |
| 3077 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3078 | /* Inserts the string <str> to the channel's buffer at the offset <offset>. This |
| 3079 | * function returns -1 if data cannot be copied. Otherwise, it returns the |
| 3080 | * number of bytes copied. |
| 3081 | */ |
| 3082 | static int _hlua_channel_insert(struct channel *chn, lua_State *L, struct ist str, size_t offset) |
| 3083 | { |
| 3084 | int ret = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3085 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3086 | /* Nothing to do, just return */ |
| 3087 | if (unlikely(istlen(str) == 0)) |
| 3088 | goto end; |
| 3089 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3090 | if (istlen(str) > c_room(chn)) { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3091 | ret = -1; |
| 3092 | goto end; |
| 3093 | } |
| 3094 | ret = b_insert_blk(&chn->buf, offset, istptr(str), istlen(str)); |
| 3095 | |
| 3096 | end: |
| 3097 | return ret; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3098 | } |
| 3099 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3100 | /* Removes <len> bytes of data at the absolute position <offset>. |
| 3101 | */ |
| 3102 | static void _hlua_channel_delete(struct channel *chn, size_t offset, size_t len) |
| 3103 | { |
| 3104 | size_t end = offset + len; |
| 3105 | |
| 3106 | if (b_peek(&chn->buf, end) != b_tail(&chn->buf)) |
| 3107 | b_move(&chn->buf, b_peek_ofs(&chn->buf, end), |
| 3108 | b_data(&chn->buf) - end, -len); |
| 3109 | b_sub(&chn->buf, len); |
| 3110 | } |
| 3111 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3112 | /* Copies input data in the channel's buffer. It is possible to set a specific |
| 3113 | * offset (0 by default) and a length (all remaining input data starting for the |
| 3114 | * offset by default). If there is not enough input data and more data can be |
| 3115 | * received, this function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3116 | * |
| 3117 | * From an action, All input data are considered. For a filter, the offset and |
| 3118 | * 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] | 3119 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3120 | __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] | 3121 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3122 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3123 | struct filter *filter; |
| 3124 | size_t input, output; |
| 3125 | int offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3126 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3127 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3128 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3129 | output = co_data(chn); |
| 3130 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3131 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3132 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3133 | if (filter && !hlua_filter_from_payload(filter)) |
| 3134 | WILL_LJMP(lua_error(L)); |
| 3135 | |
| 3136 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3137 | if (lua_gettop(L) > 1) { |
| 3138 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3139 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3140 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3141 | offset += output; |
| 3142 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3143 | lua_pushfstring(L, "offset out of range."); |
| 3144 | WILL_LJMP(lua_error(L)); |
| 3145 | } |
| 3146 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3147 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3148 | if (lua_gettop(L) == 3) { |
| 3149 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3150 | if (!len) |
| 3151 | goto dup; |
| 3152 | if (len == -1) |
| 3153 | len = global.tune.bufsize; |
| 3154 | if (len < 0) { |
| 3155 | lua_pushfstring(L, "length out of range."); |
| 3156 | WILL_LJMP(lua_error(L)); |
| 3157 | } |
| 3158 | } |
| 3159 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3160 | if (offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3161 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3162 | /* Yield waiting for more data, as requested */ |
| 3163 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_data_yield, TICK_ETERNITY, 0)); |
| 3164 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3165 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3166 | } |
| 3167 | |
| 3168 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3169 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3170 | return 1; |
| 3171 | } |
| 3172 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3173 | /* Copies the first line (including the trailing LF) of input data in the |
| 3174 | * channel's buffer. It is possible to set a specific offset (0 by default) and |
| 3175 | * a length (all remaining input data starting for the offset by default). If |
| 3176 | * there is not enough input data and more data can be received, the function |
| 3177 | * yields. If a length is explicitly specified, no more data are |
| 3178 | * copied. Otherwise, if no LF is found and more data can be received, this |
| 3179 | * function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3180 | * |
| 3181 | * From an action, All input data are considered. For a filter, the offset and |
| 3182 | * 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] | 3183 | */ |
| 3184 | __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] | 3185 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3186 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3187 | struct filter *filter; |
| 3188 | size_t l, input, output; |
| 3189 | int offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3190 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3191 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3192 | output = co_data(chn); |
| 3193 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3194 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3195 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3196 | if (filter && !hlua_filter_from_payload(filter)) |
| 3197 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3198 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3199 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3200 | if (lua_gettop(L) > 1) { |
| 3201 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3202 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3203 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3204 | offset += output; |
| 3205 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3206 | lua_pushfstring(L, "offset out of range."); |
| 3207 | WILL_LJMP(lua_error(L)); |
| 3208 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3209 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3210 | |
| 3211 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3212 | if (lua_gettop(L) == 3) { |
| 3213 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3214 | if (!len) |
| 3215 | goto dup; |
| 3216 | if (len == -1) |
| 3217 | len = global.tune.bufsize; |
| 3218 | if (len < 0) { |
| 3219 | lua_pushfstring(L, "length out of range."); |
| 3220 | WILL_LJMP(lua_error(L)); |
| 3221 | } |
| 3222 | } |
| 3223 | |
| 3224 | for (l = 0; l < len; l++) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3225 | if (l + offset >= output + input) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3226 | break; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3227 | if (*(b_peek(&chn->buf, offset + l)) == '\n') { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3228 | len = l+1; |
| 3229 | goto dup; |
| 3230 | } |
| 3231 | } |
| 3232 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3233 | if (offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3234 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3235 | /* Yield waiting for more data */ |
| 3236 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_line_yield, TICK_ETERNITY, 0)); |
| 3237 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3238 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3239 | } |
| 3240 | |
| 3241 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3242 | _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3243 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3244 | } |
| 3245 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3246 | /* [ DEPRECATED ] |
| 3247 | * |
| 3248 | * Duplicate all input data foud in the channel's buffer. The data are not |
| 3249 | * removed from the buffer. This function relies on _hlua_channel_dup(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3250 | * |
| 3251 | * From an action, All input data are considered. For a filter, the offset and |
| 3252 | * 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] | 3253 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3254 | __LJMP static int hlua_channel_dup(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3255 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3256 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3257 | struct filter *filter; |
| 3258 | size_t offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3259 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3260 | MAY_LJMP(check_args(L, 1, "dup")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3261 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3262 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3263 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3264 | WILL_LJMP(lua_error(L)); |
| 3265 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3266 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3267 | offset = co_data(chn); |
| 3268 | len = ci_data(chn); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3269 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3270 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3271 | if (filter && !hlua_filter_from_payload(filter)) |
| 3272 | WILL_LJMP(lua_error(L)); |
| 3273 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3274 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3275 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3276 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3277 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3278 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3279 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3280 | return 1; |
| 3281 | } |
| 3282 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3283 | /* [ DEPRECATED ] |
| 3284 | * |
| 3285 | * Get all input data foud in the channel's buffer. The data are removed from |
| 3286 | * the buffer after the copy. This function relies on _hlua_channel_dup() and |
| 3287 | * _hlua_channel_delete(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3288 | * |
| 3289 | * From an action, All input data are considered. For a filter, the offset and |
| 3290 | * 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] | 3291 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3292 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3293 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3294 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3295 | struct filter *filter; |
| 3296 | size_t offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3297 | int ret; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3298 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3299 | MAY_LJMP(check_args(L, 1, "get")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3300 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3301 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3302 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3303 | WILL_LJMP(lua_error(L)); |
| 3304 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3305 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3306 | offset = co_data(chn); |
| 3307 | len = ci_data(chn); |
| 3308 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3309 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3310 | if (filter && !hlua_filter_from_payload(filter)) |
| 3311 | WILL_LJMP(lua_error(L)); |
| 3312 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3313 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3314 | lua_pushnil(L); |
| 3315 | return 1; |
| 3316 | } |
| 3317 | |
| 3318 | ret = _hlua_channel_dup(chn, L, offset, len); |
| 3319 | _hlua_channel_delete(chn, offset, ret); |
| 3320 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3321 | } |
| 3322 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3323 | /* This functions consumes and returns one line. If the channel is closed, |
| 3324 | * 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] | 3325 | * 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] | 3326 | * value. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3327 | * |
| 3328 | * From an action, All input data are considered. For a filter, the offset and |
| 3329 | * 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] | 3330 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3331 | __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] | 3332 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3333 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3334 | struct filter *filter; |
| 3335 | size_t l, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3336 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3337 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3338 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3339 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3340 | offset = co_data(chn); |
| 3341 | len = ci_data(chn); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3342 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3343 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3344 | if (filter && !hlua_filter_from_payload(filter)) |
| 3345 | WILL_LJMP(lua_error(L)); |
| 3346 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3347 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3348 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3349 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3350 | } |
| 3351 | |
| 3352 | for (l = 0; l < len; l++) { |
| 3353 | if (*(b_peek(&chn->buf, offset+l)) == '\n') { |
| 3354 | len = l+1; |
| 3355 | goto dup; |
| 3356 | } |
| 3357 | } |
| 3358 | |
| 3359 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3360 | /* Yield waiting for more data */ |
| 3361 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0)); |
| 3362 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3363 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3364 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3365 | ret = _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3366 | _hlua_channel_delete(chn, offset, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3367 | return 1; |
| 3368 | } |
| 3369 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3370 | /* [ DEPRECATED ] |
| 3371 | * |
| 3372 | * Check arguments for the function "hlua_channel_getline_yield". |
| 3373 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3374 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3375 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3376 | struct channel *chn; |
| 3377 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3378 | MAY_LJMP(check_args(L, 1, "getline")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3379 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3380 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3381 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3382 | WILL_LJMP(lua_error(L)); |
| 3383 | } |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3384 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3385 | } |
| 3386 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3387 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3388 | * available input data are returned. The offset may be negactive to start from |
| 3389 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3390 | * size. |
| 3391 | */ |
| 3392 | __LJMP static int hlua_channel_get_data(lua_State *L) |
| 3393 | { |
| 3394 | struct channel *chn; |
| 3395 | |
| 3396 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3397 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 3398 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3399 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3400 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3401 | WILL_LJMP(lua_error(L)); |
| 3402 | } |
| 3403 | return MAY_LJMP(hlua_channel_get_data_yield(L, 0, 0)); |
| 3404 | } |
| 3405 | |
| 3406 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3407 | * available input data are returned. The offset may be negactive to start from |
| 3408 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3409 | * size. |
| 3410 | */ |
| 3411 | __LJMP static int hlua_channel_get_line(lua_State *L) |
| 3412 | { |
| 3413 | struct channel *chn; |
| 3414 | |
| 3415 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3416 | WILL_LJMP(luaL_error(L, "'line' expects at most 2 arguments")); |
| 3417 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3418 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3419 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3420 | WILL_LJMP(lua_error(L)); |
| 3421 | } |
| 3422 | return MAY_LJMP(hlua_channel_get_line_yield(L, 0, 0)); |
| 3423 | } |
| 3424 | |
| 3425 | /* Appends a string into the input side of channel. It returns the length of the |
| 3426 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3427 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3428 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3429 | * |
| 3430 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3431 | */ |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3432 | __LJMP static int hlua_channel_append(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3433 | { |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3434 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3435 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3436 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3437 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3438 | int ret; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3439 | |
| 3440 | MAY_LJMP(check_args(L, 2, "append")); |
| 3441 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3442 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3443 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3444 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3445 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3446 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3447 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3448 | offset = co_data(chn); |
| 3449 | len = ci_data(chn); |
| 3450 | |
| 3451 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3452 | if (filter && !hlua_filter_from_payload(filter)) |
| 3453 | WILL_LJMP(lua_error(L)); |
| 3454 | |
| 3455 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3456 | if (ret > 0 && filter) { |
| 3457 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3458 | |
| 3459 | flt_update_offsets(filter, chn, ret); |
| 3460 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3461 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3462 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3463 | return 1; |
| 3464 | } |
| 3465 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3466 | /* Prepends a string into the input side of channel. It returns the length of the |
| 3467 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3468 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3469 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3470 | * |
| 3471 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3472 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3473 | __LJMP static int hlua_channel_prepend(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3474 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3475 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3476 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3477 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3478 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3479 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3480 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3481 | MAY_LJMP(check_args(L, 2, "prepend")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3482 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3483 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3484 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3485 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3486 | WILL_LJMP(lua_error(L)); |
| 3487 | } |
| 3488 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3489 | offset = co_data(chn); |
| 3490 | len = ci_data(chn); |
| 3491 | |
| 3492 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3493 | if (filter && !hlua_filter_from_payload(filter)) |
| 3494 | WILL_LJMP(lua_error(L)); |
| 3495 | |
| 3496 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3497 | if (ret > 0 && filter) { |
| 3498 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3499 | |
| 3500 | flt_update_offsets(filter, chn, ret); |
| 3501 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3502 | } |
| 3503 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3504 | lua_pushinteger(L, ret); |
| 3505 | return 1; |
| 3506 | } |
| 3507 | |
| 3508 | /* Inserts a given amount of input data at the given offset by a string |
| 3509 | * content. By default the string is appended at the end of input data. It |
| 3510 | * returns the length of the written string, or -1 if the channel is closed or |
| 3511 | * if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3512 | * |
| 3513 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3514 | */ |
| 3515 | __LJMP static int hlua_channel_insert_data(lua_State *L) |
| 3516 | { |
| 3517 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3518 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3519 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3520 | size_t sz, input, output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3521 | int ret, offset; |
| 3522 | |
| 3523 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 3524 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
| 3525 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3526 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3527 | |
| 3528 | output = co_data(chn); |
| 3529 | input = ci_data(chn); |
| 3530 | |
| 3531 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3532 | if (filter && !hlua_filter_from_payload(filter)) |
| 3533 | WILL_LJMP(lua_error(L)); |
| 3534 | |
| 3535 | offset = input + output; |
| 3536 | if (lua_gettop(L) > 2) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3537 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3538 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3539 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3540 | offset += output; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3541 | if (offset < output || offset > output + input) { |
| 3542 | lua_pushfstring(L, "offset out of range."); |
| 3543 | WILL_LJMP(lua_error(L)); |
| 3544 | } |
| 3545 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3546 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3547 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3548 | WILL_LJMP(lua_error(L)); |
| 3549 | } |
| 3550 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3551 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3552 | if (ret > 0 && filter) { |
| 3553 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3554 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3555 | flt_update_offsets(filter, chn, ret); |
| 3556 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3557 | } |
| 3558 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3559 | lua_pushinteger(L, ret); |
| 3560 | return 1; |
| 3561 | } |
| 3562 | /* Replaces a given amount of input data at the given offset by a string |
| 3563 | * content. By default all remaining data are removed (offset = 0 and len = |
| 3564 | * -1). It returns the length of the written string, or -1 if the channel is |
| 3565 | * closed or if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3566 | * |
| 3567 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3568 | */ |
| 3569 | __LJMP static int hlua_channel_set_data(lua_State *L) |
| 3570 | { |
| 3571 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3572 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3573 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3574 | size_t sz, input, output; |
| 3575 | int ret, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3576 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3577 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 3578 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 3579 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3580 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3581 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3582 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3583 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3584 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3585 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3586 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3587 | output = co_data(chn); |
| 3588 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3589 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3590 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3591 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3592 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3593 | |
| 3594 | offset = output; |
| 3595 | if (lua_gettop(L) > 2) { |
| 3596 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3597 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3598 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3599 | offset += output; |
| 3600 | if (offset < output || offset > input + output) { |
| 3601 | lua_pushfstring(L, "offset out of range."); |
| 3602 | WILL_LJMP(lua_error(L)); |
| 3603 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3604 | } |
| 3605 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3606 | len = output + input - offset; |
| 3607 | if (lua_gettop(L) == 4) { |
| 3608 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 3609 | if (!len) |
| 3610 | goto set; |
| 3611 | if (len == -1) |
| 3612 | len = output + input - offset; |
| 3613 | if (len < 0 || offset + len > output + input) { |
| 3614 | lua_pushfstring(L, "length out of range."); |
| 3615 | WILL_LJMP(lua_error(L)); |
| 3616 | } |
| 3617 | } |
| 3618 | |
| 3619 | set: |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3620 | /* 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] | 3621 | if (sz > c_room(chn) + len) |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3622 | lua_pushinteger(L, -1); |
| 3623 | else { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3624 | _hlua_channel_delete(chn, offset, len); |
| 3625 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3626 | if (filter) { |
| 3627 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3628 | |
| 3629 | len -= (ret > 0 ? ret : 0); |
| 3630 | flt_update_offsets(filter, chn, -len); |
| 3631 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3632 | } |
| 3633 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3634 | lua_pushinteger(L, ret); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3635 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3636 | return 1; |
| 3637 | } |
| 3638 | |
| 3639 | /* 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] | 3640 | * 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] | 3641 | * the removed data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3642 | * |
| 3643 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3644 | */ |
| 3645 | __LJMP static int hlua_channel_del_data(lua_State *L) |
| 3646 | { |
| 3647 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3648 | struct filter *filter; |
| 3649 | size_t input, output; |
| 3650 | int offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3651 | |
| 3652 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3653 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
| 3654 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3655 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3656 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3657 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3658 | WILL_LJMP(lua_error(L)); |
| 3659 | } |
| 3660 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3661 | output = co_data(chn); |
| 3662 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3663 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3664 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3665 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3666 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3667 | |
| 3668 | offset = output; |
| 3669 | if (lua_gettop(L) > 2) { |
| 3670 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3671 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3672 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3673 | offset += output; |
| 3674 | if (offset < output || offset > input + output) { |
| 3675 | lua_pushfstring(L, "offset out of range."); |
| 3676 | WILL_LJMP(lua_error(L)); |
| 3677 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3678 | } |
| 3679 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3680 | len = output + input - offset; |
| 3681 | if (lua_gettop(L) == 4) { |
| 3682 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 3683 | if (!len) |
| 3684 | goto end; |
| 3685 | if (len == -1) |
| 3686 | len = output + input - offset; |
| 3687 | if (len < 0 || offset + len > output + input) { |
| 3688 | lua_pushfstring(L, "length out of range."); |
| 3689 | WILL_LJMP(lua_error(L)); |
| 3690 | } |
| 3691 | } |
| 3692 | |
| 3693 | _hlua_channel_delete(chn, offset, len); |
| 3694 | if (filter) { |
| 3695 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3696 | |
| 3697 | flt_update_offsets(filter, chn, -len); |
| 3698 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3699 | } |
| 3700 | |
| 3701 | end: |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3702 | lua_pushinteger(L, len); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3703 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3704 | } |
| 3705 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3706 | /* Append data in the output side of the buffer. This data is immediately |
| 3707 | * sent. The function returns the amount of data written. If the buffer |
| 3708 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3709 | * if the channel is closed. |
| 3710 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3711 | __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] | 3712 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3713 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3714 | struct filter *filter; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3715 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3716 | size_t offset, len, sz; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3717 | int l, ret; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3718 | struct hlua *hlua; |
| 3719 | |
| 3720 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3721 | hlua = hlua_gethlua(L); |
| 3722 | if (!hlua) { |
| 3723 | lua_pushnil(L); |
| 3724 | return 1; |
| 3725 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3726 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3727 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3728 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3729 | l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3730 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3731 | offset = co_data(chn); |
| 3732 | len = ci_data(chn); |
| 3733 | |
| 3734 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3735 | if (filter && !hlua_filter_from_payload(filter)) |
| 3736 | WILL_LJMP(lua_error(L)); |
| 3737 | |
| 3738 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3739 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3740 | lua_pushinteger(L, -1); |
| 3741 | return 1; |
| 3742 | } |
| 3743 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3744 | len = c_room(chn); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3745 | if (len > sz -l) { |
| 3746 | if (filter) { |
| 3747 | lua_pushinteger(L, -1); |
| 3748 | return 1; |
| 3749 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3750 | len = sz - l; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3751 | } |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3752 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3753 | ret = _hlua_channel_insert(chn, L, ist2(str, len), offset); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3754 | if (ret == -1) { |
| 3755 | lua_pop(L, 1); |
| 3756 | lua_pushinteger(L, -1); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3757 | return 1; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3758 | } |
| 3759 | if (ret) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3760 | if (filter) { |
| 3761 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3762 | |
| 3763 | |
| 3764 | flt_update_offsets(filter, chn, ret); |
| 3765 | FLT_OFF(filter, chn) += ret; |
| 3766 | flt_ctx->cur_off[CHN_IDX(chn)] += ret; |
| 3767 | } |
| 3768 | else |
| 3769 | c_adv(chn, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3770 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3771 | l += ret; |
| 3772 | lua_pop(L, 1); |
| 3773 | lua_pushinteger(L, l); |
| 3774 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3775 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3776 | if (l < sz) { |
| 3777 | /* Yield only if the channel's output is not empty. |
| 3778 | * Otherwise it means we cannot add more data. */ |
| 3779 | if (co_data(chn) == 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3780 | return 1; |
| 3781 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3782 | /* If we are waiting for space in the response buffer, we |
| 3783 | * must set the flag WAKERESWR. This flag required the task |
| 3784 | * wake up if any activity is detected on the response buffer. |
| 3785 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3786 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3787 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3788 | else |
| 3789 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3790 | 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] | 3791 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3792 | |
| 3793 | return 1; |
| 3794 | } |
| 3795 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3796 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3797 | * yield the LUA process, and resume it without checking the |
| 3798 | * input arguments. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3799 | * |
| 3800 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3801 | */ |
| 3802 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3803 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3804 | struct channel *chn; |
| 3805 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3806 | MAY_LJMP(check_args(L, 2, "send")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3807 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3808 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3809 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3810 | WILL_LJMP(lua_error(L)); |
| 3811 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3812 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3813 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3814 | } |
| 3815 | |
| 3816 | /* This function forward and amount of butes. The data pass from |
| 3817 | * the input side of the buffer to the output side, and can be |
| 3818 | * forwarded. This function never fails. |
| 3819 | * |
| 3820 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3821 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3822 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3823 | __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] | 3824 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3825 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3826 | struct filter *filter; |
| 3827 | size_t offset, len, fwd; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3828 | int l, max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3829 | struct hlua *hlua; |
| 3830 | |
| 3831 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3832 | hlua = hlua_gethlua(L); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3833 | if (!hlua) { |
| 3834 | lua_pushnil(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3835 | return 1; |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3836 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3837 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3838 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3839 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3840 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3841 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3842 | offset = co_data(chn); |
| 3843 | len = ci_data(chn); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3844 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3845 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3846 | if (filter && !hlua_filter_from_payload(filter)) |
| 3847 | WILL_LJMP(lua_error(L)); |
| 3848 | |
| 3849 | max = fwd - l; |
| 3850 | if (max > len) |
| 3851 | max = len; |
| 3852 | |
| 3853 | if (filter) { |
| 3854 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3855 | |
| 3856 | FLT_OFF(filter, chn) += max; |
| 3857 | flt_ctx->cur_off[CHN_IDX(chn)] += max; |
| 3858 | flt_ctx->cur_len[CHN_IDX(chn)] -= max; |
| 3859 | } |
| 3860 | else |
| 3861 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3862 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3863 | l += max; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3864 | lua_pop(L, 1); |
| 3865 | lua_pushinteger(L, l); |
| 3866 | |
| 3867 | /* Check if it miss bytes to forward. */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3868 | if (l < fwd) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3869 | /* The the input channel or the output channel are closed, we |
| 3870 | * must return the amount of data forwarded. |
| 3871 | */ |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3872 | 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] | 3873 | return 1; |
| 3874 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3875 | /* If we are waiting for space data in the response buffer, we |
| 3876 | * must set the flag WAKERESWR. This flag required the task |
| 3877 | * wake up if any activity is detected on the response buffer. |
| 3878 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3879 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3880 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3881 | else |
| 3882 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3883 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3884 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3885 | 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] | 3886 | } |
| 3887 | |
| 3888 | return 1; |
| 3889 | } |
| 3890 | |
| 3891 | /* Just check the input and prepare the stack for the previous |
| 3892 | * function "hlua_channel_forward_yield" |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3893 | * |
| 3894 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3895 | */ |
| 3896 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3897 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3898 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3899 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3900 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3901 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3902 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3903 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3904 | WILL_LJMP(lua_error(L)); |
| 3905 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3906 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3907 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3908 | } |
| 3909 | |
| 3910 | /* Just returns the number of bytes available in the input |
| 3911 | * side of the buffer. This function never fails. |
| 3912 | */ |
| 3913 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3914 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3915 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3916 | struct filter *filter; |
| 3917 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3918 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3919 | MAY_LJMP(check_args(L, 1, "input")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3920 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3921 | |
| 3922 | output = co_data(chn); |
| 3923 | input = ci_data(chn); |
| 3924 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3925 | if (filter || !IS_HTX_STRM(chn_strm(chn))) |
| 3926 | lua_pushinteger(L, input); |
| 3927 | else { |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3928 | struct htx *htx = htxbuf(&chn->buf); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3929 | |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3930 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3931 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3932 | return 1; |
| 3933 | } |
| 3934 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3935 | /* Returns true if the channel is full. */ |
| 3936 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3937 | { |
| 3938 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3939 | |
| 3940 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3941 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3942 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3943 | * applet). |
| 3944 | */ |
| 3945 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3946 | return 1; |
| 3947 | } |
| 3948 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3949 | /* Returns true if the channel may still receive data. */ |
| 3950 | __LJMP static int hlua_channel_may_recv(lua_State *L) |
| 3951 | { |
| 3952 | struct channel *chn; |
| 3953 | |
| 3954 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 3955 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3956 | lua_pushboolean(L, (!channel_input_closed(chn) && channel_may_recv(chn))); |
| 3957 | return 1; |
| 3958 | } |
| 3959 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3960 | /* Returns true if the channel is the response channel. */ |
| 3961 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3962 | { |
| 3963 | struct channel *chn; |
| 3964 | |
| 3965 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3966 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3967 | |
| 3968 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3969 | return 1; |
| 3970 | } |
| 3971 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3972 | /* Just returns the number of bytes available in the output |
| 3973 | * side of the buffer. This function never fails. |
| 3974 | */ |
| 3975 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3976 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3977 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3978 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3979 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3980 | MAY_LJMP(check_args(L, 1, "output")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3981 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3982 | |
| 3983 | output = co_data(chn); |
| 3984 | input = ci_data(chn); |
| 3985 | hlua_channel_filter(L, 1, chn, &output, &input); |
| 3986 | |
| 3987 | lua_pushinteger(L, output); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3988 | return 1; |
| 3989 | } |
| 3990 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3991 | /* |
| 3992 | * |
| 3993 | * |
| 3994 | * Class Fetches |
| 3995 | * |
| 3996 | * |
| 3997 | */ |
| 3998 | |
| 3999 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4000 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4001 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4002 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4003 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4004 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4005 | } |
| 4006 | |
| 4007 | /* This function creates and push in the stack a fetch object according |
| 4008 | * with a current TXN. |
| 4009 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4010 | 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] | 4011 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4012 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4013 | |
| 4014 | /* Check stack size. */ |
| 4015 | if (!lua_checkstack(L, 3)) |
| 4016 | return 0; |
| 4017 | |
| 4018 | /* Create the object: obj[0] = userdata. |
| 4019 | * Note that the base of the Fetches object is the |
| 4020 | * transaction object. |
| 4021 | */ |
| 4022 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4023 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4024 | lua_rawseti(L, -2, 0); |
| 4025 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4026 | hsmp->s = txn->s; |
| 4027 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4028 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4029 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4030 | |
| 4031 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 4032 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 4033 | lua_setmetatable(L, -2); |
| 4034 | |
| 4035 | return 1; |
| 4036 | } |
| 4037 | |
| 4038 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 4039 | * It uses closure argument to store the associated sample-fetch. It |
| 4040 | * returns only one argument or throws an error. An error is thrown |
| 4041 | * only if an error is encountered during the argument parsing. If |
| 4042 | * the "sample-fetch" function fails, nil is returned. |
| 4043 | */ |
| 4044 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 4045 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4046 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4047 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4048 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4049 | int i; |
| 4050 | struct sample smp; |
| 4051 | |
| 4052 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4053 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4054 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4055 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4056 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4057 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 4058 | /* Check execution authorization. */ |
| 4059 | if (f->use & SMP_USE_HTTP_ANY && |
| 4060 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 4061 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 4062 | "is not available in Lua services", f->kw); |
| 4063 | WILL_LJMP(lua_error(L)); |
| 4064 | } |
| 4065 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4066 | /* Get extra arguments. */ |
| 4067 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 4068 | if (i >= ARGM_NBARGS) |
| 4069 | break; |
| 4070 | hlua_lua2arg(L, i + 2, &args[i]); |
| 4071 | } |
| 4072 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4073 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4074 | |
| 4075 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4076 | 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] | 4077 | |
| 4078 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4079 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4080 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4081 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4082 | } |
| 4083 | |
| 4084 | /* Initialise the sample. */ |
| 4085 | memset(&smp, 0, sizeof(smp)); |
| 4086 | |
| 4087 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4088 | 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] | 4089 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4090 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4091 | lua_pushstring(L, ""); |
| 4092 | else |
| 4093 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4094 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4095 | } |
| 4096 | |
| 4097 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4098 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4099 | hlua_smp2lua_str(L, &smp); |
| 4100 | else |
| 4101 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4102 | |
| 4103 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4104 | free_args(args); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4105 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4106 | |
| 4107 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4108 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4109 | WILL_LJMP(lua_error(L)); |
| 4110 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4111 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4112 | |
| 4113 | /* |
| 4114 | * |
| 4115 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4116 | * Class Converters |
| 4117 | * |
| 4118 | * |
| 4119 | */ |
| 4120 | |
| 4121 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4122 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4123 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4124 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4125 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4126 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4127 | } |
| 4128 | |
| 4129 | /* This function creates and push in the stack a Converters object |
| 4130 | * according with a current TXN. |
| 4131 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4132 | 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] | 4133 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4134 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4135 | |
| 4136 | /* Check stack size. */ |
| 4137 | if (!lua_checkstack(L, 3)) |
| 4138 | return 0; |
| 4139 | |
| 4140 | /* Create the object: obj[0] = userdata. |
| 4141 | * Note that the base of the Converters object is the |
| 4142 | * same than the TXN object. |
| 4143 | */ |
| 4144 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4145 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4146 | lua_rawseti(L, -2, 0); |
| 4147 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4148 | hsmp->s = txn->s; |
| 4149 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4150 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4151 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4152 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4153 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4154 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 4155 | lua_setmetatable(L, -2); |
| 4156 | |
| 4157 | return 1; |
| 4158 | } |
| 4159 | |
| 4160 | /* This function is an LUA binding. It is called with each converter. |
| 4161 | * It uses closure argument to store the associated converter. It |
| 4162 | * returns only one argument or throws an error. An error is thrown |
| 4163 | * only if an error is encountered during the argument parsing. If |
| 4164 | * the converter function function fails, nil is returned. |
| 4165 | */ |
| 4166 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 4167 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4168 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4169 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4170 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4171 | int i; |
| 4172 | struct sample smp; |
| 4173 | |
| 4174 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4175 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4176 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4177 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4178 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4179 | |
| 4180 | /* Get extra arguments. */ |
| 4181 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 4182 | if (i >= ARGM_NBARGS) |
| 4183 | break; |
| 4184 | hlua_lua2arg(L, i + 3, &args[i]); |
| 4185 | } |
| 4186 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4187 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4188 | |
| 4189 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4190 | 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] | 4191 | |
| 4192 | /* Run the special args checker. */ |
| 4193 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 4194 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4195 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4196 | } |
| 4197 | |
| 4198 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4199 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4200 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 4201 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4202 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4203 | } |
| 4204 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4205 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 4206 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4207 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4208 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4209 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4210 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4211 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4212 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4213 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 4214 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4215 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4216 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4217 | } |
| 4218 | |
| 4219 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4220 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4221 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4222 | lua_pushstring(L, ""); |
| 4223 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4224 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4225 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4226 | } |
| 4227 | |
| 4228 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4229 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4230 | hlua_smp2lua_str(L, &smp); |
| 4231 | else |
| 4232 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4233 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4234 | free_args(args); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4235 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4236 | |
| 4237 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4238 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4239 | WILL_LJMP(lua_error(L)); |
| 4240 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4241 | } |
| 4242 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4243 | /* |
| 4244 | * |
| 4245 | * |
| 4246 | * Class AppletTCP |
| 4247 | * |
| 4248 | * |
| 4249 | */ |
| 4250 | |
| 4251 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4252 | * a class stream, otherwise it throws an error. |
| 4253 | */ |
| 4254 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 4255 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4256 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4257 | } |
| 4258 | |
| 4259 | /* This function creates and push in the stack an Applet object |
| 4260 | * according with a current TXN. |
| 4261 | */ |
| 4262 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 4263 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4264 | struct hlua_appctx *luactx; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4265 | struct stream *s = __cs_strm(ctx->owner); |
Christopher Faulet | 2da02ae | 2022-02-24 13:45:27 +0100 | [diff] [blame] | 4266 | struct proxy *p; |
| 4267 | |
| 4268 | ALREADY_CHECKED(s); |
| 4269 | p = s->be; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4270 | |
| 4271 | /* Check stack size. */ |
| 4272 | if (!lua_checkstack(L, 3)) |
| 4273 | return 0; |
| 4274 | |
| 4275 | /* Create the object: obj[0] = userdata. |
| 4276 | * Note that the base of the Converters object is the |
| 4277 | * same than the TXN object. |
| 4278 | */ |
| 4279 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4280 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4281 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4282 | luactx->appctx = ctx; |
| 4283 | luactx->htxn.s = s; |
| 4284 | luactx->htxn.p = p; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4285 | |
| 4286 | /* Create the "f" field that contains a list of fetches. */ |
| 4287 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4288 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4289 | return 0; |
| 4290 | lua_settable(L, -3); |
| 4291 | |
| 4292 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4293 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4294 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4295 | return 0; |
| 4296 | lua_settable(L, -3); |
| 4297 | |
| 4298 | /* Create the "c" field that contains a list of converters. */ |
| 4299 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4300 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4301 | return 0; |
| 4302 | lua_settable(L, -3); |
| 4303 | |
| 4304 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4305 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4306 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4307 | return 0; |
| 4308 | lua_settable(L, -3); |
| 4309 | |
| 4310 | /* Pop a class stream metatable and affect it to the table. */ |
| 4311 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 4312 | lua_setmetatable(L, -2); |
| 4313 | |
| 4314 | return 1; |
| 4315 | } |
| 4316 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4317 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 4318 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4319 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4320 | struct stream *s; |
| 4321 | const char *name; |
| 4322 | size_t len; |
| 4323 | struct sample smp; |
| 4324 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4325 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4326 | 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] | 4327 | |
| 4328 | /* It is useles to retrieve the stream, but this function |
| 4329 | * runs only in a stream context. |
| 4330 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4331 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4332 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4333 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4334 | |
| 4335 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4336 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4337 | hlua_lua2smp(L, 3, &smp); |
| 4338 | |
| 4339 | /* Store the sample in a variable. */ |
| 4340 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4341 | |
| 4342 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4343 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4344 | else |
| 4345 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4346 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4347 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4348 | } |
| 4349 | |
| 4350 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 4351 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4352 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4353 | struct stream *s; |
| 4354 | const char *name; |
| 4355 | size_t len; |
| 4356 | struct sample smp; |
| 4357 | |
| 4358 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4359 | |
| 4360 | /* It is useles to retrieve the stream, but this function |
| 4361 | * runs only in a stream context. |
| 4362 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4363 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4364 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4365 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4366 | |
| 4367 | /* Unset the variable. */ |
| 4368 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4369 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4370 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4371 | } |
| 4372 | |
| 4373 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 4374 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4375 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4376 | struct stream *s; |
| 4377 | const char *name; |
| 4378 | size_t len; |
| 4379 | struct sample smp; |
| 4380 | |
| 4381 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4382 | |
| 4383 | /* It is useles to retrieve the stream, but this function |
| 4384 | * runs only in a stream context. |
| 4385 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4386 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4387 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4388 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4389 | |
| 4390 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 4391 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4392 | lua_pushnil(L); |
| 4393 | return 1; |
| 4394 | } |
| 4395 | |
| 4396 | return hlua_smp2lua(L, &smp); |
| 4397 | } |
| 4398 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4399 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 4400 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4401 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4402 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4403 | struct hlua *hlua; |
| 4404 | |
| 4405 | /* 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] | 4406 | if (!s->hlua) |
| 4407 | return 0; |
| 4408 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4409 | |
| 4410 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4411 | |
| 4412 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4413 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4414 | |
| 4415 | /* Get and store new value. */ |
| 4416 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4417 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4418 | |
| 4419 | return 0; |
| 4420 | } |
| 4421 | |
| 4422 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 4423 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4424 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4425 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4426 | struct hlua *hlua; |
| 4427 | |
| 4428 | /* 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] | 4429 | if (!s->hlua) { |
| 4430 | lua_pushnil(L); |
| 4431 | return 1; |
| 4432 | } |
| 4433 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4434 | |
| 4435 | /* Push configuration index in the stack. */ |
| 4436 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4437 | |
| 4438 | return 1; |
| 4439 | } |
| 4440 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4441 | /* If expected data not yet available, it returns a yield. This function |
| 4442 | * consumes the data in the buffer. It returns a string containing the |
| 4443 | * data. This string can be empty. |
| 4444 | */ |
| 4445 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4446 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4447 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4448 | struct conn_stream *cs = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4449 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4450 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4451 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4452 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4453 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4454 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4455 | /* Read the maximum amount of data available. */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4456 | ret = co_getline_nc(cs_oc(cs), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4457 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4458 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4459 | if (ret == 0) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 4460 | cs_cant_get(cs); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4461 | 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] | 4462 | } |
| 4463 | |
| 4464 | /* End of data: commit the total strings and return. */ |
| 4465 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4466 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4467 | return 1; |
| 4468 | } |
| 4469 | |
| 4470 | /* Ensure that the block 2 length is usable. */ |
| 4471 | if (ret == 1) |
| 4472 | len2 = 0; |
| 4473 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4474 | /* don't check the max length read and don't check. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4475 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4476 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4477 | |
| 4478 | /* Consume input channel output buffer data. */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4479 | co_skip(cs_oc(cs), len1 + len2); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4480 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4481 | return 1; |
| 4482 | } |
| 4483 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4484 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4485 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 4486 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4487 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4488 | |
| 4489 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4490 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4491 | |
| 4492 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 4493 | } |
| 4494 | |
| 4495 | /* If expected data not yet available, it returns a yield. This function |
| 4496 | * consumes the data in the buffer. It returns a string containing the |
| 4497 | * data. This string can be empty. |
| 4498 | */ |
| 4499 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4500 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4501 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4502 | struct conn_stream *cs = luactx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4503 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4504 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4505 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4506 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4507 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4508 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4509 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4510 | /* Read the maximum amount of data available. */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4511 | ret = co_getblk_nc(cs_oc(cs), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4512 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4513 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4514 | if (ret == 0) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 4515 | cs_cant_get(cs); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4516 | 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] | 4517 | } |
| 4518 | |
| 4519 | /* End of data: commit the total strings and return. */ |
| 4520 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4521 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4522 | return 1; |
| 4523 | } |
| 4524 | |
| 4525 | /* Ensure that the block 2 length is usable. */ |
| 4526 | if (ret == 1) |
| 4527 | len2 = 0; |
| 4528 | |
| 4529 | if (len == -1) { |
| 4530 | |
| 4531 | /* If len == -1, catenate all the data avalaile and |
| 4532 | * yield because we want to get all the data until |
| 4533 | * the end of data stream. |
| 4534 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4535 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4536 | luaL_addlstring(&luactx->b, blk2, len2); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4537 | co_skip(cs_oc(cs), len1 + len2); |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 4538 | cs_cant_get(cs); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4539 | 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] | 4540 | |
| 4541 | } else { |
| 4542 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4543 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4544 | if (len1 > len) |
| 4545 | len1 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4546 | luaL_addlstring(&luactx->b, blk1, len1); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4547 | len -= len1; |
| 4548 | |
| 4549 | /* Copy the second block. */ |
| 4550 | if (len2 > len) |
| 4551 | len2 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4552 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4553 | len -= len2; |
| 4554 | |
| 4555 | /* Consume input channel output buffer data. */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4556 | co_skip(cs_oc(cs), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4557 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4558 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4559 | if (len > 0) { |
| 4560 | lua_pushinteger(L, len); |
| 4561 | lua_replace(L, 2); |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 4562 | cs_cant_get(cs); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4563 | 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] | 4564 | } |
| 4565 | |
| 4566 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4567 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4568 | return 1; |
| 4569 | } |
| 4570 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4571 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4572 | hlua_pusherror(L, "Lua: internal error"); |
| 4573 | WILL_LJMP(lua_error(L)); |
| 4574 | return 0; |
| 4575 | } |
| 4576 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4577 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4578 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 4579 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4580 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4581 | int len = -1; |
| 4582 | |
| 4583 | if (lua_gettop(L) > 2) |
| 4584 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4585 | if (lua_gettop(L) >= 2) { |
| 4586 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4587 | lua_pop(L, 1); |
| 4588 | } |
| 4589 | |
| 4590 | /* Confirm or set the required length */ |
| 4591 | lua_pushinteger(L, len); |
| 4592 | |
| 4593 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4594 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4595 | |
| 4596 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4597 | } |
| 4598 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4599 | /* Append data in the output side of the buffer. This data is immediately |
| 4600 | * sent. The function returns the amount of data written. If the buffer |
| 4601 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4602 | * if the channel is closed. |
| 4603 | */ |
| 4604 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4605 | { |
| 4606 | size_t len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4607 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4608 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4609 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4610 | struct conn_stream *cs = luactx->appctx->owner; |
| 4611 | struct channel *chn = cs_ic(cs); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4612 | int max; |
| 4613 | |
| 4614 | /* Get the max amount of data which can write as input in the channel. */ |
| 4615 | max = channel_recv_max(chn); |
| 4616 | if (max > (len - l)) |
| 4617 | max = len - l; |
| 4618 | |
| 4619 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4620 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4621 | |
| 4622 | /* update counters. */ |
| 4623 | l += max; |
| 4624 | lua_pop(L, 1); |
| 4625 | lua_pushinteger(L, l); |
| 4626 | |
| 4627 | /* If some data is not send, declares the situation to the |
| 4628 | * applet, and returns a yield. |
| 4629 | */ |
| 4630 | if (l < len) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 4631 | cs_rx_room_blk(cs); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4632 | 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] | 4633 | } |
| 4634 | |
| 4635 | return 1; |
| 4636 | } |
| 4637 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4638 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4639 | * yield the LUA process, and resume it without checking the |
| 4640 | * input arguments. |
| 4641 | */ |
| 4642 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4643 | { |
| 4644 | MAY_LJMP(check_args(L, 2, "send")); |
| 4645 | lua_pushinteger(L, 0); |
| 4646 | |
| 4647 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4648 | } |
| 4649 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4650 | /* |
| 4651 | * |
| 4652 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4653 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4654 | * |
| 4655 | * |
| 4656 | */ |
| 4657 | |
| 4658 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4659 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4660 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4661 | __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] | 4662 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4663 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4664 | } |
| 4665 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4666 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4667 | * according with a current TXN. |
| 4668 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4669 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4670 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4671 | struct hlua_appctx *luactx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4672 | struct hlua_txn htxn; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4673 | struct stream *s = __cs_strm(ctx->owner); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4674 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4675 | struct htx *htx; |
| 4676 | struct htx_blk *blk; |
| 4677 | struct htx_sl *sl; |
| 4678 | struct ist path; |
| 4679 | unsigned long long len = 0; |
| 4680 | int32_t pos; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4681 | struct http_uri_parser parser; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4682 | |
| 4683 | /* Check stack size. */ |
| 4684 | if (!lua_checkstack(L, 3)) |
| 4685 | return 0; |
| 4686 | |
| 4687 | /* Create the object: obj[0] = userdata. |
| 4688 | * Note that the base of the Converters object is the |
| 4689 | * same than the TXN object. |
| 4690 | */ |
| 4691 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4692 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4693 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4694 | luactx->appctx = ctx; |
| 4695 | luactx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
| 4696 | luactx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */ |
| 4697 | luactx->htxn.s = s; |
| 4698 | luactx->htxn.p = px; |
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 "f" field that contains a list of fetches. */ |
| 4701 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4702 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
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 "sf" field that contains a list of stringsafe fetches. */ |
| 4707 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4708 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
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 "c" field that contains a list of converters. */ |
| 4713 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4714 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4715 | return 0; |
| 4716 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4717 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4718 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4719 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4720 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4721 | return 0; |
| 4722 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4723 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4724 | htx = htxbuf(&s->req.buf); |
| 4725 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4726 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4727 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4728 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4729 | /* Stores the request method. */ |
| 4730 | lua_pushstring(L, "method"); |
| 4731 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4732 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4733 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4734 | /* Stores the http version. */ |
| 4735 | lua_pushstring(L, "version"); |
| 4736 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4737 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4738 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4739 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4740 | * the array on the top of the stack. |
| 4741 | */ |
| 4742 | lua_pushstring(L, "headers"); |
| 4743 | htxn.s = s; |
| 4744 | htxn.p = px; |
| 4745 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4746 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4747 | return 0; |
| 4748 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4749 | |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4750 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 4751 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4752 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4753 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4754 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4755 | p = path.ptr; |
Tim Duesterhus | 4c8f75f | 2021-11-06 15:14:44 +0100 | [diff] [blame] | 4756 | end = istend(path); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4757 | q = p; |
| 4758 | while (q < end && *q != '?') |
| 4759 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4760 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4761 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4762 | lua_pushstring(L, "path"); |
| 4763 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4764 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4765 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4766 | /* Stores the query string. */ |
| 4767 | lua_pushstring(L, "qs"); |
| 4768 | if (*q == '?') |
| 4769 | q++; |
| 4770 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4771 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4772 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4773 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4774 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4775 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4776 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4777 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4778 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4779 | break; |
| 4780 | if (type == HTX_BLK_DATA) |
| 4781 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4782 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4783 | if (htx->extra != ULLONG_MAX) |
| 4784 | len += htx->extra; |
| 4785 | |
| 4786 | /* Stores the request path. */ |
| 4787 | lua_pushstring(L, "length"); |
| 4788 | lua_pushinteger(L, len); |
| 4789 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4790 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4791 | /* Create an empty array of HTTP request headers. */ |
| 4792 | lua_pushstring(L, "response"); |
| 4793 | lua_newtable(L); |
| 4794 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4795 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4796 | /* Pop a class stream metatable and affect it to the table. */ |
| 4797 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4798 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4799 | |
| 4800 | return 1; |
| 4801 | } |
| 4802 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4803 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4804 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4805 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4806 | struct stream *s; |
| 4807 | const char *name; |
| 4808 | size_t len; |
| 4809 | struct sample smp; |
| 4810 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4811 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4812 | 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] | 4813 | |
| 4814 | /* It is useles to retrieve the stream, but this function |
| 4815 | * runs only in a stream context. |
| 4816 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4817 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4818 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4819 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4820 | |
| 4821 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4822 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4823 | hlua_lua2smp(L, 3, &smp); |
| 4824 | |
| 4825 | /* Store the sample in a variable. */ |
| 4826 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4827 | |
| 4828 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4829 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4830 | else |
| 4831 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4832 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4833 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4834 | } |
| 4835 | |
| 4836 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4837 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4838 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4839 | struct stream *s; |
| 4840 | const char *name; |
| 4841 | size_t len; |
| 4842 | struct sample smp; |
| 4843 | |
| 4844 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4845 | |
| 4846 | /* It is useles to retrieve the stream, but this function |
| 4847 | * runs only in a stream context. |
| 4848 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4849 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4850 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4851 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4852 | |
| 4853 | /* Unset the variable. */ |
| 4854 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4855 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4856 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4857 | } |
| 4858 | |
| 4859 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4860 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4861 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4862 | struct stream *s; |
| 4863 | const char *name; |
| 4864 | size_t len; |
| 4865 | struct sample smp; |
| 4866 | |
| 4867 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4868 | |
| 4869 | /* It is useles to retrieve the stream, but this function |
| 4870 | * runs only in a stream context. |
| 4871 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4872 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4873 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4874 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4875 | |
| 4876 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 4877 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4878 | lua_pushnil(L); |
| 4879 | return 1; |
| 4880 | } |
| 4881 | |
| 4882 | return hlua_smp2lua(L, &smp); |
| 4883 | } |
| 4884 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4885 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4886 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4887 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4888 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4889 | struct hlua *hlua; |
| 4890 | |
| 4891 | /* 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] | 4892 | if (!s->hlua) |
| 4893 | return 0; |
| 4894 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4895 | |
| 4896 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4897 | |
| 4898 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4899 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4900 | |
| 4901 | /* Get and store new value. */ |
| 4902 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4903 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4904 | |
| 4905 | return 0; |
| 4906 | } |
| 4907 | |
| 4908 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4909 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4910 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4911 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4912 | struct hlua *hlua; |
| 4913 | |
| 4914 | /* 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] | 4915 | if (!s->hlua) { |
| 4916 | lua_pushnil(L); |
| 4917 | return 1; |
| 4918 | } |
| 4919 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4920 | |
| 4921 | /* Push configuration index in the stack. */ |
| 4922 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4923 | |
| 4924 | return 1; |
| 4925 | } |
| 4926 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4927 | /* If expected data not yet available, it returns a yield. This function |
| 4928 | * consumes the data in the buffer. It returns a string containing the |
| 4929 | * data. This string can be empty. |
| 4930 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4931 | __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] | 4932 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4933 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4934 | struct conn_stream *cs = luactx->appctx->owner; |
| 4935 | struct channel *req = cs_oc(cs); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4936 | struct htx *htx; |
| 4937 | struct htx_blk *blk; |
| 4938 | size_t count; |
| 4939 | int stop = 0; |
| 4940 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4941 | htx = htx_from_buf(&req->buf); |
| 4942 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4943 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4944 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4945 | while (count && !stop && blk) { |
| 4946 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4947 | uint32_t sz = htx_get_blksz(blk); |
| 4948 | struct ist v; |
| 4949 | uint32_t vlen; |
| 4950 | char *nl; |
| 4951 | |
| 4952 | vlen = sz; |
| 4953 | if (vlen > count) { |
| 4954 | if (type != HTX_BLK_DATA) |
| 4955 | break; |
| 4956 | vlen = count; |
| 4957 | } |
| 4958 | |
| 4959 | switch (type) { |
| 4960 | case HTX_BLK_UNUSED: |
| 4961 | break; |
| 4962 | |
| 4963 | case HTX_BLK_DATA: |
| 4964 | v = htx_get_blk_value(htx, blk); |
| 4965 | v.len = vlen; |
| 4966 | nl = istchr(v, '\n'); |
| 4967 | if (nl != NULL) { |
| 4968 | stop = 1; |
| 4969 | vlen = nl - v.ptr + 1; |
| 4970 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4971 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4972 | break; |
| 4973 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4974 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4975 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4976 | stop = 1; |
| 4977 | break; |
| 4978 | |
| 4979 | default: |
| 4980 | break; |
| 4981 | } |
| 4982 | |
Willy Tarreau | 8424004 | 2022-02-28 16:51:23 +0100 | [diff] [blame] | 4983 | c_rew(req, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4984 | count -= vlen; |
| 4985 | if (sz == vlen) |
| 4986 | blk = htx_remove_blk(htx, blk); |
| 4987 | else { |
| 4988 | htx_cut_data_blk(htx, blk, vlen); |
| 4989 | break; |
| 4990 | } |
| 4991 | } |
| 4992 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4993 | /* The message was fully consumed and no more data are expected |
| 4994 | * (EOM flag set). |
| 4995 | */ |
| 4996 | if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) |
| 4997 | stop = 1; |
| 4998 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4999 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5000 | if (!stop) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 5001 | cs_cant_get(cs); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5002 | 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] | 5003 | } |
| 5004 | |
| 5005 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5006 | luaL_pushresult(&luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5007 | return 1; |
| 5008 | } |
| 5009 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5010 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5011 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5012 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5013 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5014 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5015 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5016 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5017 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5018 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5019 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5020 | } |
| 5021 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5022 | /* If expected data not yet available, it returns a yield. This function |
| 5023 | * consumes the data in the buffer. It returns a string containing the |
| 5024 | * data. This string can be empty. |
| 5025 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5026 | __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] | 5027 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5028 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 5029 | struct conn_stream *cs = luactx->appctx->owner; |
| 5030 | struct channel *req = cs_oc(cs); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5031 | struct htx *htx; |
| 5032 | struct htx_blk *blk; |
| 5033 | size_t count; |
| 5034 | int len; |
| 5035 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5036 | htx = htx_from_buf(&req->buf); |
| 5037 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5038 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 5039 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5040 | while (count && len && blk) { |
| 5041 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5042 | uint32_t sz = htx_get_blksz(blk); |
| 5043 | struct ist v; |
| 5044 | uint32_t vlen; |
| 5045 | |
| 5046 | vlen = sz; |
| 5047 | if (len > 0 && vlen > len) |
| 5048 | vlen = len; |
| 5049 | if (vlen > count) { |
| 5050 | if (type != HTX_BLK_DATA) |
| 5051 | break; |
| 5052 | vlen = count; |
| 5053 | } |
| 5054 | |
| 5055 | switch (type) { |
| 5056 | case HTX_BLK_UNUSED: |
| 5057 | break; |
| 5058 | |
| 5059 | case HTX_BLK_DATA: |
| 5060 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5061 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5062 | break; |
| 5063 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5064 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5065 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5066 | len = 0; |
| 5067 | break; |
| 5068 | |
| 5069 | default: |
| 5070 | break; |
| 5071 | } |
| 5072 | |
Willy Tarreau | 8424004 | 2022-02-28 16:51:23 +0100 | [diff] [blame] | 5073 | c_rew(req, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5074 | count -= vlen; |
| 5075 | if (len > 0) |
| 5076 | len -= vlen; |
| 5077 | if (sz == vlen) |
| 5078 | blk = htx_remove_blk(htx, blk); |
| 5079 | else { |
| 5080 | htx_cut_data_blk(htx, blk, vlen); |
| 5081 | break; |
| 5082 | } |
| 5083 | } |
| 5084 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5085 | /* The message was fully consumed and no more data are expected |
| 5086 | * (EOM flag set). |
| 5087 | */ |
| 5088 | if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) |
| 5089 | len = 0; |
| 5090 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5091 | htx_to_buf(htx, &req->buf); |
| 5092 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5093 | /* If we are no other data available, yield waiting for new data. */ |
| 5094 | if (len) { |
| 5095 | if (len > 0) { |
| 5096 | lua_pushinteger(L, len); |
| 5097 | lua_replace(L, 2); |
| 5098 | } |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 5099 | cs_cant_get(cs); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5100 | 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] | 5101 | } |
| 5102 | |
| 5103 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5104 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5105 | return 1; |
| 5106 | } |
| 5107 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5108 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5109 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 5110 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5111 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5112 | int len = -1; |
| 5113 | |
| 5114 | /* Check arguments. */ |
| 5115 | if (lua_gettop(L) > 2) |
| 5116 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 5117 | if (lua_gettop(L) >= 2) { |
| 5118 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5119 | lua_pop(L, 1); |
| 5120 | } |
| 5121 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5122 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5123 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5124 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5125 | luaL_buffinit(L, &luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5126 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5127 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5128 | } |
| 5129 | |
| 5130 | /* Append data in the output side of the buffer. This data is immediately |
| 5131 | * sent. The function returns the amount of data written. If the buffer |
| 5132 | * cannot contain the data, the function yields. The function returns -1 |
| 5133 | * if the channel is closed. |
| 5134 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5135 | __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] | 5136 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5137 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 5138 | struct conn_stream *cs = luactx->appctx->owner; |
| 5139 | struct channel *res = cs_ic(cs); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5140 | struct htx *htx = htx_from_buf(&res->buf); |
| 5141 | const char *data; |
| 5142 | size_t len; |
| 5143 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 5144 | int max; |
| 5145 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 5146 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5147 | if (!max) |
| 5148 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5149 | |
| 5150 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5151 | |
| 5152 | /* Get the max amount of data which can write as input in the channel. */ |
| 5153 | if (max > (len - l)) |
| 5154 | max = len - l; |
| 5155 | |
| 5156 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 5157 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5158 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5159 | |
| 5160 | /* update counters. */ |
| 5161 | l += max; |
| 5162 | lua_pop(L, 1); |
| 5163 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5164 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5165 | /* If some data is not send, declares the situation to the |
| 5166 | * applet, and returns a yield. |
| 5167 | */ |
| 5168 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5169 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5170 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 5171 | cs_rx_room_blk(cs); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5172 | 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] | 5173 | } |
| 5174 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5175 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5176 | return 1; |
| 5177 | } |
| 5178 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5179 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5180 | * yield the LUA process, and resume it without checking the |
| 5181 | * input arguments. |
| 5182 | */ |
| 5183 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 5184 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5185 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5186 | |
| 5187 | /* We want to send some data. Headers must be sent. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5188 | if (!(luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5189 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 5190 | WILL_LJMP(lua_error(L)); |
| 5191 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5192 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5193 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5194 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5195 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5196 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5197 | } |
| 5198 | |
| 5199 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 5200 | { |
| 5201 | const char *name; |
| 5202 | int ret; |
| 5203 | |
| 5204 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5205 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5206 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 5207 | |
| 5208 | /* Push in the stack the "response" entry. */ |
| 5209 | ret = lua_getfield(L, 1, "response"); |
| 5210 | if (ret != LUA_TTABLE) { |
| 5211 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 5212 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 5213 | WILL_LJMP(lua_error(L)); |
| 5214 | } |
| 5215 | |
| 5216 | /* check if the header is already registered if it is not |
| 5217 | * the case, register it. |
| 5218 | */ |
| 5219 | ret = lua_getfield(L, -1, name); |
| 5220 | if (ret == LUA_TNIL) { |
| 5221 | |
| 5222 | /* Entry not found. */ |
| 5223 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 5224 | |
| 5225 | /* Insert the new header name in the array in the top of the stack. |
| 5226 | * It left the new array in the top of the stack. |
| 5227 | */ |
| 5228 | lua_newtable(L); |
| 5229 | lua_pushvalue(L, 2); |
| 5230 | lua_pushvalue(L, -2); |
| 5231 | lua_settable(L, -4); |
| 5232 | |
| 5233 | } else if (ret != LUA_TTABLE) { |
| 5234 | |
| 5235 | /* corruption error. */ |
| 5236 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 5237 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 5238 | WILL_LJMP(lua_error(L)); |
| 5239 | } |
| 5240 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5241 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5242 | * the header value as new entry. |
| 5243 | */ |
| 5244 | lua_pushvalue(L, 3); |
| 5245 | ret = lua_rawlen(L, -2); |
| 5246 | lua_rawseti(L, -2, ret + 1); |
| 5247 | lua_pushboolean(L, 1); |
| 5248 | return 1; |
| 5249 | } |
| 5250 | |
| 5251 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 5252 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5253 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5254 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5255 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5256 | |
| 5257 | if (status < 100 || status > 599) { |
| 5258 | lua_pushboolean(L, 0); |
| 5259 | return 1; |
| 5260 | } |
| 5261 | |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5262 | luactx->appctx->ctx.hlua_apphttp.status = status; |
| 5263 | luactx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5264 | lua_pushboolean(L, 1); |
| 5265 | return 1; |
| 5266 | } |
| 5267 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5268 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5269 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5270 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5271 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 5272 | struct conn_stream *cs = luactx->appctx->owner; |
| 5273 | struct channel *res = cs_ic(cs); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5274 | struct htx *htx; |
| 5275 | struct htx_sl *sl; |
| 5276 | struct h1m h1m; |
| 5277 | const char *status, *reason; |
| 5278 | const char *name, *value; |
| 5279 | size_t nlen, vlen; |
| 5280 | unsigned int flags; |
| 5281 | |
| 5282 | /* Send the message at once. */ |
| 5283 | htx = htx_from_buf(&res->buf); |
| 5284 | h1m_init_res(&h1m); |
| 5285 | |
| 5286 | /* Use the same http version than the request. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5287 | status = ultoa_r(luactx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 5288 | reason = luactx->appctx->ctx.hlua_apphttp.reason; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5289 | if (reason == NULL) |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5290 | reason = http_get_reason(luactx->appctx->ctx.hlua_apphttp.status); |
| 5291 | if (luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5292 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5293 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 5294 | } |
| 5295 | else { |
| 5296 | flags = HTX_SL_F_IS_RESP; |
| 5297 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 5298 | } |
| 5299 | if (!sl) { |
| 5300 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5301 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5302 | WILL_LJMP(lua_error(L)); |
| 5303 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5304 | sl->info.res.status = luactx->appctx->ctx.hlua_apphttp.status; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5305 | |
| 5306 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 5307 | lua_pushvalue(L, 0); |
| 5308 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 5309 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5310 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5311 | WILL_LJMP(lua_error(L)); |
| 5312 | } |
| 5313 | |
| 5314 | /* Browse the list of headers. */ |
| 5315 | lua_pushnil(L); |
| 5316 | while(lua_next(L, -2) != 0) { |
| 5317 | /* We expect a string as -2. */ |
| 5318 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 5319 | 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] | 5320 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5321 | lua_typename(L, lua_type(L, -2))); |
| 5322 | WILL_LJMP(lua_error(L)); |
| 5323 | } |
| 5324 | name = lua_tolstring(L, -2, &nlen); |
| 5325 | |
| 5326 | /* We expect an array as -1. */ |
| 5327 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 5328 | 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] | 5329 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5330 | name, |
| 5331 | lua_typename(L, lua_type(L, -1))); |
| 5332 | WILL_LJMP(lua_error(L)); |
| 5333 | } |
| 5334 | |
| 5335 | /* Browse the table who is on the top of the stack. */ |
| 5336 | lua_pushnil(L); |
| 5337 | while(lua_next(L, -2) != 0) { |
| 5338 | int id; |
| 5339 | |
| 5340 | /* We expect a number as -2. */ |
| 5341 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 5342 | 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] | 5343 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5344 | name, |
| 5345 | lua_typename(L, lua_type(L, -2))); |
| 5346 | WILL_LJMP(lua_error(L)); |
| 5347 | } |
| 5348 | id = lua_tointeger(L, -2); |
| 5349 | |
| 5350 | /* We expect a string as -2. */ |
| 5351 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 5352 | 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] | 5353 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5354 | name, id, |
| 5355 | lua_typename(L, lua_type(L, -1))); |
| 5356 | WILL_LJMP(lua_error(L)); |
| 5357 | } |
| 5358 | value = lua_tolstring(L, -1, &vlen); |
| 5359 | |
| 5360 | /* Simple Protocol checks. */ |
Christopher Faulet | 545fbba | 2021-09-28 09:36:25 +0200 | [diff] [blame] | 5361 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) { |
| 5362 | int ret; |
| 5363 | |
| 5364 | ret = h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
| 5365 | if (ret < 0) { |
| 5366 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 5367 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
| 5368 | name); |
| 5369 | WILL_LJMP(lua_error(L)); |
| 5370 | } |
| 5371 | else if (ret == 0) |
| 5372 | goto next; /* Skip it */ |
| 5373 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5374 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 5375 | struct ist v = ist2(value, vlen); |
| 5376 | int ret; |
| 5377 | |
| 5378 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 5379 | if (ret < 0) { |
| 5380 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5381 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5382 | name); |
| 5383 | WILL_LJMP(lua_error(L)); |
| 5384 | } |
| 5385 | else if (ret == 0) |
| 5386 | goto next; /* Skip it */ |
| 5387 | } |
| 5388 | |
| 5389 | /* Add a new header */ |
| 5390 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 5391 | 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] | 5392 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5393 | name); |
| 5394 | WILL_LJMP(lua_error(L)); |
| 5395 | } |
| 5396 | next: |
| 5397 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5398 | lua_pop(L, 1); |
| 5399 | } |
| 5400 | |
| 5401 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5402 | lua_pop(L, 1); |
| 5403 | } |
| 5404 | |
| 5405 | if (h1m.flags & H1_MF_CHNK) |
| 5406 | h1m.flags &= ~H1_MF_CLEN; |
| 5407 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5408 | h1m.flags |= H1_MF_XFER_LEN; |
| 5409 | |
| 5410 | /* Uset HTX start-line flags */ |
| 5411 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5412 | flags |= HTX_SL_F_XFER_ENC; |
| 5413 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5414 | flags |= HTX_SL_F_XFER_LEN; |
| 5415 | if (h1m.flags & H1_MF_CHNK) |
| 5416 | flags |= HTX_SL_F_CHNK; |
| 5417 | else if (h1m.flags & H1_MF_CLEN) |
| 5418 | flags |= HTX_SL_F_CLEN; |
| 5419 | if (h1m.body_len == 0) |
| 5420 | flags |= HTX_SL_F_BODYLESS; |
| 5421 | } |
| 5422 | sl->flags |= flags; |
| 5423 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5424 | /* 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] | 5425 | * and the status code implies the presence of a message body, we must |
| 5426 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5427 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 5428 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5429 | */ |
| 5430 | 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] | 5431 | luactx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 5432 | luactx->appctx->ctx.hlua_apphttp.status != 204 && |
| 5433 | luactx->appctx->ctx.hlua_apphttp.status != 304) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5434 | /* Add a new header */ |
| 5435 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 5436 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 5437 | 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] | 5438 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5439 | WILL_LJMP(lua_error(L)); |
| 5440 | } |
| 5441 | } |
| 5442 | |
| 5443 | /* Finalize headers. */ |
| 5444 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 5445 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5446 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5447 | WILL_LJMP(lua_error(L)); |
| 5448 | } |
| 5449 | |
| 5450 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 5451 | b_reset(&res->buf); |
| 5452 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 5453 | WILL_LJMP(lua_error(L)); |
| 5454 | } |
| 5455 | |
| 5456 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5457 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5458 | |
| 5459 | /* Headers sent, set the flag. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5460 | luactx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5461 | return 0; |
| 5462 | |
| 5463 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5464 | /* We will build the status line and the headers of the HTTP response. |
| 5465 | * We will try send at once if its not possible, we give back the hand |
| 5466 | * waiting for more room. |
| 5467 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5468 | __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] | 5469 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5470 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 5471 | struct conn_stream *cs = luactx->appctx->owner; |
| 5472 | struct channel *res = cs_ic(cs); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5473 | |
| 5474 | if (co_data(res)) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 5475 | cs_rx_room_blk(cs); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5476 | 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] | 5477 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5478 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5479 | } |
| 5480 | |
| 5481 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5482 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5483 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5484 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5485 | } |
| 5486 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5487 | /* |
| 5488 | * |
| 5489 | * |
| 5490 | * Class HTTP |
| 5491 | * |
| 5492 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5493 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5494 | |
| 5495 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5496 | * a class stream, otherwise it throws an error. |
| 5497 | */ |
| 5498 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5499 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5500 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 5501 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5502 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5503 | /* This function creates and push in the stack a HTTP object |
| 5504 | * according with a current TXN. |
| 5505 | */ |
| 5506 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5507 | { |
| 5508 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5509 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5510 | /* Check stack size. */ |
| 5511 | if (!lua_checkstack(L, 3)) |
| 5512 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5513 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5514 | /* Create the object: obj[0] = userdata. |
| 5515 | * Note that the base of the Converters object is the |
| 5516 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5517 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5518 | lua_newtable(L); |
| 5519 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5520 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5521 | |
| 5522 | htxn->s = txn->s; |
| 5523 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 5524 | htxn->dir = txn->dir; |
| 5525 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5526 | |
| 5527 | /* Pop a class stream metatable and affect it to the table. */ |
| 5528 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5529 | lua_setmetatable(L, -2); |
| 5530 | |
| 5531 | return 1; |
| 5532 | } |
| 5533 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5534 | /* This function creates and returns an array containing the status-line |
| 5535 | * elements. This function does not fails. |
| 5536 | */ |
| 5537 | __LJMP static int hlua_http_get_stline(lua_State *L, struct htx_sl *sl) |
| 5538 | { |
| 5539 | /* Create the table. */ |
| 5540 | lua_newtable(L); |
| 5541 | |
| 5542 | if (sl->flags & HTX_SL_F_IS_RESP) { |
| 5543 | lua_pushstring(L, "version"); |
| 5544 | lua_pushlstring(L, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); |
| 5545 | lua_settable(L, -3); |
| 5546 | lua_pushstring(L, "code"); |
| 5547 | lua_pushlstring(L, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); |
| 5548 | lua_settable(L, -3); |
| 5549 | lua_pushstring(L, "reason"); |
| 5550 | lua_pushlstring(L, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); |
| 5551 | lua_settable(L, -3); |
| 5552 | } |
| 5553 | else { |
| 5554 | lua_pushstring(L, "method"); |
| 5555 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 5556 | lua_settable(L, -3); |
| 5557 | lua_pushstring(L, "uri"); |
| 5558 | lua_pushlstring(L, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); |
| 5559 | lua_settable(L, -3); |
| 5560 | lua_pushstring(L, "version"); |
| 5561 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 5562 | lua_settable(L, -3); |
| 5563 | } |
| 5564 | return 1; |
| 5565 | } |
| 5566 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5567 | /* This function creates ans returns an array of HTTP headers. |
| 5568 | * This function does not fails. It is used as wrapper with the |
| 5569 | * 2 following functions. |
| 5570 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5571 | __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] | 5572 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5573 | struct htx *htx; |
| 5574 | int32_t pos; |
| 5575 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5576 | /* Create the table. */ |
| 5577 | lua_newtable(L); |
| 5578 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5579 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5580 | htx = htxbuf(&msg->chn->buf); |
| 5581 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5582 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5583 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5584 | struct ist n, v; |
| 5585 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5586 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5587 | if (type == HTX_BLK_HDR) { |
| 5588 | n = htx_get_blk_name(htx,blk); |
| 5589 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5590 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5591 | else if (type == HTX_BLK_EOH) |
| 5592 | break; |
| 5593 | else |
| 5594 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5595 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5596 | /* Check for existing entry: |
| 5597 | * assume that the table is on the top of the stack, and |
| 5598 | * push the key in the stack, the function lua_gettable() |
| 5599 | * perform the lookup. |
| 5600 | */ |
| 5601 | lua_pushlstring(L, n.ptr, n.len); |
| 5602 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5603 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5604 | switch (lua_type(L, -1)) { |
| 5605 | case LUA_TNIL: |
| 5606 | /* Table not found, create it. */ |
| 5607 | lua_pop(L, 1); /* remove the nil value. */ |
| 5608 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5609 | lua_newtable(L); /* create and push empty table. */ |
| 5610 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5611 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5612 | 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] | 5613 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5614 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5615 | case LUA_TTABLE: |
| 5616 | /* Entry found: push the value in the table. */ |
| 5617 | len = lua_rawlen(L, -1); |
| 5618 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5619 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5620 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5621 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5622 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5623 | default: |
| 5624 | /* Other cases are errors. */ |
| 5625 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5626 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5627 | } |
| 5628 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5629 | return 1; |
| 5630 | } |
| 5631 | |
| 5632 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5633 | { |
| 5634 | struct hlua_txn *htxn; |
| 5635 | |
| 5636 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5637 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5638 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5639 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5640 | WILL_LJMP(lua_error(L)); |
| 5641 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5642 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5643 | } |
| 5644 | |
| 5645 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5646 | { |
| 5647 | struct hlua_txn *htxn; |
| 5648 | |
| 5649 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5650 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5651 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5652 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5653 | WILL_LJMP(lua_error(L)); |
| 5654 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5655 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5656 | } |
| 5657 | |
| 5658 | /* This function replace full header, or just a value in |
| 5659 | * the request or in the response. It is a wrapper fir the |
| 5660 | * 4 following functions. |
| 5661 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5662 | __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] | 5663 | { |
| 5664 | size_t name_len; |
| 5665 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5666 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5667 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5668 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5669 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5670 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5671 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5672 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5673 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5674 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5675 | 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] | 5676 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5677 | return 0; |
| 5678 | } |
| 5679 | |
| 5680 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5681 | { |
| 5682 | struct hlua_txn *htxn; |
| 5683 | |
| 5684 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5685 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5686 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5687 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5688 | WILL_LJMP(lua_error(L)); |
| 5689 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5690 | 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] | 5691 | } |
| 5692 | |
| 5693 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5694 | { |
| 5695 | struct hlua_txn *htxn; |
| 5696 | |
| 5697 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5698 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5699 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5700 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5701 | WILL_LJMP(lua_error(L)); |
| 5702 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5703 | 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] | 5704 | } |
| 5705 | |
| 5706 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5707 | { |
| 5708 | struct hlua_txn *htxn; |
| 5709 | |
| 5710 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5711 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5712 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5713 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5714 | WILL_LJMP(lua_error(L)); |
| 5715 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5716 | 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] | 5717 | } |
| 5718 | |
| 5719 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5720 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5721 | struct hlua_txn *htxn; |
| 5722 | |
| 5723 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5724 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5725 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5726 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5727 | WILL_LJMP(lua_error(L)); |
| 5728 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5729 | 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] | 5730 | } |
| 5731 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5732 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5733 | * It is a wrapper for the 2 following functions. |
| 5734 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5735 | __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] | 5736 | { |
| 5737 | size_t len; |
| 5738 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5739 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5740 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5741 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5742 | ctx.blk = NULL; |
| 5743 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5744 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5745 | return 0; |
| 5746 | } |
| 5747 | |
| 5748 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5749 | { |
| 5750 | struct hlua_txn *htxn; |
| 5751 | |
| 5752 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5753 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5754 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5755 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5756 | WILL_LJMP(lua_error(L)); |
| 5757 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5758 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5759 | } |
| 5760 | |
| 5761 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5762 | { |
| 5763 | struct hlua_txn *htxn; |
| 5764 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5765 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5766 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5767 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5768 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5769 | WILL_LJMP(lua_error(L)); |
| 5770 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5771 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5772 | } |
| 5773 | |
| 5774 | /* This function adds an header. It is a wrapper used by |
| 5775 | * the 2 following functions. |
| 5776 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5777 | __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] | 5778 | { |
| 5779 | size_t name_len; |
| 5780 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5781 | size_t value_len; |
| 5782 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5783 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5784 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5785 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5786 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5787 | return 0; |
| 5788 | } |
| 5789 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5790 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5791 | { |
| 5792 | struct hlua_txn *htxn; |
| 5793 | |
| 5794 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5795 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5796 | |
| 5797 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5798 | WILL_LJMP(lua_error(L)); |
| 5799 | |
| 5800 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 5801 | } |
| 5802 | |
| 5803 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5804 | { |
| 5805 | struct hlua_txn *htxn; |
| 5806 | |
| 5807 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5808 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5809 | |
| 5810 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5811 | WILL_LJMP(lua_error(L)); |
| 5812 | |
| 5813 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 5814 | } |
| 5815 | |
| 5816 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5817 | { |
| 5818 | struct hlua_txn *htxn; |
| 5819 | |
| 5820 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5821 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5822 | |
| 5823 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5824 | WILL_LJMP(lua_error(L)); |
| 5825 | |
| 5826 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5827 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 5828 | } |
| 5829 | |
| 5830 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5831 | { |
| 5832 | struct hlua_txn *htxn; |
| 5833 | |
| 5834 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5835 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5836 | |
| 5837 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5838 | WILL_LJMP(lua_error(L)); |
| 5839 | |
| 5840 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5841 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 5842 | } |
| 5843 | |
| 5844 | /* This function set the method. */ |
| 5845 | static int hlua_http_req_set_meth(lua_State *L) |
| 5846 | { |
| 5847 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5848 | size_t name_len; |
| 5849 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5850 | |
| 5851 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5852 | WILL_LJMP(lua_error(L)); |
| 5853 | |
| 5854 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1); |
| 5855 | return 1; |
| 5856 | } |
| 5857 | |
| 5858 | /* This function set the method. */ |
| 5859 | static int hlua_http_req_set_path(lua_State *L) |
| 5860 | { |
| 5861 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5862 | size_t name_len; |
| 5863 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5864 | |
| 5865 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5866 | WILL_LJMP(lua_error(L)); |
| 5867 | |
| 5868 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1); |
| 5869 | return 1; |
| 5870 | } |
| 5871 | |
| 5872 | /* This function set the query-string. */ |
| 5873 | static int hlua_http_req_set_query(lua_State *L) |
| 5874 | { |
| 5875 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5876 | size_t name_len; |
| 5877 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5878 | |
| 5879 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5880 | WILL_LJMP(lua_error(L)); |
| 5881 | |
| 5882 | /* Check length. */ |
| 5883 | if (name_len > trash.size - 1) { |
| 5884 | lua_pushboolean(L, 0); |
| 5885 | return 1; |
| 5886 | } |
| 5887 | |
| 5888 | /* Add the mark question as prefix. */ |
| 5889 | chunk_reset(&trash); |
| 5890 | trash.area[trash.data++] = '?'; |
| 5891 | memcpy(trash.area + trash.data, name, name_len); |
| 5892 | trash.data += name_len; |
| 5893 | |
| 5894 | lua_pushboolean(L, |
| 5895 | http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1); |
| 5896 | return 1; |
| 5897 | } |
| 5898 | |
| 5899 | /* This function set the uri. */ |
| 5900 | static int hlua_http_req_set_uri(lua_State *L) |
| 5901 | { |
| 5902 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5903 | size_t name_len; |
| 5904 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5905 | |
| 5906 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5907 | WILL_LJMP(lua_error(L)); |
| 5908 | |
| 5909 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1); |
| 5910 | return 1; |
| 5911 | } |
| 5912 | |
| 5913 | /* This function set the response code & optionally reason. */ |
| 5914 | static int hlua_http_res_set_status(lua_State *L) |
| 5915 | { |
| 5916 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5917 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5918 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5919 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
| 5920 | |
| 5921 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5922 | WILL_LJMP(lua_error(L)); |
| 5923 | |
| 5924 | http_res_set_status(code, reason, htxn->s); |
| 5925 | return 0; |
| 5926 | } |
| 5927 | |
| 5928 | /* |
| 5929 | * |
| 5930 | * |
| 5931 | * Class HTTPMessage |
| 5932 | * |
| 5933 | * |
| 5934 | */ |
| 5935 | |
| 5936 | /* Returns a struct http_msg if the stack entry "ud" is a class HTTPMessage, |
| 5937 | * otherwise it throws an error. |
| 5938 | */ |
| 5939 | __LJMP static struct http_msg *hlua_checkhttpmsg(lua_State *L, int ud) |
| 5940 | { |
| 5941 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_msg_ref)); |
| 5942 | } |
| 5943 | |
| 5944 | /* Creates and pushes on the stack a HTTP object according with a current TXN. |
| 5945 | */ |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 5946 | 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] | 5947 | { |
| 5948 | /* Check stack size. */ |
| 5949 | if (!lua_checkstack(L, 3)) |
| 5950 | return 0; |
| 5951 | |
| 5952 | lua_newtable(L); |
| 5953 | lua_pushlightuserdata(L, msg); |
| 5954 | lua_rawseti(L, -2, 0); |
| 5955 | |
| 5956 | /* Create the "channel" field that contains the request channel object. */ |
| 5957 | lua_pushstring(L, "channel"); |
| 5958 | if (!hlua_channel_new(L, msg->chn)) |
| 5959 | return 0; |
| 5960 | lua_rawset(L, -3); |
| 5961 | |
| 5962 | /* Pop a class stream metatable and affect it to the table. */ |
| 5963 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_msg_ref); |
| 5964 | lua_setmetatable(L, -2); |
| 5965 | |
| 5966 | return 1; |
| 5967 | } |
| 5968 | |
| 5969 | /* Helper function returning a filter attached to the HTTP message at the |
| 5970 | * 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] | 5971 | * 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] | 5972 | * filled with output and input length respectively. |
| 5973 | */ |
| 5974 | static struct filter *hlua_http_msg_filter(lua_State *L, int ud, struct http_msg *msg, size_t *offset, size_t *len) |
| 5975 | { |
| 5976 | struct channel *chn = msg->chn; |
| 5977 | struct htx *htx = htxbuf(&chn->buf); |
| 5978 | struct filter *filter = NULL; |
| 5979 | |
| 5980 | *offset = co_data(msg->chn); |
| 5981 | *len = htx->data - co_data(msg->chn); |
| 5982 | |
| 5983 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 5984 | filter = lua_touserdata (L, -1); |
| 5985 | if (msg->msg_state >= HTTP_MSG_DATA) { |
| 5986 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 5987 | |
| 5988 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 5989 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 5990 | } |
| 5991 | } |
| 5992 | |
| 5993 | lua_pop(L, 1); |
| 5994 | return filter; |
| 5995 | } |
| 5996 | |
| 5997 | /* Returns true if the channel attached to the HTTP message is the response |
| 5998 | * channel. |
| 5999 | */ |
| 6000 | __LJMP static int hlua_http_msg_is_resp(lua_State *L) |
| 6001 | { |
| 6002 | struct http_msg *msg; |
| 6003 | |
| 6004 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 6005 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6006 | |
| 6007 | lua_pushboolean(L, !!(msg->chn->flags & CF_ISRESP)); |
| 6008 | return 1; |
| 6009 | } |
| 6010 | |
| 6011 | /* Returns an array containing the elements status-line of the HTTP message. It relies |
| 6012 | * on hlua_http_get_stline(). |
| 6013 | */ |
| 6014 | __LJMP static int hlua_http_msg_get_stline(lua_State *L) |
| 6015 | { |
| 6016 | struct http_msg *msg; |
| 6017 | struct htx *htx; |
| 6018 | struct htx_sl *sl; |
| 6019 | |
| 6020 | MAY_LJMP(check_args(L, 1, "get_stline")); |
| 6021 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6022 | |
| 6023 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6024 | WILL_LJMP(lua_error(L)); |
| 6025 | |
| 6026 | htx = htxbuf(&msg->chn->buf); |
| 6027 | sl = http_get_stline(htx); |
| 6028 | if (!sl) |
| 6029 | return 0; |
| 6030 | return hlua_http_get_stline(L, sl); |
| 6031 | } |
| 6032 | |
| 6033 | /* Returns an array containing all headers of the HTTP message. it relies on |
| 6034 | * hlua_http_get_headers(). |
| 6035 | */ |
| 6036 | __LJMP static int hlua_http_msg_get_headers(lua_State *L) |
| 6037 | { |
| 6038 | struct http_msg *msg; |
| 6039 | |
| 6040 | MAY_LJMP(check_args(L, 1, "get_headers")); |
| 6041 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6042 | |
| 6043 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6044 | WILL_LJMP(lua_error(L)); |
| 6045 | |
| 6046 | return hlua_http_get_headers(L, msg); |
| 6047 | } |
| 6048 | |
| 6049 | /* Deletes all occurrences of an header in the HTTP message matching on its |
| 6050 | * name. It relies on hlua_http_del_hdr(). |
| 6051 | */ |
| 6052 | __LJMP static int hlua_http_msg_del_hdr(lua_State *L) |
| 6053 | { |
| 6054 | struct http_msg *msg; |
| 6055 | |
| 6056 | MAY_LJMP(check_args(L, 2, "del_header")); |
| 6057 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6058 | |
| 6059 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6060 | WILL_LJMP(lua_error(L)); |
| 6061 | |
| 6062 | return hlua_http_del_hdr(L, msg); |
| 6063 | } |
| 6064 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6065 | /* 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] | 6066 | * message given its name against a regex and replaces it if it matches. It |
| 6067 | * relies on hlua_http_rep_hdr(). |
| 6068 | */ |
| 6069 | __LJMP static int hlua_http_msg_rep_hdr(lua_State *L) |
| 6070 | { |
| 6071 | struct http_msg *msg; |
| 6072 | |
| 6073 | MAY_LJMP(check_args(L, 4, "rep_header")); |
| 6074 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6075 | |
| 6076 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6077 | WILL_LJMP(lua_error(L)); |
| 6078 | |
| 6079 | return hlua_http_rep_hdr(L, msg, 1); |
| 6080 | } |
| 6081 | |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 6082 | /* 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] | 6083 | * message given its name against a regex and replaces it if it matches. It |
| 6084 | * relies on hlua_http_rep_hdr(). |
| 6085 | */ |
| 6086 | __LJMP static int hlua_http_msg_rep_val(lua_State *L) |
| 6087 | { |
| 6088 | struct http_msg *msg; |
| 6089 | |
| 6090 | MAY_LJMP(check_args(L, 4, "rep_value")); |
| 6091 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6092 | |
| 6093 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6094 | WILL_LJMP(lua_error(L)); |
| 6095 | |
| 6096 | return hlua_http_rep_hdr(L, msg, 0); |
| 6097 | } |
| 6098 | |
| 6099 | /* Add an header in the HTTP message. It relies on hlua_http_add_hdr() */ |
| 6100 | __LJMP static int hlua_http_msg_add_hdr(lua_State *L) |
| 6101 | { |
| 6102 | struct http_msg *msg; |
| 6103 | |
| 6104 | MAY_LJMP(check_args(L, 3, "add_header")); |
| 6105 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6106 | |
| 6107 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6108 | WILL_LJMP(lua_error(L)); |
| 6109 | |
| 6110 | return hlua_http_add_hdr(L, msg); |
| 6111 | } |
| 6112 | |
| 6113 | /* Add an header in the HTTP message removing existing headers with the same |
| 6114 | * name. It relies on hlua_http_del_hdr() and hlua_http_add_hdr(). |
| 6115 | */ |
| 6116 | __LJMP static int hlua_http_msg_set_hdr(lua_State *L) |
| 6117 | { |
| 6118 | struct http_msg *msg; |
| 6119 | |
| 6120 | MAY_LJMP(check_args(L, 3, "set_header")); |
| 6121 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6122 | |
| 6123 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6124 | WILL_LJMP(lua_error(L)); |
| 6125 | |
| 6126 | hlua_http_del_hdr(L, msg); |
| 6127 | return hlua_http_add_hdr(L, msg); |
| 6128 | } |
| 6129 | |
| 6130 | /* Rewrites the request method. It relies on http_req_replace_stline(). */ |
| 6131 | __LJMP static int hlua_http_msg_set_meth(lua_State *L) |
| 6132 | { |
| 6133 | struct stream *s; |
| 6134 | struct http_msg *msg; |
| 6135 | const char *name; |
| 6136 | size_t name_len; |
| 6137 | |
| 6138 | MAY_LJMP(check_args(L, 2, "set_method")); |
| 6139 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6140 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6141 | |
| 6142 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6143 | WILL_LJMP(lua_error(L)); |
| 6144 | |
| 6145 | s = chn_strm(msg->chn); |
| 6146 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, s->be, s) != -1); |
| 6147 | return 1; |
| 6148 | } |
| 6149 | |
| 6150 | /* Rewrites the request path. It relies on http_req_replace_stline(). */ |
| 6151 | __LJMP static int hlua_http_msg_set_path(lua_State *L) |
| 6152 | { |
| 6153 | struct stream *s; |
| 6154 | struct http_msg *msg; |
| 6155 | const char *name; |
| 6156 | size_t name_len; |
| 6157 | |
| 6158 | MAY_LJMP(check_args(L, 2, "set_path")); |
| 6159 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6160 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6161 | |
| 6162 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6163 | WILL_LJMP(lua_error(L)); |
| 6164 | |
| 6165 | s = chn_strm(msg->chn); |
| 6166 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, s->be, s) != -1); |
| 6167 | return 1; |
| 6168 | } |
| 6169 | |
| 6170 | /* Rewrites the request query-string. It relies on http_req_replace_stline(). */ |
| 6171 | __LJMP static int hlua_http_msg_set_query(lua_State *L) |
| 6172 | { |
| 6173 | struct stream *s; |
| 6174 | struct http_msg *msg; |
| 6175 | const char *name; |
| 6176 | size_t name_len; |
| 6177 | |
| 6178 | MAY_LJMP(check_args(L, 2, "set_query")); |
| 6179 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6180 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6181 | |
| 6182 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6183 | WILL_LJMP(lua_error(L)); |
| 6184 | |
| 6185 | /* Check length. */ |
| 6186 | if (name_len > trash.size - 1) { |
| 6187 | lua_pushboolean(L, 0); |
| 6188 | return 1; |
| 6189 | } |
| 6190 | |
| 6191 | /* Add the mark question as prefix. */ |
| 6192 | chunk_reset(&trash); |
| 6193 | trash.area[trash.data++] = '?'; |
| 6194 | memcpy(trash.area + trash.data, name, name_len); |
| 6195 | trash.data += name_len; |
| 6196 | |
| 6197 | s = chn_strm(msg->chn); |
| 6198 | lua_pushboolean(L, http_req_replace_stline(2, trash.area, trash.data, s->be, s) != -1); |
| 6199 | return 1; |
| 6200 | } |
| 6201 | |
| 6202 | /* Rewrites the request URI. It relies on http_req_replace_stline(). */ |
| 6203 | __LJMP static int hlua_http_msg_set_uri(lua_State *L) |
| 6204 | { |
| 6205 | struct stream *s; |
| 6206 | struct http_msg *msg; |
| 6207 | const char *name; |
| 6208 | size_t name_len; |
| 6209 | |
| 6210 | MAY_LJMP(check_args(L, 2, "set_uri")); |
| 6211 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6212 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6213 | |
| 6214 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6215 | WILL_LJMP(lua_error(L)); |
| 6216 | |
| 6217 | s = chn_strm(msg->chn); |
| 6218 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, s->be, s) != -1); |
| 6219 | return 1; |
| 6220 | } |
| 6221 | |
| 6222 | /* Rewrites the response status code. It relies on http_res_set_status(). */ |
| 6223 | __LJMP static int hlua_http_msg_set_status(lua_State *L) |
| 6224 | { |
| 6225 | struct http_msg *msg; |
| 6226 | unsigned int code; |
| 6227 | const char *reason; |
| 6228 | size_t reason_len; |
| 6229 | |
| 6230 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6231 | code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6232 | reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, &reason_len)); |
| 6233 | |
| 6234 | if (!(msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6235 | WILL_LJMP(lua_error(L)); |
| 6236 | |
| 6237 | lua_pushboolean(L, http_res_set_status(code, ist2(reason, reason_len), chn_strm(msg->chn)) != -1); |
| 6238 | return 1; |
| 6239 | } |
| 6240 | |
| 6241 | /* Returns true if the HTTP message is full. */ |
| 6242 | __LJMP static int hlua_http_msg_is_full(lua_State *L) |
| 6243 | { |
| 6244 | struct http_msg *msg; |
| 6245 | |
| 6246 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 6247 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6248 | lua_pushboolean(L, channel_full(msg->chn, 0)); |
| 6249 | return 1; |
| 6250 | } |
| 6251 | |
| 6252 | /* Returns true if the HTTP message may still receive data. */ |
| 6253 | __LJMP static int hlua_http_msg_may_recv(lua_State *L) |
| 6254 | { |
| 6255 | struct http_msg *msg; |
| 6256 | struct htx *htx; |
| 6257 | |
| 6258 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6259 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6260 | htx = htxbuf(&msg->chn->buf); |
| 6261 | lua_pushboolean(L, (htx_expect_more(htx) && !channel_input_closed(msg->chn) && channel_may_recv(msg->chn))); |
| 6262 | return 1; |
| 6263 | } |
| 6264 | |
| 6265 | /* Returns true if the HTTP message EOM was received */ |
| 6266 | __LJMP static int hlua_http_msg_is_eom(lua_State *L) |
| 6267 | { |
| 6268 | struct http_msg *msg; |
| 6269 | struct htx *htx; |
| 6270 | |
| 6271 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6272 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6273 | htx = htxbuf(&msg->chn->buf); |
| 6274 | lua_pushboolean(L, !htx_expect_more(htx)); |
| 6275 | return 1; |
| 6276 | } |
| 6277 | |
| 6278 | /* Returns the number of bytes available in the input side of the HTTP |
| 6279 | * message. This function never fails. |
| 6280 | */ |
| 6281 | __LJMP static int hlua_http_msg_get_in_len(lua_State *L) |
| 6282 | { |
| 6283 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6284 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6285 | |
| 6286 | MAY_LJMP(check_args(L, 1, "input")); |
| 6287 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6288 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6289 | lua_pushinteger(L, input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6290 | return 1; |
| 6291 | } |
| 6292 | |
| 6293 | /* Returns the number of bytes available in the output side of the HTTP |
| 6294 | * message. This function never fails. |
| 6295 | */ |
| 6296 | __LJMP static int hlua_http_msg_get_out_len(lua_State *L) |
| 6297 | { |
| 6298 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6299 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6300 | |
| 6301 | MAY_LJMP(check_args(L, 1, "output")); |
| 6302 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6303 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6304 | lua_pushinteger(L, output); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6305 | return 1; |
| 6306 | } |
| 6307 | |
| 6308 | /* Copies at most <len> bytes of DATA blocks from the HTTP message <msg> |
| 6309 | * 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] | 6310 | * 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] | 6311 | * block. This function is called during the payload filtering, so the headers |
| 6312 | * are already scheduled for output (from the filter point of view). |
| 6313 | */ |
| 6314 | static int _hlua_http_msg_dup(struct http_msg *msg, lua_State *L, size_t offset, size_t len) |
| 6315 | { |
| 6316 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 6317 | struct htx_blk *blk; |
| 6318 | struct htx_ret htxret; |
| 6319 | luaL_Buffer b; |
| 6320 | int ret = 0; |
| 6321 | |
| 6322 | luaL_buffinit(L, &b); |
| 6323 | htxret = htx_find_offset(htx, offset); |
| 6324 | for (blk = htxret.blk, offset = htxret.ret; blk && len; blk = htx_get_next_blk(htx, blk)) { |
| 6325 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6326 | struct ist v; |
| 6327 | |
| 6328 | switch (type) { |
| 6329 | case HTX_BLK_UNUSED: |
| 6330 | break; |
| 6331 | |
| 6332 | case HTX_BLK_DATA: |
| 6333 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6334 | v = istadv(v, offset); |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6335 | v = isttrim(v, len); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6336 | |
| 6337 | luaL_addlstring(&b, v.ptr, v.len); |
| 6338 | ret += v.len; |
| 6339 | break; |
| 6340 | |
| 6341 | default: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6342 | if (!ret) |
| 6343 | goto no_data; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6344 | goto end; |
| 6345 | } |
| 6346 | offset = 0; |
| 6347 | } |
| 6348 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6349 | end: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6350 | if (!ret && (htx->flags & HTX_FL_EOM)) |
| 6351 | goto no_data; |
| 6352 | luaL_pushresult(&b); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6353 | return ret; |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6354 | |
| 6355 | no_data: |
| 6356 | /* Remove the empty string and push nil on the stack */ |
| 6357 | lua_pop(L, 1); |
| 6358 | lua_pushnil(L); |
| 6359 | return 0; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6360 | } |
| 6361 | |
| 6362 | /* Copies the string <str> to the HTTP message <msg> at the offset |
| 6363 | * <offset>. This function returns -1 if data cannot be copied. Otherwise, it |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6364 | * returns the amount of data written. This function is responsible to update |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6365 | * the filter context. |
| 6366 | */ |
| 6367 | static int _hlua_http_msg_insert(struct http_msg *msg, struct filter *filter, struct ist str, size_t offset) |
| 6368 | { |
| 6369 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6370 | struct htx_ret htxret; |
| 6371 | int /*max, */ret = 0; |
| 6372 | |
| 6373 | /* Nothing to do, just return */ |
| 6374 | if (unlikely(istlen(str) == 0)) |
| 6375 | goto end; |
| 6376 | |
| 6377 | if (istlen(str) > htx_free_data_space(htx)) { |
| 6378 | ret = -1; |
| 6379 | goto end; |
| 6380 | } |
| 6381 | |
| 6382 | htxret = htx_find_offset(htx, offset); |
| 6383 | if (!htxret.blk || htx_get_blk_type(htxret.blk) != HTX_BLK_DATA) { |
| 6384 | if (!htx_add_last_data(htx, str)) |
| 6385 | goto end; |
| 6386 | } |
| 6387 | else { |
| 6388 | struct ist v = htx_get_blk_value(htx, htxret.blk); |
| 6389 | v.ptr += htxret.ret; |
| 6390 | v.len = 0; |
| 6391 | if (!htx_replace_blk_value(htx, htxret.blk, v, str)) |
| 6392 | goto end; |
| 6393 | } |
| 6394 | ret = str.len; |
| 6395 | if (ret) { |
| 6396 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6397 | flt_update_offsets(filter, msg->chn, ret); |
| 6398 | flt_ctx->cur_len[CHN_IDX(msg->chn)] += ret; |
| 6399 | } |
| 6400 | |
| 6401 | end: |
| 6402 | htx_to_buf(htx, &msg->chn->buf); |
| 6403 | return ret; |
| 6404 | } |
| 6405 | |
| 6406 | /* Helper function removing at most <len> bytes of DATA blocks at the absolute |
| 6407 | * position <offset>. It stops on the first non-DATA HTX block. This function is |
| 6408 | * called during the payload filtering, so the headers are already scheduled for |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6409 | * output (from the filter point of view). This function is responsible to |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6410 | * update the filter context. |
| 6411 | */ |
| 6412 | static void _hlua_http_msg_delete(struct http_msg *msg, struct filter *filter, size_t offset, size_t len) |
| 6413 | { |
| 6414 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6415 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6416 | struct htx_blk *blk; |
| 6417 | struct htx_ret htxret; |
| 6418 | size_t ret = 0; |
| 6419 | |
| 6420 | /* Be sure <len> is always the amount of DATA to remove */ |
| 6421 | if (htx->data == offset+len && htx_get_tail_type(htx) == HTX_BLK_DATA) { |
| 6422 | htx_truncate(htx, offset); |
| 6423 | ret = len; |
| 6424 | goto end; |
| 6425 | } |
| 6426 | |
| 6427 | htxret = htx_find_offset(htx, offset); |
| 6428 | blk = htxret.blk; |
| 6429 | if (htxret.ret) { |
| 6430 | struct ist v; |
| 6431 | |
| 6432 | if (htx_get_blk_type(blk) != HTX_BLK_DATA) |
| 6433 | goto end; |
| 6434 | v = htx_get_blk_value(htx, blk); |
| 6435 | v.ptr += htxret.ret; |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6436 | v = isttrim(v, len); |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6437 | blk = htx_replace_blk_value(htx, blk, v, IST_NULL); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6438 | len -= v.len; |
| 6439 | ret += v.len; |
| 6440 | } |
| 6441 | |
| 6442 | |
| 6443 | while (blk && len) { |
| 6444 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6445 | uint32_t sz = htx_get_blksz(blk); |
| 6446 | |
| 6447 | switch (type) { |
| 6448 | case HTX_BLK_UNUSED: |
| 6449 | break; |
| 6450 | |
| 6451 | case HTX_BLK_DATA: |
| 6452 | if (len < sz) { |
| 6453 | htx_cut_data_blk(htx, blk, len); |
| 6454 | ret += len; |
| 6455 | goto end; |
| 6456 | } |
| 6457 | break; |
| 6458 | |
| 6459 | default: |
| 6460 | goto end; |
| 6461 | } |
| 6462 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6463 | /* Remove all the data block */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6464 | len -= sz; |
| 6465 | ret += sz; |
| 6466 | blk = htx_remove_blk(htx, blk); |
| 6467 | } |
| 6468 | |
| 6469 | end: |
| 6470 | flt_update_offsets(filter, msg->chn, -ret); |
| 6471 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6472 | /* WARNING: we don't call htx_to_buf() on purpose, because we don't want |
| 6473 | * to loose the EOM flag if the message is empty. |
| 6474 | */ |
| 6475 | } |
| 6476 | |
| 6477 | /* Copies input data found in an HTTP message. Unlike the channel function used |
| 6478 | * to duplicate raw data, this one can only be called inside a filter, from |
| 6479 | * http_payload callback. So it cannot yield. An exception is returned if it is |
| 6480 | * called from another callback. If nothing was copied, a nil value is pushed on |
| 6481 | * the stack. |
| 6482 | */ |
| 6483 | __LJMP static int hlua_http_msg_get_body(lua_State *L) |
| 6484 | { |
| 6485 | struct http_msg *msg; |
| 6486 | struct filter *filter; |
| 6487 | size_t output, input; |
| 6488 | int offset, len; |
| 6489 | |
| 6490 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 6491 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 6492 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6493 | |
| 6494 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6495 | WILL_LJMP(lua_error(L)); |
| 6496 | |
| 6497 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6498 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6499 | WILL_LJMP(lua_error(L)); |
| 6500 | |
| 6501 | if (!ci_data(msg->chn) && channel_input_closed(msg->chn)) { |
| 6502 | lua_pushnil(L); |
| 6503 | return 1; |
| 6504 | } |
| 6505 | |
| 6506 | offset = output; |
| 6507 | if (lua_gettop(L) > 1) { |
| 6508 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6509 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6510 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6511 | offset += output; |
| 6512 | if (offset < output || offset > input + output) { |
| 6513 | lua_pushfstring(L, "offset out of range."); |
| 6514 | WILL_LJMP(lua_error(L)); |
| 6515 | } |
| 6516 | } |
| 6517 | len = output + input - offset; |
| 6518 | if (lua_gettop(L) == 3) { |
| 6519 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6520 | if (!len) |
| 6521 | goto dup; |
| 6522 | if (len == -1) |
| 6523 | len = global.tune.bufsize; |
| 6524 | if (len < 0) { |
| 6525 | lua_pushfstring(L, "length out of range."); |
| 6526 | WILL_LJMP(lua_error(L)); |
| 6527 | } |
| 6528 | } |
| 6529 | |
| 6530 | dup: |
| 6531 | _hlua_http_msg_dup(msg, L, offset, len); |
| 6532 | return 1; |
| 6533 | } |
| 6534 | |
| 6535 | /* Appends a string to the HTTP message, after all existing DATA blocks but |
| 6536 | * before the trailers, if any. It returns the amount of data written or -1 if |
| 6537 | * nothing was copied. Unlike the channel function used to append data, this one |
| 6538 | * can only be called inside a filter, from http_payload callback. So it cannot |
| 6539 | * yield. An exception is returned if it is called from another callback. |
| 6540 | */ |
| 6541 | __LJMP static int hlua_http_msg_append(lua_State *L) |
| 6542 | { |
| 6543 | struct http_msg *msg; |
| 6544 | struct filter *filter; |
| 6545 | const char *str; |
| 6546 | size_t offset, len, sz; |
| 6547 | int ret; |
| 6548 | |
| 6549 | MAY_LJMP(check_args(L, 2, "append")); |
| 6550 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6551 | |
| 6552 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6553 | WILL_LJMP(lua_error(L)); |
| 6554 | |
| 6555 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6556 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6557 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6558 | WILL_LJMP(lua_error(L)); |
| 6559 | |
| 6560 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset+len); |
| 6561 | lua_pushinteger(L, ret); |
| 6562 | return 1; |
| 6563 | } |
| 6564 | |
| 6565 | /* Prepends a string to the HTTP message, before all existing DATA blocks. It |
| 6566 | * returns the amount of data written or -1 if nothing was copied. Unlike the |
| 6567 | * channel function used to prepend data, this one can only be called inside a |
| 6568 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6569 | * returned if it is called from another callback. |
| 6570 | */ |
| 6571 | __LJMP static int hlua_http_msg_prepend(lua_State *L) |
| 6572 | { |
| 6573 | struct http_msg *msg; |
| 6574 | struct filter *filter; |
| 6575 | const char *str; |
| 6576 | size_t offset, len, sz; |
| 6577 | int ret; |
| 6578 | |
| 6579 | MAY_LJMP(check_args(L, 2, "prepend")); |
| 6580 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6581 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6582 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6583 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6584 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6585 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6586 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6587 | if (!filter || !hlua_filter_from_payload(filter)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6588 | WILL_LJMP(lua_error(L)); |
| 6589 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6590 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6591 | lua_pushinteger(L, ret); |
| 6592 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6593 | } |
| 6594 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6595 | /* Inserts a string to the HTTP message at a given offset. By default the string |
| 6596 | * is appended at the end of DATA blocks. It returns the amount of data written |
| 6597 | * or -1 if nothing was copied. Unlike the channel function used to insert data, |
| 6598 | * this one can only be called inside a filter, from http_payload callback. So |
| 6599 | * it cannot yield. An exception is returned if it is called from another |
| 6600 | * callback. |
| 6601 | */ |
| 6602 | __LJMP static int hlua_http_msg_insert_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6603 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6604 | struct http_msg *msg; |
| 6605 | struct filter *filter; |
| 6606 | const char *str; |
| 6607 | size_t input, output, sz; |
| 6608 | int offset; |
| 6609 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6610 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6611 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 6612 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
| 6613 | MAY_LJMP(check_args(L, 2, "insert")); |
| 6614 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6615 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6616 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6617 | WILL_LJMP(lua_error(L)); |
| 6618 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6619 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6620 | filter = hlua_http_msg_filter(L, 1, msg, &input, &output); |
| 6621 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6622 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6623 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6624 | offset = input + output; |
| 6625 | if (lua_gettop(L) > 2) { |
| 6626 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6627 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6628 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6629 | offset += output; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6630 | if (offset < output || offset > output + input) { |
| 6631 | lua_pushfstring(L, "offset out of range."); |
| 6632 | WILL_LJMP(lua_error(L)); |
| 6633 | } |
| 6634 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6635 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6636 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6637 | lua_pushinteger(L, ret); |
| 6638 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6639 | } |
| 6640 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6641 | /* Removes a given amount of data from the HTTP message at a given offset. By |
| 6642 | * default all DATA blocks are removed. It returns the amount of data |
| 6643 | * removed. Unlike the channel function used to remove data, this one can only |
| 6644 | * be called inside a filter, from http_payload callback. So it cannot yield. An |
| 6645 | * exception is returned if it is called from another callback. |
| 6646 | */ |
| 6647 | __LJMP static int hlua_http_msg_del_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6648 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6649 | struct http_msg *msg; |
| 6650 | struct filter *filter; |
| 6651 | size_t input, output; |
| 6652 | int offset, len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6653 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6654 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 6655 | WILL_LJMP(luaL_error(L, "'insert' expects at most 2 arguments")); |
| 6656 | MAY_LJMP(check_args(L, 2, "insert")); |
| 6657 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6658 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6659 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6660 | WILL_LJMP(lua_error(L)); |
| 6661 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6662 | filter = hlua_http_msg_filter(L, 1, msg, &input, &output); |
| 6663 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6664 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6665 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6666 | offset = input + output; |
| 6667 | if (lua_gettop(L) > 2) { |
| 6668 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6669 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6670 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6671 | offset += output; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6672 | if (offset < output || offset > output + input) { |
| 6673 | lua_pushfstring(L, "offset out of range."); |
| 6674 | WILL_LJMP(lua_error(L)); |
| 6675 | } |
| 6676 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6677 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6678 | len = output + input - offset; |
| 6679 | if (lua_gettop(L) == 4) { |
| 6680 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6681 | if (!len) |
| 6682 | goto end; |
| 6683 | if (len == -1) |
| 6684 | len = output + input - offset; |
| 6685 | if (len < 0 || offset + len > output + input) { |
| 6686 | lua_pushfstring(L, "length out of range."); |
| 6687 | WILL_LJMP(lua_error(L)); |
| 6688 | } |
| 6689 | } |
| 6690 | |
| 6691 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6692 | |
| 6693 | end: |
| 6694 | lua_pushinteger(L, len); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6695 | return 1; |
| 6696 | } |
| 6697 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6698 | /* 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] | 6699 | * all remaining data are removed, accordingly to the filter context. It returns |
| 6700 | * the amount of data written or -1 if nothing was copied. Unlike the channel |
| 6701 | * function used to replace data, this one can only be called inside a filter, |
| 6702 | * from http_payload callback. So it cannot yield. An exception is returned if |
| 6703 | * it is called from another callback. |
| 6704 | */ |
| 6705 | __LJMP static int hlua_http_msg_set_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6706 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6707 | struct http_msg *msg; |
| 6708 | struct filter *filter; |
| 6709 | struct htx *htx; |
| 6710 | const char *str; |
| 6711 | size_t input, output, sz; |
| 6712 | int offset, len; |
| 6713 | int ret; |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 6714 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6715 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 6716 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 6717 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6718 | |
| 6719 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6720 | WILL_LJMP(lua_error(L)); |
| 6721 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6722 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6723 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6724 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6725 | WILL_LJMP(lua_error(L)); |
| 6726 | |
| 6727 | offset = output; |
| 6728 | if (lua_gettop(L) > 2) { |
| 6729 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6730 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6731 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6732 | offset += output; |
| 6733 | if (offset < output || offset > input + output) { |
| 6734 | lua_pushfstring(L, "offset out of range."); |
| 6735 | WILL_LJMP(lua_error(L)); |
| 6736 | } |
| 6737 | } |
| 6738 | |
| 6739 | len = output + input - offset; |
| 6740 | if (lua_gettop(L) == 4) { |
| 6741 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6742 | if (!len) |
| 6743 | goto set; |
| 6744 | if (len == -1) |
| 6745 | len = output + input - offset; |
| 6746 | if (len < 0 || offset + len > output + input) { |
| 6747 | lua_pushfstring(L, "length out of range."); |
| 6748 | WILL_LJMP(lua_error(L)); |
| 6749 | } |
| 6750 | } |
| 6751 | |
| 6752 | set: |
| 6753 | /* Be sure we can copied the string once input data will be removed. */ |
| 6754 | htx = htx_from_buf(&msg->chn->buf); |
| 6755 | if (sz > htx_free_data_space(htx) + len) |
| 6756 | lua_pushinteger(L, -1); |
| 6757 | else { |
| 6758 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6759 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6760 | lua_pushinteger(L, ret); |
| 6761 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6762 | return 1; |
| 6763 | } |
| 6764 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6765 | /* Prepends data into an HTTP message and forward it, from the filter point of |
| 6766 | * view. It returns the amount of data written or -1 if nothing was sent. Unlike |
| 6767 | * the channel function used to send data, this one can only be called inside a |
| 6768 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6769 | * returned if it is called from another callback. |
| 6770 | */ |
| 6771 | __LJMP static int hlua_http_msg_send(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6772 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6773 | struct http_msg *msg; |
| 6774 | struct filter *filter; |
| 6775 | struct htx *htx; |
| 6776 | const char *str; |
| 6777 | size_t offset, len, sz; |
| 6778 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6779 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6780 | MAY_LJMP(check_args(L, 2, "send")); |
| 6781 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6782 | |
| 6783 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6784 | WILL_LJMP(lua_error(L)); |
| 6785 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6786 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6787 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6788 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6789 | WILL_LJMP(lua_error(L)); |
| 6790 | |
| 6791 | /* Return an error if the channel's output is closed */ |
| 6792 | if (unlikely(channel_output_closed(msg->chn))) { |
| 6793 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6794 | return 1; |
| 6795 | } |
| 6796 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6797 | htx = htx_from_buf(&msg->chn->buf); |
| 6798 | if (sz > htx_free_data_space(htx)) { |
| 6799 | lua_pushinteger(L, -1); |
| 6800 | return 1; |
| 6801 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6802 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6803 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6804 | if (ret > 0) { |
| 6805 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6806 | |
| 6807 | FLT_OFF(filter, msg->chn) += ret; |
| 6808 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6809 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 6810 | } |
| 6811 | |
| 6812 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6813 | return 1; |
| 6814 | } |
| 6815 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6816 | /* Forwards a given amount of bytes. It return -1 if the channel's output is |
| 6817 | * closed. Otherwise, it returns the number of bytes forwarded. Unlike the |
| 6818 | * channel function used to forward data, this one can only be called inside a |
| 6819 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6820 | * returned if it is called from another callback. All other functions deal with |
| 6821 | * DATA block, this one not. |
| 6822 | */ |
| 6823 | __LJMP static int hlua_http_msg_forward(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6824 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6825 | struct http_msg *msg; |
| 6826 | struct filter *filter; |
| 6827 | size_t offset, len; |
| 6828 | int fwd, ret = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6829 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6830 | MAY_LJMP(check_args(L, 2, "forward")); |
| 6831 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6832 | |
| 6833 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6834 | WILL_LJMP(lua_error(L)); |
| 6835 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6836 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6837 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6838 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6839 | WILL_LJMP(lua_error(L)); |
| 6840 | |
| 6841 | /* Nothing to do, just return */ |
| 6842 | if (!fwd) |
| 6843 | goto end; |
| 6844 | |
| 6845 | /* Return an error if the channel's output is closed */ |
| 6846 | if (unlikely(channel_output_closed(msg->chn))) { |
| 6847 | ret = -1; |
| 6848 | goto end; |
| 6849 | } |
| 6850 | |
| 6851 | ret = fwd; |
| 6852 | if (ret > len) |
| 6853 | ret = len; |
| 6854 | |
| 6855 | if (ret) { |
| 6856 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6857 | |
| 6858 | FLT_OFF(filter, msg->chn) += ret; |
| 6859 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 6860 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6861 | } |
| 6862 | |
| 6863 | end: |
| 6864 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6865 | return 1; |
| 6866 | } |
| 6867 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6868 | /* Set EOM flag on the HTX message. |
| 6869 | * |
| 6870 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 6871 | * really know how to do without this feature. |
| 6872 | */ |
| 6873 | __LJMP static int hlua_http_msg_set_eom(lua_State *L) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6874 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6875 | struct http_msg *msg; |
| 6876 | struct htx *htx; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6877 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6878 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 6879 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6880 | htx = htxbuf(&msg->chn->buf); |
| 6881 | htx->flags |= HTX_FL_EOM; |
| 6882 | return 0; |
| 6883 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6884 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6885 | /* Unset EOM flag on the HTX message. |
| 6886 | * |
| 6887 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 6888 | * really know how to do without this feature. |
| 6889 | */ |
| 6890 | __LJMP static int hlua_http_msg_unset_eom(lua_State *L) |
| 6891 | { |
| 6892 | struct http_msg *msg; |
| 6893 | struct htx *htx; |
| 6894 | |
| 6895 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 6896 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6897 | htx = htxbuf(&msg->chn->buf); |
| 6898 | htx->flags &= ~HTX_FL_EOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6899 | return 0; |
| 6900 | } |
| 6901 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6902 | /* |
| 6903 | * |
| 6904 | * |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 6905 | * Class HTTPClient |
| 6906 | * |
| 6907 | * |
| 6908 | */ |
| 6909 | __LJMP static struct hlua_httpclient *hlua_checkhttpclient(lua_State *L, int ud) |
| 6910 | { |
| 6911 | return MAY_LJMP(hlua_checkudata(L, ud, class_httpclient_ref)); |
| 6912 | } |
| 6913 | |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 6914 | |
| 6915 | /* stops the httpclient and ask it to kill itself */ |
| 6916 | __LJMP static int hlua_httpclient_gc(lua_State *L) |
| 6917 | { |
| 6918 | struct hlua_httpclient *hlua_hc; |
| 6919 | |
| 6920 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 6921 | |
| 6922 | hlua_hc = MAY_LJMP(hlua_checkhttpclient(L, 1)); |
| 6923 | |
| 6924 | httpclient_stop_and_destroy(hlua_hc->hc); |
| 6925 | |
| 6926 | hlua_hc->hc = NULL; |
| 6927 | |
| 6928 | |
| 6929 | return 0; |
| 6930 | } |
| 6931 | |
| 6932 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 6933 | __LJMP static int hlua_httpclient_new(lua_State *L) |
| 6934 | { |
| 6935 | struct hlua_httpclient *hlua_hc; |
| 6936 | struct hlua *hlua; |
| 6937 | |
| 6938 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6939 | hlua = hlua_gethlua(L); |
| 6940 | if (!hlua) |
| 6941 | return 0; |
| 6942 | |
| 6943 | /* Check stack size. */ |
| 6944 | if (!lua_checkstack(L, 3)) { |
| 6945 | hlua_pusherror(L, "httpclient: full stack"); |
| 6946 | goto err; |
| 6947 | } |
| 6948 | /* Create the object: obj[0] = userdata. */ |
| 6949 | lua_newtable(L); |
| 6950 | hlua_hc = MAY_LJMP(lua_newuserdata(L, sizeof(*hlua_hc))); |
| 6951 | lua_rawseti(L, -2, 0); |
| 6952 | memset(hlua_hc, 0, sizeof(*hlua_hc)); |
| 6953 | |
| 6954 | hlua_hc->hc = httpclient_new(hlua, 0, IST_NULL); |
| 6955 | if (!hlua_hc->hc) |
| 6956 | goto err; |
| 6957 | |
| 6958 | /* Pop a class stream metatable and affect it to the userdata. */ |
| 6959 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_httpclient_ref); |
| 6960 | lua_setmetatable(L, -2); |
| 6961 | |
| 6962 | return 1; |
| 6963 | |
| 6964 | err: |
| 6965 | WILL_LJMP(lua_error(L)); |
| 6966 | return 0; |
| 6967 | } |
| 6968 | |
| 6969 | |
| 6970 | /* |
| 6971 | * Callback of the httpclient, this callback wakes the lua task up, once the |
| 6972 | * httpclient receives some data |
| 6973 | * |
| 6974 | */ |
| 6975 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 6976 | static void hlua_httpclient_cb(struct httpclient *hc) |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 6977 | { |
| 6978 | struct hlua *hlua = hc->caller; |
| 6979 | |
| 6980 | if (!hlua || !hlua->task) |
| 6981 | return; |
| 6982 | |
| 6983 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 6984 | } |
| 6985 | |
| 6986 | /* |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 6987 | * Fill the lua stack with headers from the httpclient response |
| 6988 | * This works the same way as the hlua_http_get_headers() function |
| 6989 | */ |
| 6990 | __LJMP static int hlua_httpclient_get_headers(lua_State *L, struct hlua_httpclient *hlua_hc) |
| 6991 | { |
| 6992 | struct http_hdr *hdr; |
| 6993 | |
| 6994 | lua_newtable(L); |
| 6995 | |
William Lallemand | ef574b2 | 2021-10-05 16:19:31 +0200 | [diff] [blame] | 6996 | for (hdr = hlua_hc->hc->res.hdrs; hdr && isttest(hdr->n); hdr++) { |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 6997 | struct ist n, v; |
| 6998 | int len; |
| 6999 | |
| 7000 | n = hdr->n; |
| 7001 | v = hdr->v; |
| 7002 | |
| 7003 | /* Check for existing entry: |
| 7004 | * assume that the table is on the top of the stack, and |
| 7005 | * push the key in the stack, the function lua_gettable() |
| 7006 | * perform the lookup. |
| 7007 | */ |
| 7008 | |
| 7009 | lua_pushlstring(L, n.ptr, n.len); |
| 7010 | lua_gettable(L, -2); |
| 7011 | |
| 7012 | switch (lua_type(L, -1)) { |
| 7013 | case LUA_TNIL: |
| 7014 | /* Table not found, create it. */ |
| 7015 | lua_pop(L, 1); /* remove the nil value. */ |
| 7016 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 7017 | lua_newtable(L); /* create and push empty table. */ |
| 7018 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7019 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 7020 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 7021 | break; |
| 7022 | |
| 7023 | case LUA_TTABLE: |
| 7024 | /* Entry found: push the value in the table. */ |
| 7025 | len = lua_rawlen(L, -1); |
| 7026 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7027 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 7028 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 7029 | break; |
| 7030 | |
| 7031 | default: |
| 7032 | /* Other cases are errors. */ |
| 7033 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 7034 | WILL_LJMP(lua_error(L)); |
| 7035 | } |
| 7036 | } |
| 7037 | return 1; |
| 7038 | } |
| 7039 | |
| 7040 | /* |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7041 | * Allocate and return an array of http_hdr ist extracted from the <headers> lua table |
| 7042 | * |
| 7043 | * Caller must free the result |
| 7044 | */ |
| 7045 | struct http_hdr *hlua_httpclient_table_to_hdrs(lua_State *L) |
| 7046 | { |
| 7047 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
| 7048 | struct http_hdr *result = NULL; |
| 7049 | uint32_t hdr_num = 0; |
| 7050 | |
| 7051 | lua_pushnil(L); |
| 7052 | while (lua_next(L, -2) != 0) { |
| 7053 | struct ist name, value; |
| 7054 | const char *n, *v; |
| 7055 | size_t nlen, vlen; |
| 7056 | |
| 7057 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 7058 | /* Skip element if the key is not a string or if the value is not a table */ |
| 7059 | goto next_hdr; |
| 7060 | } |
| 7061 | |
| 7062 | n = lua_tolstring(L, -2, &nlen); |
| 7063 | name = ist2(n, nlen); |
| 7064 | |
| 7065 | /* Loop on header's values */ |
| 7066 | lua_pushnil(L); |
| 7067 | while (lua_next(L, -2)) { |
| 7068 | if (!lua_isstring(L, -1)) { |
| 7069 | /* Skip the value if it is not a string */ |
| 7070 | goto next_value; |
| 7071 | } |
| 7072 | |
| 7073 | v = lua_tolstring(L, -1, &vlen); |
| 7074 | value = ist2(v, vlen); |
| 7075 | name = ist2(n, nlen); |
| 7076 | |
| 7077 | hdrs[hdr_num].n = istdup(name); |
| 7078 | hdrs[hdr_num].v = istdup(value); |
| 7079 | |
| 7080 | hdr_num++; |
| 7081 | |
| 7082 | next_value: |
| 7083 | lua_pop(L, 1); |
| 7084 | } |
| 7085 | |
| 7086 | next_hdr: |
| 7087 | lua_pop(L, 1); |
| 7088 | |
| 7089 | } |
| 7090 | |
| 7091 | if (hdr_num) { |
| 7092 | /* alloc and copy the headers in the httpclient struct */ |
Tim Duesterhus | 16cc16d | 2021-11-06 15:14:45 +0100 | [diff] [blame] | 7093 | result = calloc((hdr_num + 1), sizeof(*result)); |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7094 | if (!result) |
| 7095 | goto skip_headers; |
| 7096 | memcpy(result, hdrs, sizeof(struct http_hdr) * (hdr_num + 1)); |
| 7097 | |
| 7098 | result[hdr_num].n = IST_NULL; |
| 7099 | result[hdr_num].v = IST_NULL; |
| 7100 | } |
| 7101 | |
| 7102 | skip_headers: |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7103 | |
| 7104 | return result; |
| 7105 | } |
| 7106 | |
| 7107 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7108 | /* |
| 7109 | * For each yield, checks if there is some data in the httpclient and push them |
| 7110 | * in the lua buffer, once the httpclient finished its job, push the result on |
| 7111 | * the stack |
| 7112 | */ |
| 7113 | __LJMP static int hlua_httpclient_rcv_yield(lua_State *L, int status, lua_KContext ctx) |
| 7114 | { |
| 7115 | struct buffer *tr; |
| 7116 | int res; |
| 7117 | struct hlua *hlua = hlua_gethlua(L); |
| 7118 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7119 | |
| 7120 | |
| 7121 | tr = get_trash_chunk(); |
| 7122 | |
| 7123 | res = httpclient_res_xfer(hlua_hc->hc, tr); |
| 7124 | luaL_addlstring(&hlua_hc->b, b_orig(tr), res); |
| 7125 | |
| 7126 | if (!httpclient_data(hlua_hc->hc) && httpclient_ended(hlua_hc->hc)) { |
| 7127 | |
| 7128 | luaL_pushresult(&hlua_hc->b); |
| 7129 | lua_settable(L, -3); |
| 7130 | |
| 7131 | lua_pushstring(L, "status"); |
| 7132 | lua_pushinteger(L, hlua_hc->hc->res.status); |
| 7133 | lua_settable(L, -3); |
| 7134 | |
| 7135 | |
| 7136 | lua_pushstring(L, "reason"); |
| 7137 | lua_pushlstring(L, hlua_hc->hc->res.reason.ptr, hlua_hc->hc->res.reason.len); |
| 7138 | lua_settable(L, -3); |
| 7139 | |
| 7140 | lua_pushstring(L, "headers"); |
| 7141 | hlua_httpclient_get_headers(L, hlua_hc); |
| 7142 | lua_settable(L, -3); |
| 7143 | |
| 7144 | return 1; |
| 7145 | } |
| 7146 | |
| 7147 | if (httpclient_data(hlua_hc->hc)) |
| 7148 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 7149 | |
| 7150 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7151 | |
| 7152 | return 0; |
| 7153 | } |
| 7154 | |
| 7155 | /* |
| 7156 | * Call this when trying to stream a body during a request |
| 7157 | */ |
| 7158 | __LJMP static int hlua_httpclient_snd_yield(lua_State *L, int status, lua_KContext ctx) |
| 7159 | { |
| 7160 | struct hlua *hlua; |
| 7161 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7162 | const char *body_str = NULL; |
| 7163 | int ret; |
| 7164 | int end = 0; |
| 7165 | size_t buf_len; |
| 7166 | size_t to_send = 0; |
| 7167 | |
| 7168 | hlua = hlua_gethlua(L); |
| 7169 | |
| 7170 | if (!hlua || !hlua->task) |
| 7171 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7172 | "'frontend', 'backend' or 'task'")); |
| 7173 | |
| 7174 | ret = lua_getfield(L, -1, "body"); |
| 7175 | if (ret != LUA_TSTRING) |
| 7176 | goto rcv; |
| 7177 | |
| 7178 | body_str = lua_tolstring(L, -1, &buf_len); |
| 7179 | lua_pop(L, 1); |
| 7180 | |
Christopher Faulet | fc59129 | 2022-01-12 14:46:03 +0100 | [diff] [blame] | 7181 | to_send = buf_len - hlua_hc->sent; |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7182 | |
| 7183 | if ((hlua_hc->sent + to_send) >= buf_len) |
| 7184 | end = 1; |
| 7185 | |
| 7186 | /* the end flag is always set since we are using the whole remaining size */ |
| 7187 | hlua_hc->sent += httpclient_req_xfer(hlua_hc->hc, ist2(body_str + hlua_hc->sent, to_send), end); |
| 7188 | |
| 7189 | if (buf_len > hlua_hc->sent) { |
| 7190 | /* still need to process the buffer */ |
| 7191 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_snd_yield, TICK_ETERNITY, 0)); |
| 7192 | } else { |
| 7193 | goto rcv; |
| 7194 | /* we sent the whole request buffer we can recv */ |
| 7195 | } |
| 7196 | return 0; |
| 7197 | |
| 7198 | rcv: |
| 7199 | |
| 7200 | /* we return a "res" object */ |
| 7201 | lua_newtable(L); |
| 7202 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7203 | lua_pushstring(L, "body"); |
William Lallemand | d1187eb | 2021-11-02 10:40:06 +0100 | [diff] [blame] | 7204 | luaL_buffinit(L, &hlua_hc->b); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7205 | |
William Lallemand | 933fe39 | 2021-11-04 09:45:58 +0100 | [diff] [blame] | 7206 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7207 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7208 | |
| 7209 | return 1; |
| 7210 | } |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7211 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7212 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7213 | * Send an HTTP request and wait for a response |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7214 | */ |
| 7215 | |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7216 | __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] | 7217 | { |
| 7218 | struct hlua_httpclient *hlua_hc; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7219 | struct http_hdr *hdrs = NULL; |
| 7220 | struct http_hdr *hdrs_i = NULL; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7221 | struct hlua *hlua; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7222 | const char *url_str = NULL; |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7223 | const char *body_str = NULL; |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7224 | size_t buf_len; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7225 | int ret; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7226 | |
| 7227 | hlua = hlua_gethlua(L); |
| 7228 | |
| 7229 | if (!hlua || !hlua->task) |
| 7230 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7231 | "'frontend', 'backend' or 'task'")); |
| 7232 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7233 | if (lua_gettop(L) != 2 || lua_type(L, -1) != LUA_TTABLE) |
| 7234 | WILL_LJMP(luaL_error(L, "'get' needs a table as argument")); |
| 7235 | |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 7236 | hlua_hc = hlua_checkhttpclient(L, 1); |
| 7237 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7238 | lua_pushnil(L); /* first key */ |
| 7239 | while (lua_next(L, 2)) { |
| 7240 | if (strcmp(lua_tostring(L, -2), "dst") == 0) { |
| 7241 | if (httpclient_set_dst(hlua_hc->hc, lua_tostring(L, -1)) < 0) |
| 7242 | WILL_LJMP(luaL_error(L, "Can't use the 'dst' argument")); |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 7243 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7244 | } else if (strcmp(lua_tostring(L, -2), "url") == 0) { |
| 7245 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7246 | WILL_LJMP(luaL_error(L, "invalid parameter in 'url', must be a string")); |
| 7247 | url_str = lua_tostring(L, -1); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7248 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7249 | } else if (strcmp(lua_tostring(L, -2), "timeout") == 0) { |
| 7250 | if (lua_type(L, -1) != LUA_TNUMBER) |
| 7251 | WILL_LJMP(luaL_error(L, "invalid parameter in 'timeout', must be a number")); |
| 7252 | httpclient_set_timeout(hlua_hc->hc, lua_tointeger(L, -1)); |
William Lallemand | b4a4ef6 | 2022-02-23 14:18:16 +0100 | [diff] [blame] | 7253 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7254 | } else if (strcmp(lua_tostring(L, -2), "headers") == 0) { |
| 7255 | if (lua_type(L, -1) != LUA_TTABLE) |
| 7256 | WILL_LJMP(luaL_error(L, "invalid parameter in 'headers', must be a table")); |
| 7257 | hdrs = hlua_httpclient_table_to_hdrs(L); |
William Lallemand | 79416cb | 2021-09-24 14:51:44 +0200 | [diff] [blame] | 7258 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7259 | } else if (strcmp(lua_tostring(L, -2), "body") == 0) { |
| 7260 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7261 | WILL_LJMP(luaL_error(L, "invalid parameter in 'body', must be a string")); |
| 7262 | body_str = lua_tolstring(L, -1, &buf_len); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7263 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7264 | } else { |
| 7265 | WILL_LJMP(luaL_error(L, "'%s' invalid parameter name", lua_tostring(L, -2))); |
| 7266 | } |
| 7267 | /* removes 'value'; keeps 'key' for next iteration */ |
| 7268 | lua_pop(L, 1); |
| 7269 | } |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7270 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7271 | if (!url_str) { |
| 7272 | WILL_LJMP(luaL_error(L, "'get' need a 'url' argument")); |
| 7273 | return 0; |
| 7274 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7275 | |
William Lallemand | 10a3736 | 2022-03-02 16:18:26 +0100 | [diff] [blame] | 7276 | hlua_hc->sent = 0; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7277 | |
| 7278 | hlua_hc->hc->req.url = istdup(ist(url_str)); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7279 | hlua_hc->hc->req.meth = meth; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7280 | |
| 7281 | /* update the httpclient callbacks */ |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7282 | hlua_hc->hc->ops.res_stline = hlua_httpclient_cb; |
| 7283 | hlua_hc->hc->ops.res_headers = hlua_httpclient_cb; |
| 7284 | hlua_hc->hc->ops.res_payload = hlua_httpclient_cb; |
William Lallemand | 8f170c7 | 2022-03-15 10:52:07 +0100 | [diff] [blame] | 7285 | hlua_hc->hc->ops.res_end = hlua_httpclient_cb; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7286 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7287 | /* a body is available, it will use the request callback */ |
| 7288 | if (body_str) { |
| 7289 | hlua_hc->hc->ops.req_payload = hlua_httpclient_cb; |
| 7290 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7291 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7292 | 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] | 7293 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7294 | /* free the temporary headers array */ |
| 7295 | hdrs_i = hdrs; |
| 7296 | while (hdrs_i && isttest(hdrs_i->n)) { |
| 7297 | istfree(&hdrs_i->n); |
| 7298 | istfree(&hdrs_i->v); |
| 7299 | hdrs_i++; |
| 7300 | } |
| 7301 | ha_free(&hdrs); |
| 7302 | |
| 7303 | |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7304 | if (ret != ERR_NONE) { |
| 7305 | WILL_LJMP(luaL_error(L, "Can't generate the HTTP request")); |
| 7306 | return 0; |
| 7307 | } |
| 7308 | |
William Lallemand | c2d3db4 | 2022-04-26 11:46:13 +0200 | [diff] [blame] | 7309 | if (!httpclient_start(hlua_hc->hc)) |
| 7310 | WILL_LJMP(luaL_error(L, "couldn't start the httpclient")); |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7311 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7312 | 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] | 7313 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7314 | return 0; |
| 7315 | } |
| 7316 | |
| 7317 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7318 | * Sends an HTTP HEAD request and wait for a response |
| 7319 | * |
| 7320 | * httpclient:head(url, headers, payload) |
| 7321 | */ |
| 7322 | __LJMP static int hlua_httpclient_head(lua_State *L) |
| 7323 | { |
| 7324 | return hlua_httpclient_send(L, HTTP_METH_HEAD); |
| 7325 | } |
| 7326 | |
| 7327 | /* |
| 7328 | * Send an HTTP GET request and wait for a response |
| 7329 | * |
| 7330 | * httpclient:get(url, headers, payload) |
| 7331 | */ |
| 7332 | __LJMP static int hlua_httpclient_get(lua_State *L) |
| 7333 | { |
| 7334 | return hlua_httpclient_send(L, HTTP_METH_GET); |
| 7335 | |
| 7336 | } |
| 7337 | |
| 7338 | /* |
| 7339 | * Sends an HTTP PUT request and wait for a response |
| 7340 | * |
| 7341 | * httpclient:put(url, headers, payload) |
| 7342 | */ |
| 7343 | __LJMP static int hlua_httpclient_put(lua_State *L) |
| 7344 | { |
| 7345 | return hlua_httpclient_send(L, HTTP_METH_PUT); |
| 7346 | } |
| 7347 | |
| 7348 | /* |
| 7349 | * Send an HTTP POST request and wait for a response |
| 7350 | * |
| 7351 | * httpclient:post(url, headers, payload) |
| 7352 | */ |
| 7353 | __LJMP static int hlua_httpclient_post(lua_State *L) |
| 7354 | { |
| 7355 | return hlua_httpclient_send(L, HTTP_METH_POST); |
| 7356 | } |
| 7357 | |
| 7358 | |
| 7359 | /* |
| 7360 | * Sends an HTTP DELETE request and wait for a response |
| 7361 | * |
| 7362 | * httpclient:delete(url, headers, payload) |
| 7363 | */ |
| 7364 | __LJMP static int hlua_httpclient_delete(lua_State *L) |
| 7365 | { |
| 7366 | return hlua_httpclient_send(L, HTTP_METH_DELETE); |
| 7367 | } |
| 7368 | |
| 7369 | /* |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7370 | * |
| 7371 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7372 | * Class TXN |
| 7373 | * |
| 7374 | * |
| 7375 | */ |
| 7376 | |
| 7377 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7378 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7379 | */ |
| 7380 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 7381 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7382 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7383 | } |
| 7384 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7385 | __LJMP static int hlua_set_var(lua_State *L) |
| 7386 | { |
| 7387 | struct hlua_txn *htxn; |
| 7388 | const char *name; |
| 7389 | size_t len; |
| 7390 | struct sample smp; |
| 7391 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 7392 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7393 | 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] | 7394 | |
| 7395 | /* It is useles to retrieve the stream, but this function |
| 7396 | * runs only in a stream context. |
| 7397 | */ |
| 7398 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7399 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7400 | |
| 7401 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 7402 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7403 | hlua_lua2smp(L, 3, &smp); |
| 7404 | |
| 7405 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7406 | 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] | 7407 | |
| 7408 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 7409 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 7410 | else |
| 7411 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 7412 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 7413 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7414 | } |
| 7415 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7416 | __LJMP static int hlua_unset_var(lua_State *L) |
| 7417 | { |
| 7418 | struct hlua_txn *htxn; |
| 7419 | const char *name; |
| 7420 | size_t len; |
| 7421 | struct sample smp; |
| 7422 | |
| 7423 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 7424 | |
| 7425 | /* It is useles to retrieve the stream, but this function |
| 7426 | * runs only in a stream context. |
| 7427 | */ |
| 7428 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7429 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7430 | |
| 7431 | /* Unset the variable. */ |
| 7432 | 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] | 7433 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 7434 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7435 | } |
| 7436 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7437 | __LJMP static int hlua_get_var(lua_State *L) |
| 7438 | { |
| 7439 | struct hlua_txn *htxn; |
| 7440 | const char *name; |
| 7441 | size_t len; |
| 7442 | struct sample smp; |
| 7443 | |
| 7444 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 7445 | |
| 7446 | /* It is useles to retrieve the stream, but this function |
| 7447 | * runs only in a stream context. |
| 7448 | */ |
| 7449 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7450 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7451 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7452 | 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] | 7453 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7454 | lua_pushnil(L); |
| 7455 | return 1; |
| 7456 | } |
| 7457 | |
| 7458 | return hlua_smp2lua(L, &smp); |
| 7459 | } |
| 7460 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7461 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7462 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7463 | struct hlua *hlua; |
| 7464 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7465 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 7466 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7467 | /* It is useles to retrieve the stream, but this function |
| 7468 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7469 | */ |
| 7470 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7471 | |
| 7472 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7473 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7474 | if (!hlua) |
| 7475 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7476 | |
| 7477 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 7478 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7479 | |
| 7480 | /* Get and store new value. */ |
| 7481 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 7482 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 7483 | |
| 7484 | return 0; |
| 7485 | } |
| 7486 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7487 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7488 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7489 | struct hlua *hlua; |
| 7490 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7491 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 7492 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7493 | /* It is useles to retrieve the stream, but this function |
| 7494 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7495 | */ |
| 7496 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7497 | |
| 7498 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7499 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7500 | if (!hlua) { |
| 7501 | lua_pushnil(L); |
| 7502 | return 1; |
| 7503 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7504 | |
| 7505 | /* Push configuration index in the stack. */ |
| 7506 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 7507 | |
| 7508 | return 1; |
| 7509 | } |
| 7510 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7511 | /* Create stack entry containing a class TXN. This function |
| 7512 | * return 0 if the stack does not contains free slots, |
| 7513 | * otherwise it returns 1. |
| 7514 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7515 | 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] | 7516 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7517 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7518 | |
| 7519 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7520 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7521 | return 0; |
| 7522 | |
| 7523 | /* NOTE: The allocation never fails. The failure |
| 7524 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7525 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7526 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7527 | /* Create the object: obj[0] = userdata. */ |
| 7528 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7529 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7530 | lua_rawseti(L, -2, 0); |
| 7531 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7532 | htxn->s = s; |
| 7533 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 7534 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7535 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7536 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7537 | /* Create the "f" field that contains a list of fetches. */ |
| 7538 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7539 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7540 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7541 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7542 | |
| 7543 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 7544 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7545 | 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] | 7546 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7547 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7548 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7549 | /* Create the "c" field that contains a list of converters. */ |
| 7550 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7551 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7552 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7553 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7554 | |
| 7555 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 7556 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 7557 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7558 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7559 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7560 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7561 | /* Create the "req" field that contains the request channel object. */ |
| 7562 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7563 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7564 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7565 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7566 | |
| 7567 | /* Create the "res" field that contains the response channel object. */ |
| 7568 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7569 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7570 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7571 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7572 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7573 | /* Creates the HTTP object is the current proxy allows http. */ |
| 7574 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 7575 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7576 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7577 | return 0; |
| 7578 | } |
| 7579 | else |
| 7580 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7581 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7582 | |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 7583 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) { |
| 7584 | /* HTTPMessage object are created when a lua TXN is created from |
| 7585 | * a filter context only |
| 7586 | */ |
| 7587 | |
| 7588 | /* Creates the HTTP-Request object is the current proxy allows http. */ |
| 7589 | lua_pushstring(L, "http_req"); |
| 7590 | if (p->mode == PR_MODE_HTTP) { |
| 7591 | if (!hlua_http_msg_new(L, &s->txn->req)) |
| 7592 | return 0; |
| 7593 | } |
| 7594 | else |
| 7595 | lua_pushnil(L); |
| 7596 | lua_rawset(L, -3); |
| 7597 | |
| 7598 | /* Creates the HTTP-Response object is the current proxy allows http. */ |
| 7599 | lua_pushstring(L, "http_res"); |
| 7600 | if (p->mode == PR_MODE_HTTP) { |
| 7601 | if (!hlua_http_msg_new(L, &s->txn->rsp)) |
| 7602 | return 0; |
| 7603 | } |
| 7604 | else |
| 7605 | lua_pushnil(L); |
| 7606 | lua_rawset(L, -3); |
| 7607 | } |
| 7608 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7609 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 7610 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 7611 | lua_setmetatable(L, -2); |
| 7612 | |
| 7613 | return 1; |
| 7614 | } |
| 7615 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7616 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 7617 | { |
| 7618 | const char *msg; |
| 7619 | struct hlua_txn *htxn; |
| 7620 | |
| 7621 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 7622 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7623 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7624 | |
| 7625 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 7626 | return 0; |
| 7627 | } |
| 7628 | |
| 7629 | __LJMP static int hlua_txn_log(lua_State *L) |
| 7630 | { |
| 7631 | int level; |
| 7632 | const char *msg; |
| 7633 | struct hlua_txn *htxn; |
| 7634 | |
| 7635 | MAY_LJMP(check_args(L, 3, "log")); |
| 7636 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7637 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7638 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 7639 | |
| 7640 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 7641 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 7642 | |
| 7643 | hlua_sendlog(htxn->s->be, level, msg); |
| 7644 | return 0; |
| 7645 | } |
| 7646 | |
| 7647 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 7648 | { |
| 7649 | const char *msg; |
| 7650 | struct hlua_txn *htxn; |
| 7651 | |
| 7652 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 7653 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7654 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7655 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 7656 | return 0; |
| 7657 | } |
| 7658 | |
| 7659 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 7660 | { |
| 7661 | const char *msg; |
| 7662 | struct hlua_txn *htxn; |
| 7663 | |
| 7664 | MAY_LJMP(check_args(L, 2, "Info")); |
| 7665 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7666 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7667 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 7668 | return 0; |
| 7669 | } |
| 7670 | |
| 7671 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 7672 | { |
| 7673 | const char *msg; |
| 7674 | struct hlua_txn *htxn; |
| 7675 | |
| 7676 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 7677 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7678 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7679 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 7680 | return 0; |
| 7681 | } |
| 7682 | |
| 7683 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 7684 | { |
| 7685 | const char *msg; |
| 7686 | struct hlua_txn *htxn; |
| 7687 | |
| 7688 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 7689 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7690 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7691 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 7692 | return 0; |
| 7693 | } |
| 7694 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7695 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 7696 | { |
| 7697 | struct hlua_txn *htxn; |
| 7698 | int ll; |
| 7699 | |
| 7700 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 7701 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7702 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7703 | |
| 7704 | if (ll < 0 || ll > 7) |
| 7705 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 7706 | |
| 7707 | htxn->s->logs.level = ll; |
| 7708 | return 0; |
| 7709 | } |
| 7710 | |
| 7711 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 7712 | { |
| 7713 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7714 | int tos; |
| 7715 | |
| 7716 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 7717 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7718 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7719 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 7720 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7721 | return 0; |
| 7722 | } |
| 7723 | |
| 7724 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 7725 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7726 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7727 | int mark; |
| 7728 | |
| 7729 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 7730 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7731 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7732 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 7733 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7734 | return 0; |
| 7735 | } |
| 7736 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 7737 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 7738 | { |
| 7739 | struct hlua_txn *htxn; |
| 7740 | |
| 7741 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 7742 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7743 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 7744 | return 0; |
| 7745 | } |
| 7746 | |
| 7747 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 7748 | { |
| 7749 | struct hlua_txn *htxn; |
| 7750 | |
| 7751 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 7752 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7753 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 7754 | return 0; |
| 7755 | } |
| 7756 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7757 | /* 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] | 7758 | * 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] | 7759 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 7760 | * error. The Reply must be on top of the stack. |
| 7761 | */ |
| 7762 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 7763 | { |
| 7764 | struct htx *htx; |
| 7765 | struct htx_sl *sl; |
| 7766 | struct h1m h1m; |
| 7767 | const char *status, *reason, *body; |
| 7768 | size_t status_len, reason_len, body_len; |
| 7769 | int ret, code, flags; |
| 7770 | |
| 7771 | code = 200; |
| 7772 | status = "200"; |
| 7773 | status_len = 3; |
| 7774 | ret = lua_getfield(L, -1, "status"); |
| 7775 | if (ret == LUA_TNUMBER) { |
| 7776 | code = lua_tointeger(L, -1); |
| 7777 | status = lua_tolstring(L, -1, &status_len); |
| 7778 | } |
| 7779 | lua_pop(L, 1); |
| 7780 | |
| 7781 | reason = http_get_reason(code); |
| 7782 | reason_len = strlen(reason); |
| 7783 | ret = lua_getfield(L, -1, "reason"); |
| 7784 | if (ret == LUA_TSTRING) |
| 7785 | reason = lua_tolstring(L, -1, &reason_len); |
| 7786 | lua_pop(L, 1); |
| 7787 | |
| 7788 | body = NULL; |
| 7789 | body_len = 0; |
| 7790 | ret = lua_getfield(L, -1, "body"); |
| 7791 | if (ret == LUA_TSTRING) |
| 7792 | body = lua_tolstring(L, -1, &body_len); |
| 7793 | lua_pop(L, 1); |
| 7794 | |
| 7795 | /* Prepare the response before inserting the headers */ |
| 7796 | h1m_init_res(&h1m); |
| 7797 | htx = htx_from_buf(&s->res.buf); |
| 7798 | channel_htx_truncate(&s->res, htx); |
| 7799 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 7800 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 7801 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 7802 | ist2(status, status_len), ist2(reason, reason_len)); |
| 7803 | } |
| 7804 | else { |
| 7805 | flags = HTX_SL_F_IS_RESP; |
| 7806 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 7807 | ist2(status, status_len), ist2(reason, reason_len)); |
| 7808 | } |
| 7809 | if (!sl) |
| 7810 | goto fail; |
| 7811 | sl->info.res.status = code; |
| 7812 | |
| 7813 | /* Push in the stack the "headers" entry. */ |
| 7814 | ret = lua_getfield(L, -1, "headers"); |
| 7815 | if (ret != LUA_TTABLE) |
| 7816 | goto skip_headers; |
| 7817 | |
| 7818 | lua_pushnil(L); |
| 7819 | while (lua_next(L, -2) != 0) { |
| 7820 | struct ist name, value; |
| 7821 | const char *n, *v; |
| 7822 | size_t nlen, vlen; |
| 7823 | |
| 7824 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 7825 | /* Skip element if the key is not a string or if the value is not a table */ |
| 7826 | goto next_hdr; |
| 7827 | } |
| 7828 | |
| 7829 | n = lua_tolstring(L, -2, &nlen); |
| 7830 | name = ist2(n, nlen); |
| 7831 | if (isteqi(name, ist("content-length"))) { |
| 7832 | /* Always skip content-length header. It will be added |
| 7833 | * later with the correct len |
| 7834 | */ |
| 7835 | goto next_hdr; |
| 7836 | } |
| 7837 | |
| 7838 | /* Loop on header's values */ |
| 7839 | lua_pushnil(L); |
| 7840 | while (lua_next(L, -2)) { |
| 7841 | if (!lua_isstring(L, -1)) { |
| 7842 | /* Skip the value if it is not a string */ |
| 7843 | goto next_value; |
| 7844 | } |
| 7845 | |
| 7846 | v = lua_tolstring(L, -1, &vlen); |
| 7847 | value = ist2(v, vlen); |
| 7848 | |
| 7849 | if (isteqi(name, ist("transfer-encoding"))) |
| 7850 | h1_parse_xfer_enc_header(&h1m, value); |
| 7851 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 7852 | goto fail; |
| 7853 | |
| 7854 | next_value: |
| 7855 | lua_pop(L, 1); |
| 7856 | } |
| 7857 | |
| 7858 | next_hdr: |
| 7859 | lua_pop(L, 1); |
| 7860 | } |
| 7861 | skip_headers: |
| 7862 | lua_pop(L, 1); |
| 7863 | |
| 7864 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 7865 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 7866 | const char *clen = ultoa(body_len); |
| 7867 | |
| 7868 | h1m.flags |= H1_MF_CLEN; |
| 7869 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 7870 | goto fail; |
| 7871 | } |
| 7872 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 7873 | h1m.flags |= H1_MF_XFER_LEN; |
| 7874 | |
| 7875 | /* Update HTX start-line flags */ |
| 7876 | if (h1m.flags & H1_MF_XFER_ENC) |
| 7877 | flags |= HTX_SL_F_XFER_ENC; |
| 7878 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 7879 | flags |= HTX_SL_F_XFER_LEN; |
| 7880 | if (h1m.flags & H1_MF_CHNK) |
| 7881 | flags |= HTX_SL_F_CHNK; |
| 7882 | else if (h1m.flags & H1_MF_CLEN) |
| 7883 | flags |= HTX_SL_F_CLEN; |
| 7884 | if (h1m.body_len == 0) |
| 7885 | flags |= HTX_SL_F_BODYLESS; |
| 7886 | } |
| 7887 | sl->flags |= flags; |
| 7888 | |
| 7889 | |
| 7890 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7891 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7892 | goto fail; |
| 7893 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7894 | htx->flags |= HTX_FL_EOM; |
| 7895 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7896 | /* Now, forward the response and terminate the transaction */ |
| 7897 | s->txn->status = code; |
| 7898 | htx_to_buf(htx, &s->res.buf); |
| 7899 | if (!http_forward_proxy_resp(s, 1)) |
| 7900 | goto fail; |
| 7901 | |
| 7902 | return 1; |
| 7903 | |
| 7904 | fail: |
| 7905 | channel_htx_truncate(&s->res, htx); |
| 7906 | return 0; |
| 7907 | } |
| 7908 | |
| 7909 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 7910 | * processing is just aborted. Nothing is returned to the client and all |
| 7911 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 7912 | * is forwarded to the client before terminating the transaction. On success, |
| 7913 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 7914 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 7915 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7916 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7917 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7918 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 7919 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7920 | struct stream *s; |
| 7921 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7922 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 7923 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7924 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7925 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 7926 | * just end the execution of the current lua code. */ |
| 7927 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7928 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7929 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7930 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 7931 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7932 | struct channel *req = &s->req; |
| 7933 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 7934 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7935 | channel_auto_read(req); |
| 7936 | channel_abort(req); |
| 7937 | channel_auto_close(req); |
| 7938 | channel_erase(req); |
| 7939 | |
| 7940 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 7941 | channel_auto_read(res); |
| 7942 | channel_auto_close(res); |
| 7943 | channel_shutr_now(res); |
| 7944 | |
| 7945 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 7946 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 7947 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 7948 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7949 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 7950 | /* No reply or invalid reply */ |
| 7951 | s->txn->status = 0; |
| 7952 | http_reply_and_close(s, 0, NULL); |
| 7953 | } |
| 7954 | else { |
| 7955 | /* Remove extra args to have the reply on top of the stack */ |
| 7956 | if (lua_gettop(L) > 2) |
| 7957 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7958 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7959 | if (!hlua_txn_forward_reply(L, s)) { |
| 7960 | if (!(s->flags & SF_ERR_MASK)) |
| 7961 | s->flags |= SF_ERR_PRXCOND; |
| 7962 | lua_pushinteger(L, ACT_RET_ERR); |
| 7963 | WILL_LJMP(hlua_done(L)); |
| 7964 | return 0; /* Never reached */ |
| 7965 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 7966 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7967 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7968 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 7969 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 7970 | /* let's log the request time */ |
| 7971 | s->logs.tv_request = now; |
| 7972 | 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] | 7973 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7974 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 7975 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7976 | done: |
| 7977 | if (!(s->flags & SF_ERR_MASK)) |
| 7978 | s->flags |= SF_ERR_LOCAL; |
| 7979 | if (!(s->flags & SF_FINST_MASK)) |
| 7980 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 7981 | |
Christopher Faulet | e48d1dc | 2021-08-13 14:11:17 +0200 | [diff] [blame] | 7982 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) |
| 7983 | lua_pushinteger(L, -1); |
| 7984 | else |
| 7985 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7986 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7987 | return 0; |
| 7988 | } |
| 7989 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7990 | /* |
| 7991 | * |
| 7992 | * |
| 7993 | * Class REPLY |
| 7994 | * |
| 7995 | * |
| 7996 | */ |
| 7997 | |
| 7998 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 7999 | * free slots, the function fails and returns 0; |
| 8000 | */ |
| 8001 | static int hlua_txn_reply_new(lua_State *L) |
| 8002 | { |
| 8003 | struct hlua_txn *htxn; |
| 8004 | const char *reason, *body = NULL; |
| 8005 | int ret, status; |
| 8006 | |
| 8007 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8008 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8009 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 8010 | WILL_LJMP(lua_error(L)); |
| 8011 | } |
| 8012 | |
| 8013 | /* Default value */ |
| 8014 | status = 200; |
| 8015 | reason = http_get_reason(status); |
| 8016 | |
| 8017 | if (lua_istable(L, 2)) { |
| 8018 | /* load status and reason from the table argument at index 2 */ |
| 8019 | ret = lua_getfield(L, 2, "status"); |
| 8020 | if (ret == LUA_TNIL) |
| 8021 | goto reason; |
| 8022 | else if (ret != LUA_TNUMBER) { |
| 8023 | /* invalid status: ignore the reason */ |
| 8024 | goto body; |
| 8025 | } |
| 8026 | status = lua_tointeger(L, -1); |
| 8027 | |
| 8028 | reason: |
| 8029 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 8030 | ret = lua_getfield(L, 2, "reason"); |
| 8031 | if (ret == LUA_TSTRING) |
| 8032 | reason = lua_tostring(L, -1); |
| 8033 | |
| 8034 | body: |
| 8035 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 8036 | ret = lua_getfield(L, 2, "body"); |
| 8037 | if (ret == LUA_TSTRING) |
| 8038 | body = lua_tostring(L, -1); |
| 8039 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 8040 | } |
| 8041 | |
| 8042 | /* Create the Reply table */ |
| 8043 | lua_newtable(L); |
| 8044 | |
| 8045 | /* Add status element */ |
| 8046 | lua_pushstring(L, "status"); |
| 8047 | lua_pushinteger(L, status); |
| 8048 | lua_settable(L, -3); |
| 8049 | |
| 8050 | /* Add reason element */ |
| 8051 | reason = http_get_reason(status); |
| 8052 | lua_pushstring(L, "reason"); |
| 8053 | lua_pushstring(L, reason); |
| 8054 | lua_settable(L, -3); |
| 8055 | |
| 8056 | /* Add body element, nil if undefined */ |
| 8057 | lua_pushstring(L, "body"); |
| 8058 | if (body) |
| 8059 | lua_pushstring(L, body); |
| 8060 | else |
| 8061 | lua_pushnil(L); |
| 8062 | lua_settable(L, -3); |
| 8063 | |
| 8064 | /* Add headers element */ |
| 8065 | lua_pushstring(L, "headers"); |
| 8066 | lua_newtable(L); |
| 8067 | |
| 8068 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8069 | if (lua_istable(L, 2)) { |
| 8070 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 8071 | ret = lua_getfield(L, 2, "headers"); |
| 8072 | if (ret == LUA_TTABLE) { |
| 8073 | /* stack: [ ... <headers:table>, <table> ] */ |
| 8074 | lua_pushnil(L); |
| 8075 | while (lua_next(L, -2) != 0) { |
| 8076 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 8077 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 8078 | /* invalid value type, skip it */ |
| 8079 | lua_pop(L, 1); |
| 8080 | continue; |
| 8081 | } |
| 8082 | |
| 8083 | |
| 8084 | /* Duplicate the key and swap it with the value. */ |
| 8085 | lua_pushvalue(L, -2); |
| 8086 | lua_insert(L, -2); |
| 8087 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 8088 | |
| 8089 | lua_newtable(L); |
| 8090 | lua_insert(L, -2); |
| 8091 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 8092 | |
| 8093 | if (lua_isstring(L, -1)) { |
| 8094 | /* push the value in the inner table */ |
| 8095 | lua_rawseti(L, -2, 1); |
| 8096 | } |
| 8097 | else { /* table */ |
| 8098 | lua_pushnil(L); |
| 8099 | while (lua_next(L, -2) != 0) { |
| 8100 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 8101 | if (!lua_isstring(L, -1)) { |
| 8102 | /* invalid value type, skip it*/ |
| 8103 | lua_pop(L, 1); |
| 8104 | continue; |
| 8105 | } |
| 8106 | /* push the value in the inner table */ |
| 8107 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 8108 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 8109 | } |
| 8110 | lua_pop(L, 1); |
| 8111 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 8112 | } |
| 8113 | |
| 8114 | /* push (k,v) on the stack in the headers table: |
| 8115 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 8116 | */ |
| 8117 | lua_settable(L, -5); |
| 8118 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 8119 | } |
| 8120 | } |
| 8121 | lua_pop(L, 1); |
| 8122 | } |
| 8123 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8124 | lua_settable(L, -3); |
| 8125 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 8126 | |
| 8127 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 8128 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 8129 | lua_setmetatable(L, -2); |
| 8130 | return 1; |
| 8131 | } |
| 8132 | |
| 8133 | /* Set the reply status code, and optionally the reason. If no reason is |
| 8134 | * provided, the default one corresponding to the status code is used. |
| 8135 | */ |
| 8136 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 8137 | { |
| 8138 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 8139 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 8140 | |
| 8141 | /* First argument (self) must be a table */ |
| 8142 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8143 | |
| 8144 | if (status < 100 || status > 599) { |
| 8145 | lua_pushboolean(L, 0); |
| 8146 | return 1; |
| 8147 | } |
| 8148 | if (!reason) |
| 8149 | reason = http_get_reason(status); |
| 8150 | |
| 8151 | lua_pushinteger(L, status); |
| 8152 | lua_setfield(L, 1, "status"); |
| 8153 | |
| 8154 | lua_pushstring(L, reason); |
| 8155 | lua_setfield(L, 1, "reason"); |
| 8156 | |
| 8157 | lua_pushboolean(L, 1); |
| 8158 | return 1; |
| 8159 | } |
| 8160 | |
| 8161 | /* Add a header into the reply object. Each header name is associated to an |
| 8162 | * array of values in the "headers" table. If the header name is not found, a |
| 8163 | * new entry is created. |
| 8164 | */ |
| 8165 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 8166 | { |
| 8167 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8168 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 8169 | int ret; |
| 8170 | |
| 8171 | /* First argument (self) must be a table */ |
| 8172 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8173 | |
| 8174 | /* Push in the stack the "headers" entry. */ |
| 8175 | ret = lua_getfield(L, 1, "headers"); |
| 8176 | if (ret != LUA_TTABLE) { |
| 8177 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 8178 | WILL_LJMP(lua_error(L)); |
| 8179 | } |
| 8180 | |
| 8181 | /* check if the header is already registered. If not, register it. */ |
| 8182 | ret = lua_getfield(L, -1, name); |
| 8183 | if (ret == LUA_TNIL) { |
| 8184 | /* Entry not found. */ |
| 8185 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 8186 | |
| 8187 | /* Insert the new header name in the array in the top of the stack. |
| 8188 | * It left the new array in the top of the stack. |
| 8189 | */ |
| 8190 | lua_newtable(L); |
| 8191 | lua_pushstring(L, name); |
| 8192 | lua_pushvalue(L, -2); |
| 8193 | lua_settable(L, -4); |
| 8194 | } |
| 8195 | else if (ret != LUA_TTABLE) { |
| 8196 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 8197 | WILL_LJMP(lua_error(L)); |
| 8198 | } |
| 8199 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8200 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8201 | * the header value as new entry. |
| 8202 | */ |
| 8203 | lua_pushstring(L, value); |
| 8204 | ret = lua_rawlen(L, -2); |
| 8205 | lua_rawseti(L, -2, ret + 1); |
| 8206 | |
| 8207 | lua_pushboolean(L, 1); |
| 8208 | return 1; |
| 8209 | } |
| 8210 | |
| 8211 | /* Remove all occurrences of a given header name. */ |
| 8212 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 8213 | { |
| 8214 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8215 | int ret; |
| 8216 | |
| 8217 | /* First argument (self) must be a table */ |
| 8218 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8219 | |
| 8220 | /* Push in the stack the "headers" entry. */ |
| 8221 | ret = lua_getfield(L, 1, "headers"); |
| 8222 | if (ret != LUA_TTABLE) { |
| 8223 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 8224 | WILL_LJMP(lua_error(L)); |
| 8225 | } |
| 8226 | |
| 8227 | lua_pushstring(L, name); |
| 8228 | lua_pushnil(L); |
| 8229 | lua_settable(L, -3); |
| 8230 | |
| 8231 | lua_pushboolean(L, 1); |
| 8232 | return 1; |
| 8233 | } |
| 8234 | |
| 8235 | /* Set the reply's body. Overwrite any existing entry. */ |
| 8236 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 8237 | { |
| 8238 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8239 | |
| 8240 | /* First argument (self) must be a table */ |
| 8241 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8242 | |
| 8243 | lua_pushstring(L, payload); |
| 8244 | lua_setfield(L, 1, "body"); |
| 8245 | |
| 8246 | lua_pushboolean(L, 1); |
| 8247 | return 1; |
| 8248 | } |
| 8249 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8250 | __LJMP static int hlua_log(lua_State *L) |
| 8251 | { |
| 8252 | int level; |
| 8253 | const char *msg; |
| 8254 | |
| 8255 | MAY_LJMP(check_args(L, 2, "log")); |
| 8256 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 8257 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8258 | |
| 8259 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 8260 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 8261 | |
| 8262 | hlua_sendlog(NULL, level, msg); |
| 8263 | return 0; |
| 8264 | } |
| 8265 | |
| 8266 | __LJMP static int hlua_log_debug(lua_State *L) |
| 8267 | { |
| 8268 | const char *msg; |
| 8269 | |
| 8270 | MAY_LJMP(check_args(L, 1, "debug")); |
| 8271 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8272 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 8273 | return 0; |
| 8274 | } |
| 8275 | |
| 8276 | __LJMP static int hlua_log_info(lua_State *L) |
| 8277 | { |
| 8278 | const char *msg; |
| 8279 | |
| 8280 | MAY_LJMP(check_args(L, 1, "info")); |
| 8281 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8282 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 8283 | return 0; |
| 8284 | } |
| 8285 | |
| 8286 | __LJMP static int hlua_log_warning(lua_State *L) |
| 8287 | { |
| 8288 | const char *msg; |
| 8289 | |
| 8290 | MAY_LJMP(check_args(L, 1, "warning")); |
| 8291 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8292 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 8293 | return 0; |
| 8294 | } |
| 8295 | |
| 8296 | __LJMP static int hlua_log_alert(lua_State *L) |
| 8297 | { |
| 8298 | const char *msg; |
| 8299 | |
| 8300 | MAY_LJMP(check_args(L, 1, "alert")); |
| 8301 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8302 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8303 | return 0; |
| 8304 | } |
| 8305 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8306 | __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] | 8307 | { |
| 8308 | int wakeup_ms = lua_tointeger(L, -1); |
Willy Tarreau | 12c0270 | 2021-09-30 16:12:31 +0200 | [diff] [blame] | 8309 | if (!tick_is_expired(wakeup_ms, now_ms)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8310 | 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] | 8311 | return 0; |
| 8312 | } |
| 8313 | |
| 8314 | __LJMP static int hlua_sleep(lua_State *L) |
| 8315 | { |
| 8316 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8317 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8318 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8319 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8320 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8321 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8322 | wakeup_ms = tick_add(now_ms, delay); |
| 8323 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8324 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8325 | 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] | 8326 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8327 | } |
| 8328 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8329 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8330 | { |
| 8331 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8332 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8333 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8334 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8335 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8336 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8337 | wakeup_ms = tick_add(now_ms, delay); |
| 8338 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8339 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8340 | 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] | 8341 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8342 | } |
| 8343 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8344 | /* This functionis an LUA binding. it permits to give back |
| 8345 | * the hand at the HAProxy scheduler. It is used when the |
| 8346 | * LUA processing consumes a lot of time. |
| 8347 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8348 | __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] | 8349 | { |
| 8350 | return 0; |
| 8351 | } |
| 8352 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8353 | __LJMP static int hlua_yield(lua_State *L) |
| 8354 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8355 | 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] | 8356 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8357 | } |
| 8358 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8359 | /* This function change the nice of the currently executed |
| 8360 | * task. It is used set low or high priority at the current |
| 8361 | * task. |
| 8362 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8363 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8364 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8365 | struct hlua *hlua; |
| 8366 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8367 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8368 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8369 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8370 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8371 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 8372 | hlua = hlua_gethlua(L); |
| 8373 | |
| 8374 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8375 | if (!hlua || !hlua->task) |
| 8376 | return 0; |
| 8377 | |
| 8378 | if (nice < -1024) |
| 8379 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8380 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8381 | nice = 1024; |
| 8382 | |
| 8383 | hlua->task->nice = nice; |
| 8384 | return 0; |
| 8385 | } |
| 8386 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8387 | /* 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] | 8388 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 8389 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 8390 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8391 | * |
| 8392 | * Task wrapper are longjmp safe because the only one Lua code |
| 8393 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8394 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 8395 | 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] | 8396 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 8397 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8398 | enum hlua_exec status; |
| 8399 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 8400 | if (task->thread_mask == MAX_THREADS_MASK) |
| 8401 | task_set_affinity(task, tid_bit); |
| 8402 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8403 | /* If it is the first call to the task, we must initialize the |
| 8404 | * execution timeouts. |
| 8405 | */ |
| 8406 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 8407 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8408 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8409 | /* Execute the Lua code. */ |
| 8410 | status = hlua_ctx_resume(hlua, 1); |
| 8411 | |
| 8412 | switch (status) { |
| 8413 | /* finished or yield */ |
| 8414 | case HLUA_E_OK: |
| 8415 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8416 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 8417 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8418 | break; |
| 8419 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 8420 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 8421 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 8422 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8423 | break; |
| 8424 | |
| 8425 | /* finished with error. */ |
| 8426 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8427 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8428 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8429 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8430 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8431 | break; |
| 8432 | |
| 8433 | case HLUA_E_ERR: |
| 8434 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8435 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8436 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8437 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8438 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8439 | break; |
| 8440 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8441 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8442 | } |
| 8443 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8444 | /* This function is an LUA binding that register LUA function to be |
| 8445 | * executed after the HAProxy configuration parsing and before the |
| 8446 | * HAProxy scheduler starts. This function expect only one LUA |
| 8447 | * argument that is a function. This function returns nothing, but |
| 8448 | * throws if an error is encountered. |
| 8449 | */ |
| 8450 | __LJMP static int hlua_register_init(lua_State *L) |
| 8451 | { |
| 8452 | struct hlua_init_function *init; |
| 8453 | int ref; |
| 8454 | |
| 8455 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 8456 | |
| 8457 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8458 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8459 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8460 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8461 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8462 | |
| 8463 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 8464 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8465 | return 0; |
| 8466 | } |
| 8467 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8468 | /* This functio is an LUA binding. It permits to register a task |
| 8469 | * executed in parallel of the main HAroxy activity. The task is |
| 8470 | * created and it is set in the HAProxy scheduler. It can be called |
| 8471 | * from the "init" section, "post init" or during the runtime. |
| 8472 | * |
| 8473 | * Lua prototype: |
| 8474 | * |
| 8475 | * <none> core.register_task(<function>) |
| 8476 | */ |
| 8477 | static int hlua_register_task(lua_State *L) |
| 8478 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8479 | struct hlua *hlua = NULL; |
| 8480 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8481 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8482 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8483 | |
| 8484 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 8485 | |
| 8486 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8487 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8488 | /* Get the reference state. If the reference is NULL, L is the master |
| 8489 | * state, otherwise hlua->T is. |
| 8490 | */ |
| 8491 | hlua = hlua_gethlua(L); |
| 8492 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8493 | /* we are in runtime processing */ |
| 8494 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8495 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8496 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8497 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8498 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8499 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8500 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8501 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8502 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8503 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8504 | /* We are in the common lua state, execute the task anywhere, |
| 8505 | * otherwise, inherit the current thread identifier |
| 8506 | */ |
| 8507 | if (state_id == 0) |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8508 | task = task_new_anywhere(); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8509 | else |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8510 | task = task_new_here(); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8511 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8512 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8513 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8514 | task->context = hlua; |
| 8515 | task->process = hlua_process_task; |
| 8516 | |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8517 | if (!hlua_ctx_init(hlua, state_id, task, 1)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8518 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8519 | |
| 8520 | /* Restore the function in the stack. */ |
| 8521 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 8522 | hlua->nargs = 0; |
| 8523 | |
| 8524 | /* Schedule task. */ |
Willy Tarreau | e3957f8 | 2021-09-30 16:17:37 +0200 | [diff] [blame] | 8525 | task_wakeup(task, TASK_WOKEN_INIT); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8526 | |
| 8527 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8528 | |
| 8529 | alloc_error: |
| 8530 | task_destroy(task); |
| 8531 | hlua_ctx_destroy(hlua); |
| 8532 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8533 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8534 | } |
| 8535 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8536 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 8537 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 8538 | * resume converters. |
| 8539 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8540 | 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] | 8541 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8542 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8543 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8544 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8545 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8546 | if (!stream) |
| 8547 | return 0; |
| 8548 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8549 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8550 | * Lua context can be not initialized. This behavior |
| 8551 | * permits to save performances because a systematic |
| 8552 | * Lua initialization cause 5% performances loss. |
| 8553 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8554 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8555 | struct hlua *hlua; |
| 8556 | |
| 8557 | hlua = pool_alloc(pool_head_hlua); |
| 8558 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8559 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8560 | return 0; |
| 8561 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8562 | HLUA_INIT(hlua); |
| 8563 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8564 | 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] | 8565 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8566 | return 0; |
| 8567 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8568 | } |
| 8569 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8570 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8571 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8572 | |
| 8573 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8574 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8575 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8576 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8577 | else |
| 8578 | error = "critical error"; |
| 8579 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8580 | return 0; |
| 8581 | } |
| 8582 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8583 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8584 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8585 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8586 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8587 | return 0; |
| 8588 | } |
| 8589 | |
| 8590 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8591 | 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] | 8592 | |
| 8593 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8594 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8595 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8596 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8597 | return 0; |
| 8598 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8599 | hlua_smp2lua(stream->hlua->T, smp); |
| 8600 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8601 | |
| 8602 | /* push keywords in the stack. */ |
| 8603 | if (arg_p) { |
| 8604 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8605 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8606 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8607 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8608 | return 0; |
| 8609 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8610 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8611 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8612 | } |
| 8613 | } |
| 8614 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8615 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8616 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8617 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8618 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8619 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8620 | } |
| 8621 | |
| 8622 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8623 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8624 | /* finished. */ |
| 8625 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8626 | /* If the stack is empty, the function fails. */ |
| 8627 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8628 | return 0; |
| 8629 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8630 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8631 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8632 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8633 | return 1; |
| 8634 | |
| 8635 | /* yield. */ |
| 8636 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8637 | 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] | 8638 | return 0; |
| 8639 | |
| 8640 | /* finished with error. */ |
| 8641 | case HLUA_E_ERRMSG: |
| 8642 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8643 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8644 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8645 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8646 | return 0; |
| 8647 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8648 | case HLUA_E_ETMOUT: |
| 8649 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 8650 | return 0; |
| 8651 | |
| 8652 | case HLUA_E_NOMEM: |
| 8653 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 8654 | return 0; |
| 8655 | |
| 8656 | case HLUA_E_YIELD: |
| 8657 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 8658 | return 0; |
| 8659 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8660 | case HLUA_E_ERR: |
| 8661 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8662 | 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] | 8663 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8664 | |
| 8665 | default: |
| 8666 | return 0; |
| 8667 | } |
| 8668 | } |
| 8669 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8670 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 8671 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8672 | * resume sample-fetches. This function will be called by the sample |
| 8673 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8674 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8675 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 8676 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8677 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8678 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8679 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8680 | const char *error; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 8681 | unsigned int hflags = HLUA_TXN_NOTERM | HLUA_TXN_SMP_CTX; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8682 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8683 | if (!stream) |
| 8684 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8685 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8686 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8687 | * Lua context can be not initialized. This behavior |
| 8688 | * permits to save performances because a systematic |
| 8689 | * Lua initialization cause 5% performances loss. |
| 8690 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8691 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8692 | struct hlua *hlua; |
| 8693 | |
| 8694 | hlua = pool_alloc(pool_head_hlua); |
| 8695 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8696 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8697 | return 0; |
| 8698 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8699 | hlua->T = NULL; |
| 8700 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8701 | 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] | 8702 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8703 | return 0; |
| 8704 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8705 | } |
| 8706 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8707 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8708 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8709 | |
| 8710 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8711 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8712 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8713 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8714 | else |
| 8715 | error = "critical error"; |
| 8716 | 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] | 8717 | return 0; |
| 8718 | } |
| 8719 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8720 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8721 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8722 | SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8723 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8724 | return 0; |
| 8725 | } |
| 8726 | |
| 8727 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8728 | 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] | 8729 | |
| 8730 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 8731 | 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] | 8732 | 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] | 8733 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8734 | return 0; |
| 8735 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8736 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8737 | |
| 8738 | /* push keywords in the stack. */ |
| 8739 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 8740 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8741 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8742 | 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] | 8743 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8744 | return 0; |
| 8745 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8746 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8747 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8748 | } |
| 8749 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8750 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8751 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8752 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8753 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8754 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8755 | } |
| 8756 | |
| 8757 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8758 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8759 | /* finished. */ |
| 8760 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8761 | /* If the stack is empty, the function fails. */ |
| 8762 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8763 | return 0; |
| 8764 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8765 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8766 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8767 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8768 | |
| 8769 | /* Set the end of execution flag. */ |
| 8770 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 8771 | return 1; |
| 8772 | |
| 8773 | /* yield. */ |
| 8774 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8775 | 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] | 8776 | return 0; |
| 8777 | |
| 8778 | /* finished with error. */ |
| 8779 | case HLUA_E_ERRMSG: |
| 8780 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8781 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8782 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8783 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8784 | return 0; |
| 8785 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8786 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8787 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 8788 | return 0; |
| 8789 | |
| 8790 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8791 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 8792 | return 0; |
| 8793 | |
| 8794 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8795 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 8796 | return 0; |
| 8797 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8798 | case HLUA_E_ERR: |
| 8799 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8800 | 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] | 8801 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8802 | |
| 8803 | default: |
| 8804 | return 0; |
| 8805 | } |
| 8806 | } |
| 8807 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8808 | /* This function is an LUA binding used for registering |
| 8809 | * "sample-conv" functions. It expects a converter name used |
| 8810 | * in the haproxy configuration file, and an LUA function. |
| 8811 | */ |
| 8812 | __LJMP static int hlua_register_converters(lua_State *L) |
| 8813 | { |
| 8814 | struct sample_conv_kw_list *sck; |
| 8815 | const char *name; |
| 8816 | int ref; |
| 8817 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8818 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8819 | struct sample_conv *sc; |
| 8820 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8821 | |
| 8822 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 8823 | |
| 8824 | /* First argument : converter name. */ |
| 8825 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8826 | |
| 8827 | /* Second argument : lua function. */ |
| 8828 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 8829 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8830 | /* Check if the converter is already registered */ |
| 8831 | trash = get_trash_chunk(); |
| 8832 | chunk_printf(trash, "lua.%s", name); |
| 8833 | sc = find_sample_conv(trash->area, trash->data); |
| 8834 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8835 | fcn = sc->private; |
| 8836 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8837 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 8838 | "This will become a hard error in version 2.5.\n", name); |
| 8839 | } |
| 8840 | fcn->function_ref[hlua_state_id] = ref; |
| 8841 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8842 | } |
| 8843 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8844 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8845 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8846 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8847 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8848 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8849 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8850 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8851 | |
| 8852 | /* Fill fcn. */ |
| 8853 | fcn->name = strdup(name); |
| 8854 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8855 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8856 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8857 | |
| 8858 | /* List head */ |
| 8859 | sck->list.n = sck->list.p = NULL; |
| 8860 | |
| 8861 | /* converter keyword. */ |
| 8862 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8863 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8864 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8865 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8866 | |
| 8867 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 8868 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 8869 | 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] | 8870 | sck->kw[0].val_args = NULL; |
| 8871 | sck->kw[0].in_type = SMP_T_STR; |
| 8872 | sck->kw[0].out_type = SMP_T_STR; |
| 8873 | sck->kw[0].private = fcn; |
| 8874 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8875 | /* Register this new converter */ |
| 8876 | sample_register_convs(sck); |
| 8877 | |
| 8878 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8879 | |
| 8880 | alloc_error: |
| 8881 | release_hlua_function(fcn); |
| 8882 | ha_free(&sck); |
| 8883 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8884 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8885 | } |
| 8886 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8887 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8888 | * "sample-fetch" functions. It expects a converter name used |
| 8889 | * in the haproxy configuration file, and an LUA function. |
| 8890 | */ |
| 8891 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 8892 | { |
| 8893 | const char *name; |
| 8894 | int ref; |
| 8895 | int len; |
| 8896 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8897 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8898 | struct sample_fetch *sf; |
| 8899 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8900 | |
| 8901 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 8902 | |
| 8903 | /* First argument : sample-fetch name. */ |
| 8904 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8905 | |
| 8906 | /* Second argument : lua function. */ |
| 8907 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 8908 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8909 | /* Check if the sample-fetch is already registered */ |
| 8910 | trash = get_trash_chunk(); |
| 8911 | chunk_printf(trash, "lua.%s", name); |
| 8912 | sf = find_sample_fetch(trash->area, trash->data); |
| 8913 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8914 | fcn = sf->private; |
| 8915 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8916 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 8917 | "This will become a hard error in version 2.5.\n", name); |
| 8918 | } |
| 8919 | fcn->function_ref[hlua_state_id] = ref; |
| 8920 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8921 | } |
| 8922 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8923 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8924 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8925 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8926 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8927 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8928 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8929 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8930 | |
| 8931 | /* Fill fcn. */ |
| 8932 | fcn->name = strdup(name); |
| 8933 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8934 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8935 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8936 | |
| 8937 | /* List head */ |
| 8938 | sfk->list.n = sfk->list.p = NULL; |
| 8939 | |
| 8940 | /* sample-fetch keyword. */ |
| 8941 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8942 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8943 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8944 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8945 | |
| 8946 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 8947 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 8948 | 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] | 8949 | sfk->kw[0].val_args = NULL; |
| 8950 | sfk->kw[0].out_type = SMP_T_STR; |
| 8951 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 8952 | sfk->kw[0].val = 0; |
| 8953 | sfk->kw[0].private = fcn; |
| 8954 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8955 | /* Register this new fetch. */ |
| 8956 | sample_register_fetches(sfk); |
| 8957 | |
| 8958 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8959 | |
| 8960 | alloc_error: |
| 8961 | release_hlua_function(fcn); |
| 8962 | ha_free(&sfk); |
| 8963 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8964 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8965 | } |
| 8966 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 8967 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8968 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 8969 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8970 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8971 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8972 | unsigned int delay; |
| 8973 | unsigned int wakeup_ms; |
| 8974 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8975 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 8976 | hlua = hlua_gethlua(L); |
| 8977 | if (!hlua) { |
| 8978 | return 0; |
| 8979 | } |
| 8980 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8981 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 8982 | |
| 8983 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 8984 | wakeup_ms = tick_add(now_ms, delay); |
| 8985 | hlua->wake_time = wakeup_ms; |
| 8986 | return 0; |
| 8987 | } |
| 8988 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8989 | /* This function is a wrapper to execute each LUA function declared as an action |
| 8990 | * wrapper during the initialisation period. This function may return any |
| 8991 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 8992 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 8993 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8994 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 8995 | 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] | 8996 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8997 | { |
| 8998 | char **arg; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 8999 | unsigned int hflags = HLUA_TXN_ACT_CTX; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9000 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9001 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9002 | |
| 9003 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 9004 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 9005 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 9006 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 9007 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9008 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9009 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9010 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9011 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9012 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9013 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9014 | * Lua context can be not initialized. This behavior |
| 9015 | * permits to save performances because a systematic |
| 9016 | * Lua initialization cause 5% performances loss. |
| 9017 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9018 | if (!s->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9019 | struct hlua *hlua; |
| 9020 | |
| 9021 | hlua = pool_alloc(pool_head_hlua); |
| 9022 | if (!hlua) { |
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 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9027 | HLUA_INIT(hlua); |
| 9028 | s->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9029 | 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] | 9030 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9031 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9032 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9033 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9034 | } |
| 9035 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9036 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9037 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9038 | |
| 9039 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9040 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9041 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 9042 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9043 | else |
| 9044 | error = "critical error"; |
| 9045 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9046 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9047 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9048 | } |
| 9049 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9050 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9051 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9052 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9053 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9054 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9055 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9056 | } |
| 9057 | |
| 9058 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9059 | 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] | 9060 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9061 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 9062 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9063 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9064 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9065 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9066 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9067 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9068 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9069 | |
| 9070 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9071 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9072 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9073 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9074 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9075 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9076 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9077 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9078 | lua_pushstring(s->hlua->T, *arg); |
| 9079 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9080 | } |
| 9081 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9082 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9083 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9084 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9085 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9086 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9087 | } |
| 9088 | |
| 9089 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 9090 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9091 | /* finished. */ |
| 9092 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9093 | /* Catch the return value */ |
| 9094 | if (lua_gettop(s->hlua->T) > 0) |
| 9095 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9096 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9097 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 9098 | if (act_ret == ACT_RET_YIELD) { |
| 9099 | if (flags & ACT_OPT_FINAL) |
| 9100 | goto err_yield; |
| 9101 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9102 | if (dir == SMP_OPT_DIR_REQ) |
| 9103 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 9104 | s->hlua->wake_time); |
| 9105 | else |
| 9106 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 9107 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9108 | } |
| 9109 | goto end; |
| 9110 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9111 | /* yield. */ |
| 9112 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 9113 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9114 | if (dir == SMP_OPT_DIR_REQ) |
| 9115 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 9116 | s->hlua->wake_time); |
| 9117 | else |
| 9118 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 9119 | s->hlua->wake_time); |
| 9120 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9121 | /* Some actions can be wake up when a "write" event |
| 9122 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9123 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9124 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 9125 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9126 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9127 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9128 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9129 | act_ret = ACT_RET_YIELD; |
| 9130 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9131 | |
| 9132 | /* finished with error. */ |
| 9133 | case HLUA_E_ERRMSG: |
| 9134 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9135 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9136 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9137 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9138 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9139 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9140 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9141 | 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] | 9142 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9143 | |
| 9144 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9145 | 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] | 9146 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9147 | |
| 9148 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 9149 | err_yield: |
| 9150 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9151 | 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] | 9152 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9153 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9154 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9155 | case HLUA_E_ERR: |
| 9156 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9157 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9158 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9159 | |
| 9160 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9161 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9162 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9163 | |
| 9164 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 9165 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9166 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9167 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9168 | } |
| 9169 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 9170 | 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] | 9171 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 9172 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9173 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9174 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 9175 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 9176 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9177 | } |
| 9178 | |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9179 | static int hlua_applet_tcp_init(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9180 | { |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 9181 | struct conn_stream *cs = ctx->owner; |
| 9182 | struct stream *strm = __cs_strm(cs); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9183 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9184 | struct task *task; |
| 9185 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9186 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9187 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9188 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9189 | if (!hlua) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9190 | SEND_ERR(strm->be, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9191 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9192 | return 0; |
| 9193 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9194 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9195 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9196 | ctx->ctx.hlua_apptcp.flags = 0; |
| 9197 | |
| 9198 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 9199 | task = task_new_here(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9200 | if (!task) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9201 | SEND_ERR(strm->be, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9202 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9203 | return 0; |
| 9204 | } |
| 9205 | task->nice = 0; |
| 9206 | task->context = ctx; |
| 9207 | task->process = hlua_applet_wakeup; |
| 9208 | ctx->ctx.hlua_apptcp.task = task; |
| 9209 | |
| 9210 | /* In the execution wrappers linked with a stream, the |
| 9211 | * Lua context can be not initialized. This behavior |
| 9212 | * permits to save performances because a systematic |
| 9213 | * Lua initialization cause 5% performances loss. |
| 9214 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9215 | 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] | 9216 | 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] | 9217 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9218 | return 0; |
| 9219 | } |
| 9220 | |
| 9221 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 9222 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9223 | |
| 9224 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9225 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9226 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9227 | error = lua_tostring(hlua->T, -1); |
| 9228 | else |
| 9229 | error = "critical error"; |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9230 | SEND_ERR(strm->be, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9231 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9232 | return 0; |
| 9233 | } |
| 9234 | |
| 9235 | /* Check stack available size. */ |
| 9236 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9237 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9238 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9239 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9240 | return 0; |
| 9241 | } |
| 9242 | |
| 9243 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9244 | 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] | 9245 | |
| 9246 | /* Create and and push object stream in the stack. */ |
| 9247 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9248 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9249 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9250 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9251 | return 0; |
| 9252 | } |
| 9253 | hlua->nargs = 1; |
| 9254 | |
| 9255 | /* push keywords in the stack. */ |
| 9256 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 9257 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9258 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9259 | ctx->rule->arg.hlua_rule->fcn->name); |
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 | return 0; |
| 9262 | } |
| 9263 | lua_pushstring(hlua->T, *arg); |
| 9264 | hlua->nargs++; |
| 9265 | } |
| 9266 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9267 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9268 | |
| 9269 | /* Wakeup the applet ASAP. */ |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 9270 | cs_cant_get(cs); |
| 9271 | cs_rx_endp_more(cs); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9272 | |
| 9273 | return 1; |
| 9274 | } |
| 9275 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 9276 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9277 | { |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 9278 | struct conn_stream *cs = ctx->owner; |
| 9279 | struct stream *strm = __cs_strm(cs); |
| 9280 | struct channel *res = cs_ic(cs); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9281 | struct act_rule *rule = ctx->rule; |
| 9282 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9283 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9284 | |
| 9285 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 9286 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 9287 | /* eat the whole request */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 9288 | co_skip(cs_oc(cs), co_data(cs_oc(cs))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9289 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 9290 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9291 | |
| 9292 | /* If the stream is disconnect or closed, ldo nothing. */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 9293 | if (unlikely(cs->state == CS_ST_DIS || cs->state == CS_ST_CLO)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9294 | return; |
| 9295 | |
| 9296 | /* Execute the function. */ |
| 9297 | switch (hlua_ctx_resume(hlua, 1)) { |
| 9298 | /* finished. */ |
| 9299 | case HLUA_E_OK: |
| 9300 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 9301 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9302 | /* eat the whole request */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 9303 | co_skip(cs_oc(cs), co_data(cs_oc(cs))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9304 | res->flags |= CF_READ_NULL; |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame] | 9305 | cs_shutr(cs); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9306 | return; |
| 9307 | |
| 9308 | /* yield. */ |
| 9309 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 9310 | if (hlua->wake_time != TICK_ETERNITY) |
| 9311 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9312 | return; |
| 9313 | |
| 9314 | /* finished with error. */ |
| 9315 | case HLUA_E_ERRMSG: |
| 9316 | /* Display log. */ |
| 9317 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9318 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9319 | lua_pop(hlua->T, 1); |
| 9320 | goto error; |
| 9321 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9322 | case HLUA_E_ETMOUT: |
| 9323 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9324 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9325 | goto error; |
| 9326 | |
| 9327 | case HLUA_E_NOMEM: |
| 9328 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9329 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9330 | goto error; |
| 9331 | |
| 9332 | case HLUA_E_YIELD: /* unexpected */ |
| 9333 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9334 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9335 | goto error; |
| 9336 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9337 | case HLUA_E_ERR: |
| 9338 | /* Display log. */ |
| 9339 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9340 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9341 | goto error; |
| 9342 | |
| 9343 | default: |
| 9344 | goto error; |
| 9345 | } |
| 9346 | |
| 9347 | error: |
| 9348 | |
| 9349 | /* For all other cases, just close the stream. */ |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame] | 9350 | cs_shutw(cs); |
| 9351 | cs_shutr(cs); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9352 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 9353 | } |
| 9354 | |
| 9355 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 9356 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 9357 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9358 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9359 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9360 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9361 | } |
| 9362 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9363 | /* The function returns 1 if the initialisation is complete, 0 if |
| 9364 | * an errors occurs and -1 if more data are required for initializing |
| 9365 | * the applet. |
| 9366 | */ |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9367 | static int hlua_applet_http_init(struct appctx *ctx) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9368 | { |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 9369 | struct conn_stream *cs = ctx->owner; |
| 9370 | struct stream *strm = __cs_strm(cs); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9371 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9372 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9373 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9374 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9375 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9376 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9377 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9378 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9379 | if (!hlua) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9380 | SEND_ERR(strm->be, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9381 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9382 | return 0; |
| 9383 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9384 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9385 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9386 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 9387 | ctx->ctx.hlua_apphttp.flags = 0; |
| 9388 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 9389 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 9390 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 9391 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9392 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 9393 | task = task_new_here(); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9394 | if (!task) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9395 | SEND_ERR(strm->be, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9396 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9397 | return 0; |
| 9398 | } |
| 9399 | task->nice = 0; |
| 9400 | task->context = ctx; |
| 9401 | task->process = hlua_applet_wakeup; |
| 9402 | ctx->ctx.hlua_apphttp.task = task; |
| 9403 | |
| 9404 | /* In the execution wrappers linked with a stream, the |
| 9405 | * Lua context can be not initialized. This behavior |
| 9406 | * permits to save performances because a systematic |
| 9407 | * Lua initialization cause 5% performances loss. |
| 9408 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9409 | 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] | 9410 | 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] | 9411 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9412 | return 0; |
| 9413 | } |
| 9414 | |
| 9415 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 9416 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9417 | |
| 9418 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9419 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9420 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9421 | error = lua_tostring(hlua->T, -1); |
| 9422 | else |
| 9423 | error = "critical error"; |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9424 | SEND_ERR(strm->be, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9425 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9426 | return 0; |
| 9427 | } |
| 9428 | |
| 9429 | /* Check stack available size. */ |
| 9430 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9431 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9432 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9433 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9434 | return 0; |
| 9435 | } |
| 9436 | |
| 9437 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9438 | 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] | 9439 | |
| 9440 | /* Create and and push object stream in the stack. */ |
| 9441 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9442 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9443 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9444 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9445 | return 0; |
| 9446 | } |
| 9447 | hlua->nargs = 1; |
| 9448 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9449 | /* push keywords in the stack. */ |
| 9450 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 9451 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9452 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9453 | ctx->rule->arg.hlua_rule->fcn->name); |
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 | return 0; |
| 9456 | } |
| 9457 | lua_pushstring(hlua->T, *arg); |
| 9458 | hlua->nargs++; |
| 9459 | } |
| 9460 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9461 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9462 | |
| 9463 | /* Wakeup the applet when data is ready for read. */ |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 9464 | cs_cant_get(cs); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9465 | |
| 9466 | return 1; |
| 9467 | } |
| 9468 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 9469 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9470 | { |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 9471 | struct conn_stream *cs = ctx->owner; |
| 9472 | struct stream *strm = __cs_strm(cs); |
| 9473 | struct channel *req = cs_oc(cs); |
| 9474 | struct channel *res = cs_ic(cs); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9475 | struct act_rule *rule = ctx->rule; |
| 9476 | struct proxy *px = strm->be; |
| 9477 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 9478 | struct htx *req_htx, *res_htx; |
| 9479 | |
| 9480 | res_htx = htx_from_buf(&res->buf); |
| 9481 | |
| 9482 | /* If the stream is disconnect or closed, ldo nothing. */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 9483 | if (unlikely(cs->state == CS_ST_DIS || cs->state == CS_ST_CLO)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9484 | goto out; |
| 9485 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9486 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9487 | if (!b_size(&res->buf)) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 9488 | cs_rx_room_blk(cs); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9489 | goto out; |
| 9490 | } |
| 9491 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9492 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9493 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 9494 | |
| 9495 | /* Set the currently running flag. */ |
| 9496 | if (!HLUA_IS_RUNNING(hlua) && |
| 9497 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 9498 | if (!co_data(req)) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 9499 | cs_cant_get(cs); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9500 | goto out; |
| 9501 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9502 | } |
| 9503 | |
| 9504 | /* Executes The applet if it is not done. */ |
| 9505 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 9506 | |
| 9507 | /* Execute the function. */ |
| 9508 | switch (hlua_ctx_resume(hlua, 1)) { |
| 9509 | /* finished. */ |
| 9510 | case HLUA_E_OK: |
| 9511 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 9512 | break; |
| 9513 | |
| 9514 | /* yield. */ |
| 9515 | case HLUA_E_AGAIN: |
| 9516 | if (hlua->wake_time != TICK_ETERNITY) |
| 9517 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9518 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9519 | |
| 9520 | /* finished with error. */ |
| 9521 | case HLUA_E_ERRMSG: |
| 9522 | /* Display log. */ |
| 9523 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9524 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9525 | lua_pop(hlua->T, 1); |
| 9526 | goto error; |
| 9527 | |
| 9528 | case HLUA_E_ETMOUT: |
| 9529 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9530 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9531 | goto error; |
| 9532 | |
| 9533 | case HLUA_E_NOMEM: |
| 9534 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9535 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9536 | goto error; |
| 9537 | |
| 9538 | case HLUA_E_YIELD: /* unexpected */ |
| 9539 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9540 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9541 | goto error; |
| 9542 | |
| 9543 | case HLUA_E_ERR: |
| 9544 | /* Display log. */ |
| 9545 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9546 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9547 | goto error; |
| 9548 | |
| 9549 | default: |
| 9550 | goto error; |
| 9551 | } |
| 9552 | } |
| 9553 | |
| 9554 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9555 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 9556 | goto done; |
| 9557 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9558 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 9559 | goto error; |
| 9560 | |
Christopher Faulet | f89af9c | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 9561 | /* no more data are expected. If the response buffer is empty |
| 9562 | * for a chunked message, be sure to add something (EOT block in |
| 9563 | * this case) to have something to send. It is important to be |
| 9564 | * sure the EOM flags will be handled by the endpoint. |
| 9565 | */ |
| 9566 | if (htx_is_empty(res_htx) && (strm->txn->rsp.flags & (HTTP_MSGF_XFER_LEN|HTTP_MSGF_CNT_LEN)) == HTTP_MSGF_XFER_LEN) { |
| 9567 | if (!htx_add_endof(res_htx, HTX_BLK_EOT)) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 9568 | cs_rx_room_blk(cs); |
Christopher Faulet | f89af9c | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 9569 | goto out; |
| 9570 | } |
| 9571 | channel_add_input(res, 1); |
| 9572 | } |
| 9573 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 9574 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 9575 | cs->endp->flags |= CS_EP_EOI; |
Christopher Faulet | d8d2708 | 2022-03-07 15:50:54 +0100 | [diff] [blame] | 9576 | res->flags |= CF_EOI; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9577 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 9578 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9579 | } |
| 9580 | |
| 9581 | done: |
| 9582 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9583 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9584 | res->flags |= CF_READ_NULL; |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame] | 9585 | cs_shutr(cs); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9586 | } |
| 9587 | |
| 9588 | /* eat the whole request */ |
| 9589 | if (co_data(req)) { |
| 9590 | req_htx = htx_from_buf(&req->buf); |
| 9591 | co_htx_skip(req, req_htx, co_data(req)); |
| 9592 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9593 | } |
| 9594 | } |
| 9595 | |
| 9596 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9597 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9598 | return; |
| 9599 | |
| 9600 | error: |
| 9601 | |
| 9602 | /* If we are in HTTP mode, and we are not send any |
| 9603 | * data, return a 500 server error in best effort: |
| 9604 | * if there is no room available in the buffer, |
| 9605 | * just close the connection. |
| 9606 | */ |
| 9607 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 9608 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9609 | |
| 9610 | channel_erase(res); |
| 9611 | res->buf.data = b_data(err); |
| 9612 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 9613 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 9614 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9615 | } |
| 9616 | if (!(strm->flags & SF_ERR_MASK)) |
| 9617 | strm->flags |= SF_ERR_RESOURCE; |
| 9618 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 9619 | goto done; |
| 9620 | } |
| 9621 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9622 | static void hlua_applet_http_release(struct appctx *ctx) |
| 9623 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 9624 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9625 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9626 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9627 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9628 | } |
| 9629 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9630 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9631 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9632 | * |
| 9633 | * This function can fail with an abort() due to an Lua critical error. |
| 9634 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 9635 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9636 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9637 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 9638 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9639 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9640 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9641 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9642 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9643 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9644 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9645 | if (!rule->arg.hlua_rule) { |
| 9646 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9647 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9648 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9649 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9650 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 9651 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 9652 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9653 | if (!rule->arg.hlua_rule->args) { |
| 9654 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9655 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9656 | } |
| 9657 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9658 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9659 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9660 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9661 | /* Expect some arguments */ |
| 9662 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9663 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9664 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9665 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9666 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9667 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9668 | if (!rule->arg.hlua_rule->args[i]) { |
| 9669 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9670 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9671 | } |
| 9672 | (*cur_arg)++; |
| 9673 | } |
| 9674 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9675 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 9676 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9677 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9678 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9679 | |
| 9680 | error: |
| 9681 | if (rule->arg.hlua_rule) { |
| 9682 | if (rule->arg.hlua_rule->args) { |
| 9683 | for (i = 0; i < fcn->nargs; i++) |
| 9684 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 9685 | ha_free(&rule->arg.hlua_rule->args); |
| 9686 | } |
| 9687 | ha_free(&rule->arg.hlua_rule); |
| 9688 | } |
| 9689 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9690 | } |
| 9691 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9692 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 9693 | struct act_rule *rule, char **err) |
| 9694 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9695 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9696 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9697 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9698 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9699 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9700 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9701 | * the call of this analyzer. |
| 9702 | */ |
| 9703 | if (rule->from != ACT_F_HTTP_REQ) { |
| 9704 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 9705 | return ACT_RET_PRS_ERR; |
| 9706 | } |
| 9707 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9708 | /* Memory for the rule. */ |
| 9709 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 9710 | if (!rule->arg.hlua_rule) { |
| 9711 | memprintf(err, "out of memory error"); |
| 9712 | return ACT_RET_PRS_ERR; |
| 9713 | } |
| 9714 | |
| 9715 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9716 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9717 | |
| 9718 | /* TODO: later accept arguments. */ |
| 9719 | rule->arg.hlua_rule->args = NULL; |
| 9720 | |
| 9721 | /* Add applet pointer in the rule. */ |
| 9722 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 9723 | rule->applet.name = fcn->name; |
| 9724 | rule->applet.init = hlua_applet_http_init; |
| 9725 | rule->applet.fct = hlua_applet_http_fct; |
| 9726 | rule->applet.release = hlua_applet_http_release; |
| 9727 | rule->applet.timeout = hlua_timeout_applet; |
| 9728 | |
| 9729 | return ACT_RET_PRS_OK; |
| 9730 | } |
| 9731 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9732 | /* This function is an LUA binding used for registering |
| 9733 | * "sample-conv" functions. It expects a converter name used |
| 9734 | * in the haproxy configuration file, and an LUA function. |
| 9735 | */ |
| 9736 | __LJMP static int hlua_register_action(lua_State *L) |
| 9737 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9738 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9739 | const char *name; |
| 9740 | int ref; |
| 9741 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9742 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9743 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9744 | struct buffer *trash; |
| 9745 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9746 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9747 | /* Initialise the number of expected arguments at 0. */ |
| 9748 | nargs = 0; |
| 9749 | |
| 9750 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 9751 | 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] | 9752 | |
| 9753 | /* First argument : converter name. */ |
| 9754 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9755 | |
| 9756 | /* Second argument : environment. */ |
| 9757 | if (lua_type(L, 2) != LUA_TTABLE) |
| 9758 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9759 | |
| 9760 | /* Third argument : lua function. */ |
| 9761 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 9762 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9763 | /* 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] | 9764 | if (lua_gettop(L) >= 4) |
| 9765 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 9766 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9767 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9768 | lua_pushnil(L); |
| 9769 | while (lua_next(L, 2) != 0) { |
| 9770 | if (lua_type(L, -1) != LUA_TSTRING) |
| 9771 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9772 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9773 | /* Check if action exists */ |
| 9774 | trash = get_trash_chunk(); |
| 9775 | chunk_printf(trash, "lua.%s", name); |
| 9776 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 9777 | akw = tcp_req_cont_action(trash->area); |
| 9778 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 9779 | akw = tcp_res_cont_action(trash->area); |
| 9780 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 9781 | akw = action_http_req_custom(trash->area); |
| 9782 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 9783 | akw = action_http_res_custom(trash->area); |
| 9784 | } else { |
| 9785 | akw = NULL; |
| 9786 | } |
| 9787 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9788 | fcn = akw->private; |
| 9789 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9790 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 9791 | "This will become a hard error in version 2.5.\n", name); |
| 9792 | } |
| 9793 | fcn->function_ref[hlua_state_id] = ref; |
| 9794 | |
| 9795 | /* pop the environment string. */ |
| 9796 | lua_pop(L, 1); |
| 9797 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9798 | } |
| 9799 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9800 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 9801 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9802 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9803 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9804 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9805 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9806 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9807 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9808 | |
| 9809 | /* Fill fcn. */ |
| 9810 | fcn->name = strdup(name); |
| 9811 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9812 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9813 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9814 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 9815 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9816 | fcn->nargs = nargs; |
| 9817 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9818 | /* List head */ |
| 9819 | akl->list.n = akl->list.p = NULL; |
| 9820 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9821 | /* action keyword. */ |
| 9822 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9823 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9824 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9825 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9826 | |
| 9827 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 9828 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 9829 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9830 | akl->kw[0].private = fcn; |
| 9831 | akl->kw[0].parse = action_register_lua; |
| 9832 | |
| 9833 | /* select the action registering point. */ |
| 9834 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 9835 | tcp_req_cont_keywords_register(akl); |
| 9836 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 9837 | tcp_res_cont_keywords_register(akl); |
| 9838 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 9839 | http_req_keywords_register(akl); |
| 9840 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 9841 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9842 | else { |
| 9843 | release_hlua_function(fcn); |
| 9844 | if (akl) |
| 9845 | ha_free((char **)&(akl->kw[0].kw)); |
| 9846 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 9847 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9848 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 9849 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9850 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9851 | |
| 9852 | /* pop the environment string. */ |
| 9853 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9854 | |
| 9855 | /* reset for next loop */ |
| 9856 | akl = NULL; |
| 9857 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9858 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9859 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9860 | |
| 9861 | alloc_error: |
| 9862 | release_hlua_function(fcn); |
| 9863 | ha_free(&akl); |
| 9864 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9865 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9866 | } |
| 9867 | |
| 9868 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 9869 | struct act_rule *rule, char **err) |
| 9870 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9871 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9872 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 9873 | if (px->mode == PR_MODE_HTTP) { |
| 9874 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 9875 | return ACT_RET_PRS_ERR; |
| 9876 | } |
| 9877 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9878 | /* Memory for the rule. */ |
| 9879 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 9880 | if (!rule->arg.hlua_rule) { |
| 9881 | memprintf(err, "out of memory error"); |
| 9882 | return ACT_RET_PRS_ERR; |
| 9883 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9884 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9885 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9886 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9887 | |
| 9888 | /* TODO: later accept arguments. */ |
| 9889 | rule->arg.hlua_rule->args = NULL; |
| 9890 | |
| 9891 | /* Add applet pointer in the rule. */ |
| 9892 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 9893 | rule->applet.name = fcn->name; |
| 9894 | rule->applet.init = hlua_applet_tcp_init; |
| 9895 | rule->applet.fct = hlua_applet_tcp_fct; |
| 9896 | rule->applet.release = hlua_applet_tcp_release; |
| 9897 | rule->applet.timeout = hlua_timeout_applet; |
| 9898 | |
| 9899 | return 0; |
| 9900 | } |
| 9901 | |
| 9902 | /* This function is an LUA binding used for registering |
| 9903 | * "sample-conv" functions. It expects a converter name used |
| 9904 | * in the haproxy configuration file, and an LUA function. |
| 9905 | */ |
| 9906 | __LJMP static int hlua_register_service(lua_State *L) |
| 9907 | { |
| 9908 | struct action_kw_list *akl; |
| 9909 | const char *name; |
| 9910 | const char *env; |
| 9911 | int ref; |
| 9912 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9913 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9914 | struct buffer *trash; |
| 9915 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9916 | |
| 9917 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 9918 | |
| 9919 | /* First argument : converter name. */ |
| 9920 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9921 | |
| 9922 | /* Second argument : environment. */ |
| 9923 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 9924 | |
| 9925 | /* Third argument : lua function. */ |
| 9926 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 9927 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9928 | /* Check for service already registered */ |
| 9929 | trash = get_trash_chunk(); |
| 9930 | chunk_printf(trash, "lua.%s", name); |
| 9931 | akw = service_find(trash->area); |
| 9932 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9933 | fcn = akw->private; |
| 9934 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9935 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 9936 | "This will become a hard error in version 2.5.\n", name); |
| 9937 | } |
| 9938 | fcn->function_ref[hlua_state_id] = ref; |
| 9939 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9940 | } |
| 9941 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9942 | /* Allocate and fill the sample fetch keyword struct. */ |
| 9943 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 9944 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9945 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9946 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9947 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9948 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9949 | |
| 9950 | /* Fill fcn. */ |
| 9951 | len = strlen("<lua.>") + strlen(name) + 1; |
| 9952 | fcn->name = calloc(1, len); |
| 9953 | if (!fcn->name) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9954 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9955 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9956 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9957 | |
| 9958 | /* List head */ |
| 9959 | akl->list.n = akl->list.p = NULL; |
| 9960 | |
| 9961 | /* converter keyword. */ |
| 9962 | len = strlen("lua.") + strlen(name) + 1; |
| 9963 | akl->kw[0].kw = calloc(1, len); |
| 9964 | if (!akl->kw[0].kw) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9965 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9966 | |
| 9967 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 9968 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 9969 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9970 | if (strcmp(env, "tcp") == 0) |
| 9971 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9972 | else if (strcmp(env, "http") == 0) |
| 9973 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9974 | else { |
| 9975 | release_hlua_function(fcn); |
| 9976 | if (akl) |
| 9977 | ha_free((char **)&(akl->kw[0].kw)); |
| 9978 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 9979 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 9980 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9981 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9982 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 9983 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9984 | akl->kw[0].private = fcn; |
| 9985 | |
| 9986 | /* End of array. */ |
| 9987 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 9988 | |
| 9989 | /* Register this new converter */ |
| 9990 | service_keywords_register(akl); |
| 9991 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9992 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9993 | |
| 9994 | alloc_error: |
| 9995 | release_hlua_function(fcn); |
| 9996 | ha_free(&akl); |
| 9997 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9998 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9999 | } |
| 10000 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10001 | /* This function initialises Lua cli handler. It copies the |
| 10002 | * arguments in the Lua stack and create channel IO objects. |
| 10003 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 10004 | 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] | 10005 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10006 | struct hlua_cli_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10007 | struct hlua *hlua; |
| 10008 | struct hlua_function *fcn; |
| 10009 | int i; |
| 10010 | const char *error; |
| 10011 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10012 | fcn = private; |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10013 | ctx->fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10014 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10015 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10016 | if (!hlua) { |
| 10017 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10018 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10019 | } |
| 10020 | HLUA_INIT(hlua); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10021 | ctx->hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10022 | |
| 10023 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 10024 | * 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] | 10025 | * applet_http. It is absolutely compatible. |
| 10026 | */ |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10027 | ctx->task = task_new_here(); |
| 10028 | if (!ctx->task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 10029 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10030 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10031 | } |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10032 | ctx->task->nice = 0; |
| 10033 | ctx->task->context = appctx; |
| 10034 | ctx->task->process = hlua_applet_wakeup; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10035 | |
| 10036 | /* Initialises the Lua context */ |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10037 | if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(fcn), ctx->task, 0)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10038 | 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] | 10039 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10040 | } |
| 10041 | |
| 10042 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10043 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10044 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 10045 | error = lua_tostring(hlua->T, -1); |
| 10046 | else |
| 10047 | error = "critical error"; |
| 10048 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 10049 | goto error; |
| 10050 | } |
| 10051 | |
| 10052 | /* Check stack available size. */ |
| 10053 | if (!lua_checkstack(hlua->T, 2)) { |
| 10054 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10055 | goto error; |
| 10056 | } |
| 10057 | |
| 10058 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10059 | 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] | 10060 | |
| 10061 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 10062 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10063 | */ |
| 10064 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 10065 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10066 | goto error; |
| 10067 | } |
| 10068 | hlua->nargs = 1; |
| 10069 | |
| 10070 | /* push keywords in the stack. */ |
| 10071 | for (i = 0; *args[i]; i++) { |
| 10072 | /* Check stack available size. */ |
| 10073 | if (!lua_checkstack(hlua->T, 1)) { |
| 10074 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10075 | goto error; |
| 10076 | } |
| 10077 | lua_pushstring(hlua->T, args[i]); |
| 10078 | hlua->nargs++; |
| 10079 | } |
| 10080 | |
| 10081 | /* We must initialize the execution timeouts. */ |
| 10082 | hlua->max_time = hlua_timeout_session; |
| 10083 | |
| 10084 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10085 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10086 | |
| 10087 | /* It's ok */ |
| 10088 | return 0; |
| 10089 | |
| 10090 | /* It's not ok. */ |
| 10091 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10092 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10093 | hlua_ctx_destroy(hlua); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10094 | ctx->hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10095 | return 1; |
| 10096 | } |
| 10097 | |
| 10098 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 10099 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10100 | struct hlua_cli_ctx *ctx = appctx->svcctx; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10101 | struct hlua *hlua; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 10102 | struct conn_stream *cs; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10103 | struct hlua_function *fcn; |
| 10104 | |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10105 | hlua = ctx->hlua; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 10106 | cs = appctx->owner; |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10107 | fcn = ctx->fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10108 | |
| 10109 | /* If the stream is disconnect or closed, ldo nothing. */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 10110 | 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] | 10111 | return 1; |
| 10112 | |
| 10113 | /* Execute the function. */ |
| 10114 | switch (hlua_ctx_resume(hlua, 1)) { |
| 10115 | |
| 10116 | /* finished. */ |
| 10117 | case HLUA_E_OK: |
| 10118 | return 1; |
| 10119 | |
| 10120 | /* yield. */ |
| 10121 | case HLUA_E_AGAIN: |
| 10122 | /* We want write. */ |
| 10123 | if (HLUA_IS_WAKERESWR(hlua)) |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 10124 | cs_rx_room_blk(cs); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10125 | /* Set the timeout. */ |
| 10126 | if (hlua->wake_time != TICK_ETERNITY) |
| 10127 | task_schedule(hlua->task, hlua->wake_time); |
| 10128 | return 0; |
| 10129 | |
| 10130 | /* finished with error. */ |
| 10131 | case HLUA_E_ERRMSG: |
| 10132 | /* Display log. */ |
| 10133 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 10134 | fcn->name, lua_tostring(hlua->T, -1)); |
| 10135 | lua_pop(hlua->T, 1); |
| 10136 | return 1; |
| 10137 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10138 | case HLUA_E_ETMOUT: |
| 10139 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 10140 | fcn->name); |
| 10141 | return 1; |
| 10142 | |
| 10143 | case HLUA_E_NOMEM: |
| 10144 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 10145 | fcn->name); |
| 10146 | return 1; |
| 10147 | |
| 10148 | case HLUA_E_YIELD: /* unexpected */ |
| 10149 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 10150 | fcn->name); |
| 10151 | return 1; |
| 10152 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10153 | case HLUA_E_ERR: |
| 10154 | /* Display log. */ |
| 10155 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 10156 | fcn->name); |
| 10157 | return 1; |
| 10158 | |
| 10159 | default: |
| 10160 | return 1; |
| 10161 | } |
| 10162 | |
| 10163 | return 1; |
| 10164 | } |
| 10165 | |
| 10166 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 10167 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10168 | struct hlua_cli_ctx *ctx = appctx->svcctx; |
| 10169 | |
| 10170 | hlua_ctx_destroy(ctx->hlua); |
| 10171 | ctx->hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10172 | } |
| 10173 | |
| 10174 | /* This function is an LUA binding used for registering |
| 10175 | * new keywords in the cli. It expects a list of keywords |
| 10176 | * which are the "path". It is limited to 5 keywords. A |
| 10177 | * description of the command, a function to be executed |
| 10178 | * for the parsing and a function for io handlers. |
| 10179 | */ |
| 10180 | __LJMP static int hlua_register_cli(lua_State *L) |
| 10181 | { |
| 10182 | struct cli_kw_list *cli_kws; |
| 10183 | const char *message; |
| 10184 | int ref_io; |
| 10185 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10186 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10187 | int index; |
| 10188 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10189 | struct buffer *trash; |
| 10190 | const char *kw[5]; |
| 10191 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10192 | const char *errmsg; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10193 | |
| 10194 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 10195 | |
| 10196 | /* First argument : an array of maximum 5 keywords. */ |
| 10197 | if (!lua_istable(L, 1)) |
| 10198 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 10199 | |
| 10200 | /* Second argument : string with contextual message. */ |
| 10201 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 10202 | |
| 10203 | /* Third and fourth argument : lua function. */ |
| 10204 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 10205 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10206 | /* Check for CLI service already registered */ |
| 10207 | trash = get_trash_chunk(); |
| 10208 | index = 0; |
| 10209 | lua_pushnil(L); |
| 10210 | memset(kw, 0, sizeof(kw)); |
| 10211 | while (lua_next(L, 1) != 0) { |
| 10212 | if (index >= CLI_PREFIX_KW_NB) |
| 10213 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 10214 | if (lua_type(L, -1) != LUA_TSTRING) |
| 10215 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 10216 | kw[index] = lua_tostring(L, -1); |
| 10217 | if (index == 0) |
| 10218 | chunk_printf(trash, "%s", kw[index]); |
| 10219 | else |
| 10220 | chunk_appendf(trash, " %s", kw[index]); |
| 10221 | index++; |
| 10222 | lua_pop(L, 1); |
| 10223 | } |
| 10224 | cli_kw = cli_find_kw_exact((char **)kw); |
| 10225 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10226 | fcn = cli_kw->private; |
| 10227 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 10228 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 10229 | "This will become a hard error in version 2.5.\n", trash->area); |
| 10230 | } |
| 10231 | fcn->function_ref[hlua_state_id] = ref_io; |
| 10232 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10233 | } |
| 10234 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10235 | /* Allocate and fill the sample fetch keyword struct. */ |
| 10236 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10237 | if (!cli_kws) { |
| 10238 | errmsg = "Lua out of memory error."; |
| 10239 | goto error; |
| 10240 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10241 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10242 | if (!fcn) { |
| 10243 | errmsg = "Lua out of memory error."; |
| 10244 | goto error; |
| 10245 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10246 | |
| 10247 | /* Fill path. */ |
| 10248 | index = 0; |
| 10249 | lua_pushnil(L); |
| 10250 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10251 | if (index >= 5) { |
| 10252 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 10253 | goto error; |
| 10254 | } |
| 10255 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 10256 | errmsg = "1st argument must be a table filled with strings"; |
| 10257 | goto error; |
| 10258 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10259 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10260 | if (!cli_kws->kw[0].str_kw[index]) { |
| 10261 | errmsg = "Lua out of memory error."; |
| 10262 | goto error; |
| 10263 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10264 | index++; |
| 10265 | lua_pop(L, 1); |
| 10266 | } |
| 10267 | |
| 10268 | /* Copy help message. */ |
| 10269 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10270 | if (!cli_kws->kw[0].usage) { |
| 10271 | errmsg = "Lua out of memory error."; |
| 10272 | goto error; |
| 10273 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10274 | |
| 10275 | /* Fill fcn io handler. */ |
| 10276 | len = strlen("<lua.cli>") + 1; |
| 10277 | for (i = 0; i < index; i++) |
| 10278 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 10279 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10280 | if (!fcn->name) { |
| 10281 | errmsg = "Lua out of memory error."; |
| 10282 | goto error; |
| 10283 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10284 | strncat((char *)fcn->name, "<lua.cli", len); |
| 10285 | for (i = 0; i < index; i++) { |
| 10286 | strncat((char *)fcn->name, ".", len); |
| 10287 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 10288 | } |
| 10289 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10290 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10291 | |
| 10292 | /* Fill last entries. */ |
| 10293 | cli_kws->kw[0].private = fcn; |
| 10294 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 10295 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 10296 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 10297 | |
| 10298 | /* Register this new converter */ |
| 10299 | cli_register_kw(cli_kws); |
| 10300 | |
| 10301 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10302 | |
| 10303 | error: |
| 10304 | release_hlua_function(fcn); |
| 10305 | if (cli_kws) { |
| 10306 | for (i = 0; i < index; i++) |
| 10307 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 10308 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 10309 | } |
| 10310 | ha_free(&cli_kws); |
| 10311 | WILL_LJMP(luaL_error(L, errmsg)); |
| 10312 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10313 | } |
| 10314 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10315 | static int hlua_filter_init_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 10316 | { |
| 10317 | struct hlua_flt_config *conf = fconf->conf; |
| 10318 | lua_State *L; |
| 10319 | int error, pos, state_id, flt_ref; |
| 10320 | |
| 10321 | state_id = reg_flt_to_stack_id(conf->reg); |
| 10322 | L = hlua_states[state_id]; |
| 10323 | pos = lua_gettop(L); |
| 10324 | |
| 10325 | /* The filter parsing function */ |
| 10326 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->fun_ref[state_id]); |
| 10327 | |
| 10328 | /* Push the filter class on the stack and resolve all callbacks */ |
| 10329 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->flt_ref[state_id]); |
| 10330 | |
| 10331 | /* Duplicate the filter class so each filter will have its own copy */ |
| 10332 | lua_newtable(L); |
| 10333 | lua_pushnil(L); |
| 10334 | |
| 10335 | while (lua_next(L, pos+2)) { |
| 10336 | lua_pushvalue(L, -2); |
| 10337 | lua_insert(L, -2); |
| 10338 | lua_settable(L, -4); |
| 10339 | } |
| 10340 | flt_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
| 10341 | |
| 10342 | /* Remove the original lua filter class from the stack */ |
| 10343 | lua_pop(L, 1); |
| 10344 | |
| 10345 | /* Push the copy on the stack */ |
| 10346 | lua_rawgeti(L, LUA_REGISTRYINDEX, flt_ref); |
| 10347 | |
| 10348 | /* extra args are pushed in a table */ |
| 10349 | lua_newtable(L); |
| 10350 | for (pos = 0; conf->args[pos]; pos++) { |
| 10351 | /* Check stack available size. */ |
| 10352 | if (!lua_checkstack(L, 1)) { |
| 10353 | ha_alert("Lua filter '%s' : Lua error : full stack.", conf->reg->name); |
| 10354 | goto error; |
| 10355 | } |
| 10356 | lua_pushstring(L, conf->args[pos]); |
| 10357 | lua_rawseti(L, -2, lua_rawlen(L, -2) + 1); |
| 10358 | } |
| 10359 | |
| 10360 | error = lua_pcall(L, 2, LUA_MULTRET, 0); |
| 10361 | switch (error) { |
| 10362 | case LUA_OK: |
| 10363 | /* replace the filter ref */ |
| 10364 | conf->ref[state_id] = flt_ref; |
| 10365 | break; |
| 10366 | case LUA_ERRRUN: |
| 10367 | ha_alert("Lua filter '%s' : runtime error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10368 | goto error; |
| 10369 | case LUA_ERRMEM: |
| 10370 | ha_alert("Lua filter '%s' : out of memory error", conf->reg->name); |
| 10371 | goto error; |
| 10372 | case LUA_ERRERR: |
| 10373 | ha_alert("Lua filter '%s' : message handler error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10374 | goto error; |
| 10375 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
| 10376 | case LUA_ERRGCMM: |
| 10377 | ha_alert("Lua filter '%s' : garbage collector error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10378 | goto error; |
| 10379 | #endif |
| 10380 | default: |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 10381 | 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] | 10382 | goto error; |
| 10383 | } |
| 10384 | |
| 10385 | lua_settop(L, 0); |
| 10386 | return 0; |
| 10387 | |
| 10388 | error: |
| 10389 | lua_settop(L, 0); |
| 10390 | return -1; |
| 10391 | } |
| 10392 | |
| 10393 | static void hlua_filter_deinit_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 10394 | { |
| 10395 | struct hlua_flt_config *conf = fconf->conf; |
| 10396 | lua_State *L; |
| 10397 | int state_id; |
| 10398 | |
| 10399 | if (!conf) |
| 10400 | return; |
| 10401 | |
| 10402 | state_id = reg_flt_to_stack_id(conf->reg); |
| 10403 | L = hlua_states[state_id]; |
| 10404 | luaL_unref(L, LUA_REGISTRYINDEX, conf->ref[state_id]); |
| 10405 | } |
| 10406 | |
| 10407 | static int hlua_filter_init(struct proxy *px, struct flt_conf *fconf) |
| 10408 | { |
| 10409 | struct hlua_flt_config *conf = fconf->conf; |
| 10410 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 10411 | |
| 10412 | /* Rely on per-thread init for global scripts */ |
| 10413 | if (!state_id) |
| 10414 | return hlua_filter_init_per_thread(px, fconf); |
| 10415 | return 0; |
| 10416 | } |
| 10417 | |
| 10418 | static void hlua_filter_deinit(struct proxy *px, struct flt_conf *fconf) |
| 10419 | { |
| 10420 | |
| 10421 | if (fconf->conf) { |
| 10422 | struct hlua_flt_config *conf = fconf->conf; |
| 10423 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 10424 | int pos; |
| 10425 | |
| 10426 | /* Rely on per-thread deinit for global scripts */ |
| 10427 | if (!state_id) |
| 10428 | hlua_filter_deinit_per_thread(px, fconf); |
| 10429 | |
| 10430 | for (pos = 0; conf->args[pos]; pos++) |
| 10431 | free(conf->args[pos]); |
| 10432 | free(conf->args); |
| 10433 | } |
| 10434 | ha_free(&fconf->conf); |
| 10435 | ha_free((char **)&fconf->id); |
| 10436 | ha_free(&fconf->ops); |
| 10437 | } |
| 10438 | |
| 10439 | static int hlua_filter_new(struct stream *s, struct filter *filter) |
| 10440 | { |
| 10441 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 10442 | struct hlua_flt_ctx *flt_ctx = NULL; |
| 10443 | int ret = 1; |
| 10444 | |
| 10445 | /* In the execution wrappers linked with a stream, the |
| 10446 | * Lua context can be not initialized. This behavior |
| 10447 | * permits to save performances because a systematic |
| 10448 | * Lua initialization cause 5% performances loss. |
| 10449 | */ |
| 10450 | if (!s->hlua) { |
| 10451 | struct hlua *hlua; |
| 10452 | |
| 10453 | hlua = pool_alloc(pool_head_hlua); |
| 10454 | if (!hlua) { |
| 10455 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 10456 | conf->reg->name); |
| 10457 | ret = 0; |
| 10458 | goto end; |
| 10459 | } |
| 10460 | HLUA_INIT(hlua); |
| 10461 | s->hlua = hlua; |
| 10462 | if (!hlua_ctx_init(s->hlua, reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 10463 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 10464 | conf->reg->name); |
| 10465 | ret = 0; |
| 10466 | goto end; |
| 10467 | } |
| 10468 | } |
| 10469 | |
| 10470 | flt_ctx = pool_zalloc(pool_head_hlua_flt_ctx); |
| 10471 | if (!flt_ctx) { |
| 10472 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10473 | conf->reg->name); |
| 10474 | ret = 0; |
| 10475 | goto end; |
| 10476 | } |
| 10477 | flt_ctx->hlua[0] = pool_alloc(pool_head_hlua); |
| 10478 | flt_ctx->hlua[1] = pool_alloc(pool_head_hlua); |
| 10479 | if (!flt_ctx->hlua[0] || !flt_ctx->hlua[1]) { |
| 10480 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10481 | conf->reg->name); |
| 10482 | ret = 0; |
| 10483 | goto end; |
| 10484 | } |
| 10485 | HLUA_INIT(flt_ctx->hlua[0]); |
| 10486 | HLUA_INIT(flt_ctx->hlua[1]); |
| 10487 | if (!hlua_ctx_init(flt_ctx->hlua[0], reg_flt_to_stack_id(conf->reg), s->task, 0) || |
| 10488 | !hlua_ctx_init(flt_ctx->hlua[1], reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 10489 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10490 | conf->reg->name); |
| 10491 | ret = 0; |
| 10492 | goto end; |
| 10493 | } |
| 10494 | |
| 10495 | if (!HLUA_IS_RUNNING(s->hlua)) { |
| 10496 | /* The following Lua calls can fail. */ |
| 10497 | if (!SET_SAFE_LJMP(s->hlua)) { |
| 10498 | const char *error; |
| 10499 | |
| 10500 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 10501 | error = lua_tostring(s->hlua->T, -1); |
| 10502 | else |
| 10503 | error = "critical error"; |
| 10504 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 10505 | ret = 0; |
| 10506 | goto end; |
| 10507 | } |
| 10508 | |
| 10509 | /* Check stack size. */ |
| 10510 | if (!lua_checkstack(s->hlua->T, 1)) { |
| 10511 | 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] | 10512 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10513 | ret = 0; |
| 10514 | goto end; |
| 10515 | } |
| 10516 | |
| 10517 | lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, conf->ref[s->hlua->state_id]); |
| 10518 | if (lua_getfield(s->hlua->T, -1, "new") != LUA_TFUNCTION) { |
| 10519 | SEND_ERR(s->be, "Lua filter '%s': 'new' field is not a function.\n", |
| 10520 | conf->reg->name); |
| 10521 | RESET_SAFE_LJMP(s->hlua); |
| 10522 | ret = 0; |
| 10523 | goto end; |
| 10524 | } |
| 10525 | lua_insert(s->hlua->T, -2); |
| 10526 | |
| 10527 | /* Push the copy on the stack */ |
| 10528 | s->hlua->nargs = 1; |
| 10529 | |
| 10530 | /* We must initialize the execution timeouts. */ |
| 10531 | s->hlua->max_time = hlua_timeout_session; |
| 10532 | |
| 10533 | /* At this point the execution is safe. */ |
| 10534 | RESET_SAFE_LJMP(s->hlua); |
| 10535 | } |
| 10536 | |
| 10537 | switch (hlua_ctx_resume(s->hlua, 0)) { |
| 10538 | case HLUA_E_OK: |
| 10539 | /* Nothing returned or not a table, ignore the filter for current stream */ |
| 10540 | if (!lua_gettop(s->hlua->T) || !lua_istable(s->hlua->T, 1)) { |
| 10541 | ret = 0; |
| 10542 | goto end; |
| 10543 | } |
| 10544 | |
| 10545 | /* Attached the filter pointer to the ctx */ |
| 10546 | lua_pushstring(s->hlua->T, "__filter"); |
| 10547 | lua_pushlightuserdata(s->hlua->T, filter); |
| 10548 | lua_settable(s->hlua->T, -3); |
| 10549 | |
| 10550 | /* Save a ref on the filter ctx */ |
| 10551 | lua_pushvalue(s->hlua->T, 1); |
| 10552 | flt_ctx->ref = luaL_ref(s->hlua->T, LUA_REGISTRYINDEX); |
| 10553 | filter->ctx = flt_ctx; |
| 10554 | break; |
| 10555 | case HLUA_E_ERRMSG: |
| 10556 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(s->hlua->T, -1)); |
| 10557 | ret = -1; |
| 10558 | goto end; |
| 10559 | case HLUA_E_ETMOUT: |
| 10560 | SEND_ERR(s->be, "Lua filter '%s' : 'new' execution timeout.\n", conf->reg->name); |
| 10561 | ret = 0; |
| 10562 | goto end; |
| 10563 | case HLUA_E_NOMEM: |
| 10564 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10565 | ret = 0; |
| 10566 | goto end; |
| 10567 | case HLUA_E_AGAIN: |
| 10568 | case HLUA_E_YIELD: |
| 10569 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10570 | " are not allowed from 'new' function.\n", conf->reg->name); |
| 10571 | ret = 0; |
| 10572 | goto end; |
| 10573 | case HLUA_E_ERR: |
| 10574 | SEND_ERR(s->be, "Lua filter '%s': 'new' returns an unknown error.\n", conf->reg->name); |
| 10575 | ret = 0; |
| 10576 | goto end; |
| 10577 | default: |
| 10578 | ret = 0; |
| 10579 | goto end; |
| 10580 | } |
| 10581 | |
| 10582 | end: |
| 10583 | if (s->hlua) |
| 10584 | lua_settop(s->hlua->T, 0); |
| 10585 | if (ret <= 0) { |
| 10586 | if (flt_ctx) { |
| 10587 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10588 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10589 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10590 | } |
| 10591 | } |
| 10592 | return ret; |
| 10593 | } |
| 10594 | |
| 10595 | static void hlua_filter_delete(struct stream *s, struct filter *filter) |
| 10596 | { |
| 10597 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10598 | |
| 10599 | luaL_unref(s->hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 10600 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10601 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10602 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10603 | filter->ctx = NULL; |
| 10604 | } |
| 10605 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 10606 | static int hlua_filter_from_payload(struct filter *filter) |
| 10607 | { |
| 10608 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10609 | |
| 10610 | return (flt_ctx && !!(flt_ctx->flags & HLUA_FLT_CTX_FL_PAYLOAD)); |
| 10611 | } |
| 10612 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10613 | static int hlua_filter_callback(struct stream *s, struct filter *filter, const char *fun, |
| 10614 | int dir, unsigned int flags) |
| 10615 | { |
| 10616 | struct hlua *flt_hlua; |
| 10617 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 10618 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10619 | unsigned int hflags = HLUA_TXN_FLT_CTX; |
| 10620 | int ret = 1; |
| 10621 | |
| 10622 | flt_hlua = flt_ctx->hlua[(dir == SMP_OPT_DIR_REQ ? 0 : 1)]; |
| 10623 | if (!flt_hlua) |
| 10624 | goto end; |
| 10625 | |
| 10626 | if (!HLUA_IS_RUNNING(flt_hlua)) { |
| 10627 | int extra_idx = lua_gettop(flt_hlua->T); |
| 10628 | |
| 10629 | /* The following Lua calls can fail. */ |
| 10630 | if (!SET_SAFE_LJMP(flt_hlua)) { |
| 10631 | const char *error; |
| 10632 | |
| 10633 | if (lua_type(flt_hlua->T, -1) == LUA_TSTRING) |
| 10634 | error = lua_tostring(flt_hlua->T, -1); |
| 10635 | else |
| 10636 | error = "critical error"; |
| 10637 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 10638 | goto end; |
| 10639 | } |
| 10640 | |
| 10641 | /* Check stack size. */ |
| 10642 | if (!lua_checkstack(flt_hlua->T, 3)) { |
| 10643 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10644 | RESET_SAFE_LJMP(flt_hlua); |
| 10645 | goto end; |
| 10646 | } |
| 10647 | |
| 10648 | lua_rawgeti(flt_hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 10649 | if (lua_getfield(flt_hlua->T, -1, fun) != LUA_TFUNCTION) { |
| 10650 | RESET_SAFE_LJMP(flt_hlua); |
| 10651 | goto end; |
| 10652 | } |
| 10653 | lua_insert(flt_hlua->T, -2); |
| 10654 | |
| 10655 | if (!hlua_txn_new(flt_hlua->T, s, s->be, dir, hflags)) { |
| 10656 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10657 | RESET_SAFE_LJMP(flt_hlua); |
| 10658 | goto end; |
| 10659 | } |
| 10660 | flt_hlua->nargs = 2; |
| 10661 | |
| 10662 | if (flags & HLUA_FLT_CB_ARG_CHN) { |
| 10663 | if (dir == SMP_OPT_DIR_REQ) |
| 10664 | lua_getfield(flt_hlua->T, -1, "req"); |
| 10665 | else |
| 10666 | lua_getfield(flt_hlua->T, -1, "res"); |
| 10667 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10668 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10669 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10670 | lua_settable(flt_hlua->T, -3); |
| 10671 | } |
| 10672 | flt_hlua->nargs++; |
| 10673 | } |
| 10674 | else if (flags & HLUA_FLT_CB_ARG_HTTP_MSG) { |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10675 | if (dir == SMP_OPT_DIR_REQ) |
| 10676 | lua_getfield(flt_hlua->T, -1, "http_req"); |
| 10677 | else |
| 10678 | lua_getfield(flt_hlua->T, -1, "http_res"); |
| 10679 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10680 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10681 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10682 | lua_settable(flt_hlua->T, -3); |
| 10683 | } |
| 10684 | flt_hlua->nargs++; |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10685 | } |
| 10686 | |
| 10687 | /* Check stack size. */ |
| 10688 | if (!lua_checkstack(flt_hlua->T, 1)) { |
| 10689 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10690 | RESET_SAFE_LJMP(flt_hlua); |
| 10691 | goto end; |
| 10692 | } |
| 10693 | |
| 10694 | while (extra_idx--) { |
| 10695 | lua_pushvalue(flt_hlua->T, 1); |
| 10696 | lua_remove(flt_hlua->T, 1); |
| 10697 | flt_hlua->nargs++; |
| 10698 | } |
| 10699 | |
| 10700 | /* We must initialize the execution timeouts. */ |
| 10701 | flt_hlua->max_time = hlua_timeout_session; |
| 10702 | |
| 10703 | /* At this point the execution is safe. */ |
| 10704 | RESET_SAFE_LJMP(flt_hlua); |
| 10705 | } |
| 10706 | |
| 10707 | switch (hlua_ctx_resume(flt_hlua, !(flags & HLUA_FLT_CB_FINAL))) { |
| 10708 | case HLUA_E_OK: |
| 10709 | /* Catch the return value if it required */ |
| 10710 | if ((flags & HLUA_FLT_CB_RETVAL) && lua_gettop(flt_hlua->T) > 0) { |
| 10711 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10712 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10713 | } |
| 10714 | |
| 10715 | /* Set timeout in the required channel. */ |
| 10716 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10717 | if (dir == SMP_OPT_DIR_REQ) |
| 10718 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10719 | else |
| 10720 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10721 | } |
| 10722 | break; |
| 10723 | case HLUA_E_AGAIN: |
| 10724 | /* Set timeout in the required channel. */ |
| 10725 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10726 | if (dir == SMP_OPT_DIR_REQ) |
| 10727 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10728 | else |
| 10729 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10730 | } |
| 10731 | /* Some actions can be wake up when a "write" event |
| 10732 | * is detected on a response channel. This is useful |
| 10733 | * only for actions targeted on the requests. |
| 10734 | */ |
| 10735 | if (HLUA_IS_WAKERESWR(flt_hlua)) |
| 10736 | s->res.flags |= CF_WAKE_WRITE; |
| 10737 | if (HLUA_IS_WAKEREQWR(flt_hlua)) |
| 10738 | s->req.flags |= CF_WAKE_WRITE; |
| 10739 | ret = 0; |
| 10740 | goto end; |
| 10741 | case HLUA_E_ERRMSG: |
| 10742 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(flt_hlua->T, -1)); |
| 10743 | ret = -1; |
| 10744 | goto end; |
| 10745 | case HLUA_E_ETMOUT: |
| 10746 | SEND_ERR(s->be, "Lua filter '%s' : '%s' callback execution timeout.\n", conf->reg->name, fun); |
| 10747 | goto end; |
| 10748 | case HLUA_E_NOMEM: |
| 10749 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10750 | goto end; |
| 10751 | case HLUA_E_YIELD: |
| 10752 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10753 | " are not allowed from '%s' callback.\n", conf->reg->name, fun); |
| 10754 | goto end; |
| 10755 | case HLUA_E_ERR: |
| 10756 | SEND_ERR(s->be, "Lua filter '%s': '%s' returns an unknown error.\n", conf->reg->name, fun); |
| 10757 | goto end; |
| 10758 | default: |
| 10759 | goto end; |
| 10760 | } |
| 10761 | |
| 10762 | |
| 10763 | end: |
| 10764 | return ret; |
| 10765 | } |
| 10766 | |
| 10767 | static int hlua_filter_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 10768 | { |
| 10769 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10770 | |
| 10771 | flt_ctx->flags = 0; |
| 10772 | return hlua_filter_callback(s, filter, "start_analyze", |
| 10773 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10774 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 10775 | } |
| 10776 | |
| 10777 | static int hlua_filter_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 10778 | { |
| 10779 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10780 | |
| 10781 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10782 | return hlua_filter_callback(s, filter, "end_analyze", |
| 10783 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10784 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 10785 | } |
| 10786 | |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10787 | static int hlua_filter_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 10788 | { |
| 10789 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10790 | |
| 10791 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10792 | return hlua_filter_callback(s, filter, "http_headers", |
| 10793 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10794 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10795 | } |
| 10796 | |
| 10797 | static int hlua_filter_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg, |
| 10798 | unsigned int offset, unsigned int len) |
| 10799 | { |
| 10800 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10801 | struct hlua *flt_hlua; |
| 10802 | int dir = (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 10803 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 10804 | int ret; |
| 10805 | |
| 10806 | flt_hlua = flt_ctx->hlua[idx]; |
| 10807 | flt_ctx->cur_off[idx] = offset; |
| 10808 | flt_ctx->cur_len[idx] = len; |
| 10809 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 10810 | ret = hlua_filter_callback(s, filter, "http_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10811 | if (ret != -1) { |
| 10812 | ret = flt_ctx->cur_len[idx]; |
| 10813 | if (lua_gettop(flt_hlua->T) > 0) { |
| 10814 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10815 | if (ret > flt_ctx->cur_len[idx]) |
| 10816 | ret = flt_ctx->cur_len[idx]; |
| 10817 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10818 | } |
| 10819 | } |
| 10820 | return ret; |
| 10821 | } |
| 10822 | |
| 10823 | static int hlua_filter_http_end(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 10824 | { |
| 10825 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10826 | |
| 10827 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10828 | return hlua_filter_callback(s, filter, "http_end", |
| 10829 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10830 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10831 | } |
| 10832 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10833 | static int hlua_filter_tcp_payload(struct stream *s, struct filter *filter, struct channel *chn, |
| 10834 | unsigned int offset, unsigned int len) |
| 10835 | { |
| 10836 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10837 | struct hlua *flt_hlua; |
| 10838 | int dir = (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 10839 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 10840 | int ret; |
| 10841 | |
| 10842 | flt_hlua = flt_ctx->hlua[idx]; |
| 10843 | flt_ctx->cur_off[idx] = offset; |
| 10844 | flt_ctx->cur_len[idx] = len; |
| 10845 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 10846 | ret = hlua_filter_callback(s, filter, "tcp_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_CHN)); |
| 10847 | if (ret != -1) { |
| 10848 | ret = flt_ctx->cur_len[idx]; |
| 10849 | if (lua_gettop(flt_hlua->T) > 0) { |
| 10850 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10851 | if (ret > flt_ctx->cur_len[idx]) |
| 10852 | ret = flt_ctx->cur_len[idx]; |
| 10853 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10854 | } |
| 10855 | } |
| 10856 | return ret; |
| 10857 | } |
| 10858 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10859 | static int hlua_filter_parse_fct(char **args, int *cur_arg, struct proxy *px, |
| 10860 | struct flt_conf *fconf, char **err, void *private) |
| 10861 | { |
| 10862 | struct hlua_reg_filter *reg_flt = private; |
| 10863 | lua_State *L; |
| 10864 | struct hlua_flt_config *conf = NULL; |
| 10865 | const char *flt_id = NULL; |
| 10866 | int state_id, pos, flt_flags = 0; |
| 10867 | struct flt_ops *hlua_flt_ops = NULL; |
| 10868 | |
| 10869 | state_id = reg_flt_to_stack_id(reg_flt); |
| 10870 | L = hlua_states[state_id]; |
| 10871 | |
| 10872 | /* Initialize the filter ops with default callbacks */ |
| 10873 | hlua_flt_ops = calloc(1, sizeof(*hlua_flt_ops)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10874 | if (!hlua_flt_ops) |
| 10875 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10876 | hlua_flt_ops->init = hlua_filter_init; |
| 10877 | hlua_flt_ops->deinit = hlua_filter_deinit; |
| 10878 | if (state_id) { |
| 10879 | /* Set per-thread callback if script is loaded per-thread */ |
| 10880 | hlua_flt_ops->init_per_thread = hlua_filter_init_per_thread; |
| 10881 | hlua_flt_ops->deinit_per_thread = hlua_filter_deinit_per_thread; |
| 10882 | } |
| 10883 | hlua_flt_ops->attach = hlua_filter_new; |
| 10884 | hlua_flt_ops->detach = hlua_filter_delete; |
| 10885 | |
| 10886 | /* Push the filter class on the stack and resolve all callbacks */ |
| 10887 | lua_rawgeti(L, LUA_REGISTRYINDEX, reg_flt->flt_ref[state_id]); |
| 10888 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10889 | if (lua_getfield(L, -1, "start_analyze") == LUA_TFUNCTION) |
| 10890 | hlua_flt_ops->channel_start_analyze = hlua_filter_start_analyze; |
| 10891 | lua_pop(L, 1); |
| 10892 | if (lua_getfield(L, -1, "end_analyze") == LUA_TFUNCTION) |
| 10893 | hlua_flt_ops->channel_end_analyze = hlua_filter_end_analyze; |
| 10894 | lua_pop(L, 1); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10895 | if (lua_getfield(L, -1, "http_headers") == LUA_TFUNCTION) |
| 10896 | hlua_flt_ops->http_headers = hlua_filter_http_headers; |
| 10897 | lua_pop(L, 1); |
| 10898 | if (lua_getfield(L, -1, "http_payload") == LUA_TFUNCTION) |
| 10899 | hlua_flt_ops->http_payload = hlua_filter_http_payload; |
| 10900 | lua_pop(L, 1); |
| 10901 | if (lua_getfield(L, -1, "http_end") == LUA_TFUNCTION) |
| 10902 | hlua_flt_ops->http_end = hlua_filter_http_end; |
| 10903 | lua_pop(L, 1); |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10904 | if (lua_getfield(L, -1, "tcp_payload") == LUA_TFUNCTION) |
| 10905 | hlua_flt_ops->tcp_payload = hlua_filter_tcp_payload; |
| 10906 | lua_pop(L, 1); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10907 | |
| 10908 | /* Get id and flags of the filter class */ |
| 10909 | if (lua_getfield(L, -1, "id") == LUA_TSTRING) |
| 10910 | flt_id = lua_tostring(L, -1); |
| 10911 | lua_pop(L, 1); |
| 10912 | if (lua_getfield(L, -1, "flags") == LUA_TNUMBER) |
| 10913 | flt_flags = lua_tointeger(L, -1); |
| 10914 | lua_pop(L, 1); |
| 10915 | |
| 10916 | /* Create the filter config */ |
| 10917 | conf = calloc(1, sizeof(*conf)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10918 | if (!conf) |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10919 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10920 | conf->reg = reg_flt; |
| 10921 | |
| 10922 | /* duplicate args */ |
| 10923 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++); |
| 10924 | conf->args = calloc(pos + 1, sizeof(*conf->args)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10925 | if (!conf->args) |
| 10926 | goto error; |
| 10927 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++) { |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10928 | conf->args[pos] = strdup(args[*cur_arg + 1 + pos]); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10929 | if (!conf->args[pos]) |
| 10930 | goto error; |
| 10931 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10932 | conf->args[pos] = NULL; |
| 10933 | *cur_arg += pos + 1; |
| 10934 | |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10935 | if (flt_id) { |
| 10936 | fconf->id = strdup(flt_id); |
| 10937 | if (!fconf->id) |
| 10938 | goto error; |
| 10939 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10940 | fconf->flags = flt_flags; |
| 10941 | fconf->conf = conf; |
| 10942 | fconf->ops = hlua_flt_ops; |
| 10943 | |
| 10944 | lua_settop(L, 0); |
| 10945 | return 0; |
| 10946 | |
| 10947 | error: |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10948 | 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] | 10949 | free(hlua_flt_ops); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10950 | if (conf && conf->args) { |
| 10951 | for (pos = 0; conf->args[pos]; pos++) |
| 10952 | free(conf->args[pos]); |
| 10953 | free(conf->args); |
| 10954 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10955 | free(conf); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10956 | free((char *)fconf->id); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10957 | lua_settop(L, 0); |
| 10958 | return -1; |
| 10959 | } |
| 10960 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10961 | __LJMP static int hlua_register_data_filter(lua_State *L) |
| 10962 | { |
| 10963 | struct filter *filter; |
| 10964 | struct channel *chn; |
| 10965 | |
| 10966 | MAY_LJMP(check_args(L, 2, "register_data_filter")); |
| 10967 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 10968 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 10969 | |
| 10970 | lua_getfield(L, 1, "__filter"); |
| 10971 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 10972 | filter = lua_touserdata (L, -1); |
| 10973 | lua_pop(L, 1); |
| 10974 | |
| 10975 | register_data_filter(chn_strm(chn), chn, filter); |
| 10976 | return 1; |
| 10977 | } |
| 10978 | |
| 10979 | __LJMP static int hlua_unregister_data_filter(lua_State *L) |
| 10980 | { |
| 10981 | struct filter *filter; |
| 10982 | struct channel *chn; |
| 10983 | |
| 10984 | MAY_LJMP(check_args(L, 2, "unregister_data_filter")); |
| 10985 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 10986 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 10987 | |
| 10988 | lua_getfield(L, 1, "__filter"); |
| 10989 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 10990 | filter = lua_touserdata (L, -1); |
| 10991 | lua_pop(L, 1); |
| 10992 | |
| 10993 | unregister_data_filter(chn_strm(chn), chn, filter); |
| 10994 | return 1; |
| 10995 | } |
| 10996 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10997 | /* 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] | 10998 | * 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] | 10999 | * parse configuration arguments. |
| 11000 | */ |
| 11001 | __LJMP static int hlua_register_filter(lua_State *L) |
| 11002 | { |
| 11003 | struct buffer *trash; |
| 11004 | struct flt_kw_list *fkl; |
| 11005 | struct flt_kw *fkw; |
| 11006 | const char *name; |
| 11007 | struct hlua_reg_filter *reg_flt= NULL; |
| 11008 | int flt_ref, fun_ref; |
| 11009 | int len; |
| 11010 | |
| 11011 | MAY_LJMP(check_args(L, 3, "register_filter")); |
| 11012 | |
| 11013 | /* First argument : filter name. */ |
| 11014 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 11015 | |
| 11016 | /* Second argument : The filter class */ |
| 11017 | flt_ref = MAY_LJMP(hlua_checktable(L, 2)); |
| 11018 | |
| 11019 | /* Third argument : lua function. */ |
| 11020 | fun_ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 11021 | |
| 11022 | trash = get_trash_chunk(); |
| 11023 | chunk_printf(trash, "lua.%s", name); |
| 11024 | fkw = flt_find_kw(trash->area); |
| 11025 | if (fkw != NULL) { |
| 11026 | reg_flt = fkw->private; |
| 11027 | if (reg_flt->flt_ref[hlua_state_id] != -1 || reg_flt->fun_ref[hlua_state_id] != -1) { |
| 11028 | ha_warning("Trying to register filter 'lua.%s' more than once. " |
| 11029 | "This will become a hard error in version 2.5.\n", name); |
| 11030 | } |
| 11031 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 11032 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 11033 | return 0; |
| 11034 | } |
| 11035 | |
| 11036 | fkl = calloc(1, sizeof(*fkl) + sizeof(struct flt_kw) * 2); |
| 11037 | if (!fkl) |
| 11038 | goto alloc_error; |
| 11039 | fkl->scope = "HLUA"; |
| 11040 | |
| 11041 | reg_flt = new_hlua_reg_filter(name); |
| 11042 | if (!reg_flt) |
| 11043 | goto alloc_error; |
| 11044 | |
| 11045 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 11046 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 11047 | |
| 11048 | /* The filter keyword */ |
| 11049 | len = strlen("lua.") + strlen(name) + 1; |
| 11050 | fkl->kw[0].kw = calloc(1, len); |
| 11051 | if (!fkl->kw[0].kw) |
| 11052 | goto alloc_error; |
| 11053 | |
| 11054 | snprintf((char *)fkl->kw[0].kw, len, "lua.%s", name); |
| 11055 | |
| 11056 | fkl->kw[0].parse = hlua_filter_parse_fct; |
| 11057 | fkl->kw[0].private = reg_flt; |
| 11058 | memset(&fkl->kw[1], 0, sizeof(*fkl->kw)); |
| 11059 | |
| 11060 | /* Register this new filter */ |
| 11061 | flt_register_keywords(fkl); |
| 11062 | |
| 11063 | return 0; |
| 11064 | |
| 11065 | alloc_error: |
| 11066 | release_hlua_reg_filter(reg_flt); |
| 11067 | ha_free(&fkl); |
| 11068 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 11069 | return 0; /* Never reached */ |
| 11070 | } |
| 11071 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11072 | 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] | 11073 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11074 | char **err, unsigned int *timeout) |
| 11075 | { |
| 11076 | const char *error; |
| 11077 | |
| 11078 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 11079 | if (error == PARSE_TIME_OVER) { |
| 11080 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 11081 | args[1], args[0]); |
| 11082 | return -1; |
| 11083 | } |
| 11084 | else if (error == PARSE_TIME_UNDER) { |
| 11085 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 11086 | args[1], args[0]); |
| 11087 | return -1; |
| 11088 | } |
| 11089 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11090 | memprintf(err, "%s: invalid timeout", args[0]); |
| 11091 | return -1; |
| 11092 | } |
| 11093 | return 0; |
| 11094 | } |
| 11095 | |
| 11096 | 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] | 11097 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11098 | char **err) |
| 11099 | { |
| 11100 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11101 | file, line, err, &hlua_timeout_session); |
| 11102 | } |
| 11103 | |
| 11104 | 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] | 11105 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11106 | char **err) |
| 11107 | { |
| 11108 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11109 | file, line, err, &hlua_timeout_task); |
| 11110 | } |
| 11111 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11112 | 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] | 11113 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11114 | char **err) |
| 11115 | { |
| 11116 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11117 | file, line, err, &hlua_timeout_applet); |
| 11118 | } |
| 11119 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11120 | 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] | 11121 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11122 | char **err) |
| 11123 | { |
| 11124 | char *error; |
| 11125 | |
| 11126 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 11127 | if (*error != '\0') { |
| 11128 | memprintf(err, "%s: invalid number", args[0]); |
| 11129 | return -1; |
| 11130 | } |
| 11131 | return 0; |
| 11132 | } |
| 11133 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11134 | 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] | 11135 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11136 | char **err) |
| 11137 | { |
| 11138 | char *error; |
| 11139 | |
| 11140 | if (*(args[1]) == 0) { |
| 11141 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 11142 | return -1; |
| 11143 | } |
| 11144 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 11145 | if (*error != '\0') { |
| 11146 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 11147 | return -1; |
| 11148 | } |
| 11149 | return 0; |
| 11150 | } |
| 11151 | |
| 11152 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11153 | /* This function is called by the main configuration key "lua-load". It loads and |
| 11154 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 11155 | * the main lua entry point. |
| 11156 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 11157 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 11158 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11159 | * |
| 11160 | * In some error case, LUA set an error message in top of the stack. This function |
| 11161 | * 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] | 11162 | * |
| 11163 | * This function can fail with an abort() due to an Lua critical error. |
| 11164 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 11165 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11166 | */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11167 | static int hlua_load_state(char *filename, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11168 | { |
| 11169 | int error; |
| 11170 | |
| 11171 | /* Just load and compile the file. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11172 | error = luaL_loadfile(L, filename); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11173 | if (error) { |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11174 | memprintf(err, "error in Lua file '%s': %s", filename, 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 | } |
| 11178 | |
| 11179 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11180 | error = lua_pcall(L, 0, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11181 | switch (error) { |
| 11182 | case LUA_OK: |
| 11183 | break; |
| 11184 | case LUA_ERRRUN: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11185 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1)); |
| 11186 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11187 | return -1; |
| 11188 | case LUA_ERRMEM: |
Thierry Fournier | de6145f | 2020-11-29 00:55:53 +0100 | [diff] [blame] | 11189 | memprintf(err, "Lua out of memory error\n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11190 | return -1; |
| 11191 | case LUA_ERRERR: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11192 | memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1)); |
| 11193 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11194 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 11195 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11196 | case LUA_ERRGCMM: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11197 | memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1)); |
| 11198 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11199 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 11200 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11201 | default: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11202 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1)); |
| 11203 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11204 | return -1; |
| 11205 | } |
| 11206 | |
| 11207 | return 0; |
| 11208 | } |
| 11209 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11210 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 11211 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11212 | char **err) |
| 11213 | { |
| 11214 | if (*(args[1]) == 0) { |
| 11215 | memprintf(err, "'%s' expects a file name as parameter.\n", args[0]); |
| 11216 | return -1; |
| 11217 | } |
| 11218 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11219 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11220 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11221 | ha_set_thread(NULL); |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 11222 | return hlua_load_state(args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11223 | } |
| 11224 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11225 | 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] | 11226 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11227 | char **err) |
| 11228 | { |
| 11229 | int len; |
| 11230 | |
| 11231 | if (*(args[1]) == 0) { |
| 11232 | memprintf(err, "'%s' expects a file as parameter.\n", args[0]); |
| 11233 | return -1; |
| 11234 | } |
| 11235 | |
| 11236 | if (per_thread_load == NULL) { |
| 11237 | /* allocate the first entry large enough to store the final NULL */ |
| 11238 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 11239 | if (per_thread_load == NULL) { |
| 11240 | memprintf(err, "out of memory error"); |
| 11241 | return -1; |
| 11242 | } |
| 11243 | } |
| 11244 | |
| 11245 | /* count used entries */ |
| 11246 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 11247 | ; |
| 11248 | |
| 11249 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 11250 | if (per_thread_load == NULL) { |
| 11251 | memprintf(err, "out of memory error"); |
| 11252 | return -1; |
| 11253 | } |
| 11254 | |
| 11255 | per_thread_load[len] = strdup(args[1]); |
| 11256 | per_thread_load[len + 1] = NULL; |
| 11257 | |
| 11258 | if (per_thread_load[len] == NULL) { |
| 11259 | memprintf(err, "out of memory error"); |
| 11260 | return -1; |
| 11261 | } |
| 11262 | |
| 11263 | /* loading for thread 1 only */ |
| 11264 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11265 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11266 | return hlua_load_state(args[1], hlua_states[1], err); |
| 11267 | } |
| 11268 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11269 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 11270 | * in the given <ctx>. |
| 11271 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 11272 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11273 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 11274 | lua_getglobal(L, "package"); /* push package variable */ |
| 11275 | lua_pushstring(L, path); /* push given path */ |
| 11276 | lua_pushstring(L, ";"); /* push semicolon */ |
| 11277 | lua_getfield(L, -3, type); /* push old path */ |
| 11278 | lua_concat(L, 3); /* concatenate to new path */ |
| 11279 | lua_setfield(L, -2, type); /* store new path */ |
| 11280 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11281 | |
| 11282 | return 0; |
| 11283 | } |
| 11284 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11285 | 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] | 11286 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11287 | char **err) |
| 11288 | { |
| 11289 | char *path; |
| 11290 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11291 | struct prepend_path *p = NULL; |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 11292 | size_t i; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11293 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11294 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11295 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11296 | } |
| 11297 | |
| 11298 | if (!(*args[1])) { |
| 11299 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11300 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11301 | } |
| 11302 | path = args[1]; |
| 11303 | |
| 11304 | if (*args[2]) { |
| 11305 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 11306 | 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] | 11307 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11308 | } |
| 11309 | type = args[2]; |
| 11310 | } |
| 11311 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11312 | p = calloc(1, sizeof(*p)); |
| 11313 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11314 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11315 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11316 | } |
| 11317 | p->path = strdup(path); |
| 11318 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11319 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11320 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11321 | } |
| 11322 | p->type = strdup(type); |
| 11323 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11324 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11325 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11326 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 11327 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11328 | |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 11329 | /* Handle the global state and the per-thread state for the first |
| 11330 | * thread. The remaining threads will be initialized based on |
| 11331 | * prepend_path_list. |
| 11332 | */ |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 11333 | for (i = 0; i < 2; i++) { |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 11334 | lua_State *L = hlua_states[i]; |
| 11335 | const char *error; |
| 11336 | |
| 11337 | if (setjmp(safe_ljmp_env) != 0) { |
| 11338 | lua_atpanic(L, hlua_panic_safe); |
| 11339 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11340 | error = lua_tostring(L, -1); |
| 11341 | else |
| 11342 | error = "critical error"; |
| 11343 | fprintf(stderr, "lua-prepend-path: %s.\n", error); |
| 11344 | exit(1); |
| 11345 | } else { |
| 11346 | lua_atpanic(L, hlua_panic_ljmp); |
| 11347 | } |
| 11348 | |
| 11349 | hlua_prepend_path(L, type, path); |
| 11350 | |
| 11351 | lua_atpanic(L, hlua_panic_safe); |
| 11352 | } |
| 11353 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11354 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11355 | |
| 11356 | err2: |
| 11357 | free(p->type); |
| 11358 | free(p->path); |
| 11359 | err: |
| 11360 | free(p); |
| 11361 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11362 | } |
| 11363 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11364 | /* configuration keywords declaration */ |
| 11365 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11366 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11367 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11368 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11369 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 11370 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 11371 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11372 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11373 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11374 | { 0, NULL, NULL }, |
| 11375 | }}; |
| 11376 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 11377 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 11378 | |
William Lallemand | 5213918 | 2022-03-30 15:05:42 +0200 | [diff] [blame] | 11379 | #ifdef USE_OPENSSL |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 11380 | |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11381 | /* |
| 11382 | * This function replace a ckch_store by another one, and rebuild the ckch_inst and all its dependencies. |
| 11383 | * It does the sam as "cli_io_handler_commit_cert" but for lua, the major |
| 11384 | * difference is that the yield in lua and for the CLI is not handled the same |
| 11385 | * way. |
| 11386 | */ |
| 11387 | __LJMP static int hlua_ckch_commit_yield(lua_State *L, int status, lua_KContext ctx) |
| 11388 | { |
| 11389 | struct ckch_inst **lua_ckchi = lua_touserdata(L, -1); |
| 11390 | struct ckch_store **lua_ckchs = lua_touserdata(L, -2); |
| 11391 | struct ckch_inst *ckchi = *lua_ckchi; |
| 11392 | struct ckch_store *old_ckchs = lua_ckchs[0]; |
| 11393 | struct ckch_store *new_ckchs = lua_ckchs[1]; |
| 11394 | struct hlua *hlua; |
| 11395 | char *err = NULL; |
| 11396 | int y = 1; |
| 11397 | |
| 11398 | hlua = hlua_gethlua(L); |
| 11399 | |
| 11400 | /* get the first ckchi to copy */ |
| 11401 | if (ckchi == NULL) |
| 11402 | ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs); |
| 11403 | |
| 11404 | /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */ |
| 11405 | list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) { |
| 11406 | struct ckch_inst *new_inst; |
| 11407 | |
| 11408 | /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */ |
| 11409 | if (y % 10 == 0) { |
| 11410 | |
| 11411 | *lua_ckchi = ckchi; |
| 11412 | |
| 11413 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 11414 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_ckch_commit_yield, TICK_ETERNITY, 0)); |
| 11415 | } |
| 11416 | |
| 11417 | if (ckch_inst_rebuild(new_ckchs, ckchi, &new_inst, &err)) |
| 11418 | goto error; |
| 11419 | |
| 11420 | /* link the new ckch_inst to the duplicate */ |
| 11421 | LIST_APPEND(&new_ckchs->ckch_inst, &new_inst->by_ckchs); |
| 11422 | y++; |
| 11423 | } |
| 11424 | |
| 11425 | /* The generation is finished, we can insert everything */ |
| 11426 | ckch_store_replace(old_ckchs, new_ckchs); |
| 11427 | |
| 11428 | lua_pop(L, 2); /* pop the lua_ckchs and ckchi */ |
| 11429 | |
| 11430 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11431 | |
| 11432 | return 0; |
| 11433 | |
| 11434 | error: |
| 11435 | ckch_store_free(new_ckchs); |
| 11436 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11437 | WILL_LJMP(luaL_error(L, "%s", err)); |
| 11438 | free(err); |
| 11439 | |
| 11440 | return 0; |
| 11441 | } |
| 11442 | |
| 11443 | /* |
| 11444 | * Replace a ckch_store <filename> in the ckchs_tree with a ckch_store created |
| 11445 | * from the table in parameter. |
| 11446 | * |
| 11447 | * This is equivalent to "set ssl cert" + "commit ssl cert" over the CLI, which |
| 11448 | * means it does not need to have a transaction since everything is done in the |
| 11449 | * same function. |
| 11450 | * |
| 11451 | * CertCache.set{filename="", crt="", key="", sctl="", ocsp="", issuer=""} |
| 11452 | * |
| 11453 | */ |
| 11454 | __LJMP static int hlua_ckch_set(lua_State *L) |
| 11455 | { |
| 11456 | struct hlua *hlua; |
| 11457 | struct ckch_inst **lua_ckchi; |
| 11458 | struct ckch_store **lua_ckchs; |
| 11459 | struct ckch_store *old_ckchs = NULL; |
| 11460 | struct ckch_store *new_ckchs = NULL; |
| 11461 | int errcode = 0; |
| 11462 | char *err = NULL; |
| 11463 | struct cert_exts *cert_ext = NULL; |
| 11464 | char *filename; |
| 11465 | struct cert_key_and_chain *ckch; |
| 11466 | int ret; |
| 11467 | |
| 11468 | if (lua_type(L, -1) != LUA_TTABLE) |
| 11469 | WILL_LJMP(luaL_error(L, "'CertCache.set' needs a table as argument")); |
| 11470 | |
| 11471 | hlua = hlua_gethlua(L); |
| 11472 | |
| 11473 | /* FIXME: this should not return an error but should come back later */ |
| 11474 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 11475 | WILL_LJMP(luaL_error(L, "CertCache already under lock")); |
| 11476 | |
| 11477 | ret = lua_getfield(L, -1, "filename"); |
| 11478 | if (ret != LUA_TSTRING) { |
| 11479 | memprintf(&err, "%sNo filename specified!\n", err ? err : ""); |
| 11480 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11481 | goto end; |
| 11482 | } |
| 11483 | filename = (char *)lua_tostring(L, -1); |
| 11484 | |
| 11485 | |
| 11486 | /* look for the filename in the tree */ |
| 11487 | old_ckchs = ckchs_lookup(filename); |
| 11488 | if (!old_ckchs) { |
| 11489 | memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n", err ? err : ""); |
| 11490 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11491 | goto end; |
| 11492 | } |
| 11493 | /* TODO: handle extra_files_noext */ |
| 11494 | |
| 11495 | new_ckchs = ckchs_dup(old_ckchs); |
| 11496 | if (!new_ckchs) { |
| 11497 | memprintf(&err, "%sCannot allocate memory!\n", err ? err : ""); |
| 11498 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11499 | goto end; |
| 11500 | } |
| 11501 | |
| 11502 | ckch = new_ckchs->ckch; |
| 11503 | |
| 11504 | /* loop on the field in the table, which have the same name as the |
| 11505 | * possible extensions of files */ |
| 11506 | lua_pushnil(L); |
| 11507 | while (lua_next(L, 1)) { |
| 11508 | int i; |
| 11509 | const char *field = lua_tostring(L, -2); |
| 11510 | char *payload = (char *)lua_tostring(L, -1); |
| 11511 | |
| 11512 | if (!field || strcmp(field, "filename") == 0) { |
| 11513 | lua_pop(L, 1); |
| 11514 | continue; |
| 11515 | } |
| 11516 | |
| 11517 | for (i = 0; field && cert_exts[i].ext != NULL; i++) { |
| 11518 | if (strcmp(field, cert_exts[i].ext) == 0) { |
| 11519 | cert_ext = &cert_exts[i]; |
| 11520 | break; |
| 11521 | } |
| 11522 | } |
| 11523 | |
| 11524 | /* this is the default type, the field is not supported */ |
| 11525 | if (cert_ext == NULL) { |
| 11526 | memprintf(&err, "%sUnsupported field '%s'\n", err ? err : "", field); |
| 11527 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11528 | goto end; |
| 11529 | } |
| 11530 | |
| 11531 | /* appply the change on the duplicate */ |
| 11532 | if (cert_exts->load(filename, payload, ckch, &err) != 0) { |
| 11533 | memprintf(&err, "%sCan't load the payload\n", err ? err : ""); |
| 11534 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11535 | goto end; |
| 11536 | } |
| 11537 | lua_pop(L, 1); |
| 11538 | } |
| 11539 | |
| 11540 | /* store the pointers on the lua stack */ |
| 11541 | lua_ckchs = lua_newuserdata(L, sizeof(struct ckch_store *) * 2); |
| 11542 | lua_ckchs[0] = old_ckchs; |
| 11543 | lua_ckchs[1] = new_ckchs; |
| 11544 | lua_ckchi = lua_newuserdata(L, sizeof(struct ckch_inst *)); |
| 11545 | *lua_ckchi = NULL; |
| 11546 | |
| 11547 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 11548 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_ckch_commit_yield, TICK_ETERNITY, 0)); |
| 11549 | |
| 11550 | end: |
| 11551 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11552 | |
| 11553 | if (errcode & ERR_CODE) { |
| 11554 | ckch_store_free(new_ckchs); |
| 11555 | WILL_LJMP(luaL_error(L, "%s", err)); |
| 11556 | } |
| 11557 | free(err); |
| 11558 | |
| 11559 | return 0; |
| 11560 | } |
| 11561 | |
William Lallemand | 5213918 | 2022-03-30 15:05:42 +0200 | [diff] [blame] | 11562 | #else |
| 11563 | |
| 11564 | __LJMP static int hlua_ckch_set(lua_State *L) |
| 11565 | { |
| 11566 | WILL_LJMP(luaL_error(L, "'CertCache.set' needs an HAProxy built with OpenSSL")); |
| 11567 | |
| 11568 | return 0; |
| 11569 | } |
| 11570 | #endif /* ! USE_OPENSSL */ |
| 11571 | |
| 11572 | |
| 11573 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 11574 | /* This function can fail with an abort() due to an Lua critical error. |
| 11575 | * We are in the initialisation process of HAProxy, this abort() is |
| 11576 | * tolerated. |
| 11577 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11578 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11579 | { |
| 11580 | struct hlua_init_function *init; |
| 11581 | const char *msg; |
| 11582 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 11583 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11584 | const char *kind; |
| 11585 | const char *trace; |
| 11586 | int return_status = 1; |
| 11587 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 11588 | int nres; |
| 11589 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11590 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11591 | /* disable memory limit checks if limit is not set */ |
| 11592 | if (!hlua_global_allocator.limit) |
| 11593 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 11594 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 11595 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11596 | if (setjmp(safe_ljmp_env) != 0) { |
| 11597 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11598 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11599 | error = lua_tostring(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11600 | else |
| 11601 | error = "critical error"; |
| 11602 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 11603 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11604 | } else { |
| 11605 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11606 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 11607 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11608 | hlua_fcn_post_init(L); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11609 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11610 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11611 | lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11612 | |
| 11613 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11614 | ret = lua_resume(L, L, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11615 | #else |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11616 | ret = lua_resume(L, L, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11617 | #endif |
| 11618 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11619 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11620 | |
| 11621 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11622 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 11623 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11624 | |
| 11625 | case LUA_ERRERR: |
| 11626 | kind = "message handler error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11627 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11628 | case LUA_ERRRUN: |
| 11629 | if (!kind) |
| 11630 | kind = "runtime error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11631 | msg = lua_tostring(L, -1); |
| 11632 | lua_settop(L, 0); /* Empty the stack. */ |
| 11633 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 11634 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11635 | if (msg) |
| 11636 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 11637 | else |
| 11638 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
| 11639 | return_status = 0; |
| 11640 | break; |
| 11641 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11642 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11643 | /* Unknown error */ |
| 11644 | kind = "Unknown error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11645 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11646 | case LUA_YIELD: |
| 11647 | /* yield is not configured at this step, this state doesn't happen */ |
| 11648 | if (!kind) |
| 11649 | kind = "yield not allowed"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11650 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11651 | case LUA_ERRMEM: |
| 11652 | if (!kind) |
| 11653 | kind = "out of memory error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11654 | lua_settop(L, 0); |
| 11655 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 11656 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11657 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 11658 | return_status = 0; |
| 11659 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11660 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11661 | if (!return_status) |
| 11662 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11663 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11664 | |
| 11665 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11666 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11667 | } |
| 11668 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11669 | int hlua_post_init() |
| 11670 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11671 | int ret; |
| 11672 | int i; |
| 11673 | int errors; |
| 11674 | char *err = NULL; |
| 11675 | struct hlua_function *fcn; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11676 | struct hlua_reg_filter *reg_flt; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11677 | |
Willy Tarreau | b131049 | 2021-08-30 09:35:18 +0200 | [diff] [blame] | 11678 | #if defined(USE_OPENSSL) |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11679 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11680 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11681 | int saved_used_backed = global.ssl_used_backend; |
| 11682 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11683 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11684 | global.ssl_used_backend = saved_used_backed; |
| 11685 | } |
| 11686 | #endif |
| 11687 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11688 | /* Perform post init of common thread */ |
| 11689 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11690 | ha_set_thread(&ha_thread_info[0]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11691 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 11692 | if (ret == 0) |
| 11693 | return 0; |
| 11694 | |
| 11695 | /* init remaining lua states and load files */ |
| 11696 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 11697 | |
| 11698 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11699 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11700 | |
| 11701 | /* Init lua state */ |
| 11702 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 11703 | |
| 11704 | /* Load lua files */ |
| 11705 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 11706 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 11707 | if (ret != 0) { |
| 11708 | ha_alert("Lua init: %s\n", err); |
| 11709 | return 0; |
| 11710 | } |
| 11711 | } |
| 11712 | } |
| 11713 | |
| 11714 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11715 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11716 | |
| 11717 | /* Execute post init for all states */ |
| 11718 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 11719 | |
| 11720 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11721 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11722 | |
| 11723 | /* run post init */ |
| 11724 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 11725 | if (ret == 0) |
| 11726 | return 0; |
| 11727 | } |
| 11728 | |
| 11729 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11730 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11731 | |
| 11732 | /* control functions registering. Each function must have: |
| 11733 | * - only the function_ref[0] set positive and all other to -1 |
| 11734 | * - only the function_ref[0] set to -1 and all other positive |
| 11735 | * This ensure a same reference is not used both in shared |
| 11736 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11737 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11738 | * complicated to found for the end user. |
| 11739 | */ |
| 11740 | errors = 0; |
| 11741 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 11742 | ret = 0; |
| 11743 | for (i = 1; i < global.nbthread + 1; i++) { |
| 11744 | if (fcn->function_ref[i] == -1) |
| 11745 | ret--; |
| 11746 | else |
| 11747 | ret++; |
| 11748 | } |
| 11749 | if (abs(ret) != global.nbthread) { |
| 11750 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 11751 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 11752 | errors++; |
| 11753 | continue; |
| 11754 | } |
| 11755 | |
| 11756 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11757 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 11758 | "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] | 11759 | "exclusive.\n", fcn->name); |
| 11760 | errors++; |
| 11761 | } |
| 11762 | } |
| 11763 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11764 | /* Do the same with registered filters */ |
| 11765 | list_for_each_entry(reg_flt, &referenced_filters, l) { |
| 11766 | ret = 0; |
| 11767 | for (i = 1; i < global.nbthread + 1; i++) { |
| 11768 | if (reg_flt->flt_ref[i] == -1) |
| 11769 | ret--; |
| 11770 | else |
| 11771 | ret++; |
| 11772 | } |
| 11773 | if (abs(ret) != global.nbthread) { |
| 11774 | ha_alert("Lua filter '%s' is not referenced in all thread. " |
| 11775 | "Expect function in all thread or in none thread.\n", reg_flt->name); |
| 11776 | errors++; |
| 11777 | continue; |
| 11778 | } |
| 11779 | |
| 11780 | if ((reg_flt->flt_ref[0] == -1) == (ret < 0)) { |
| 11781 | ha_alert("Lua filter '%s' is referenced both ins shared Lua context (through lua-load) " |
| 11782 | "and per-thread Lua context (through lua-load-per-thread). these two context " |
| 11783 | "exclusive.\n", fcn->name); |
| 11784 | errors++; |
| 11785 | } |
| 11786 | } |
| 11787 | |
| 11788 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11789 | if (errors > 0) |
| 11790 | return 0; |
| 11791 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11792 | /* 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] | 11793 | * -1 in order to have probably a segfault if someone use it |
| 11794 | */ |
| 11795 | hlua_state_id = -1; |
| 11796 | |
| 11797 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11798 | } |
| 11799 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11800 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 11801 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 11802 | * 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] | 11803 | * allocation. <nsize> is the requested new size. A new allocation is |
| 11804 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11805 | * zero. This one verifies that the limits are respected but is optimized |
| 11806 | * 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] | 11807 | * |
| 11808 | * Warning: while this API ressembles glibc's realloc() a lot, glibc surpasses |
| 11809 | * POSIX by making realloc(ptr,0) an effective free(), but others do not do |
| 11810 | * that and will simply allocate zero as if it were the result of malloc(0), |
| 11811 | * so mapping this onto realloc() will lead to memory leaks on non-glibc |
| 11812 | * systems. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11813 | */ |
| 11814 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 11815 | { |
| 11816 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11817 | size_t limit, old, new; |
| 11818 | |
| 11819 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 11820 | * for accounting anymore. |
| 11821 | */ |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 11822 | if (likely(~zone->limit == 0)) { |
| 11823 | if (!nsize) |
| 11824 | ha_free(&ptr); |
| 11825 | else |
| 11826 | ptr = realloc(ptr, nsize); |
| 11827 | return ptr; |
| 11828 | } |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11829 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 11830 | if (!ptr) |
| 11831 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11832 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11833 | /* enforce strict limits across all threads */ |
| 11834 | limit = zone->limit; |
| 11835 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 11836 | do { |
| 11837 | new = old + nsize - osize; |
| 11838 | if (unlikely(nsize && limit && new > limit)) |
| 11839 | return NULL; |
| 11840 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11841 | |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 11842 | if (!nsize) |
| 11843 | ha_free(&ptr); |
| 11844 | else |
| 11845 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11846 | |
| 11847 | if (unlikely(!ptr && nsize)) // failed |
| 11848 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 11849 | |
| 11850 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11851 | return ptr; |
| 11852 | } |
| 11853 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11854 | /* 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] | 11855 | * We are in the initialisation process of HAProxy, this abort() is |
| 11856 | * tolerated. |
| 11857 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11858 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 11859 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11860 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11861 | int idx; |
| 11862 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11863 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11864 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 11865 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11866 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11867 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11868 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11869 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11870 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11871 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11872 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11873 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11874 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11875 | *context = NULL; |
| 11876 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 11877 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 11878 | * the Lua function can fail with an abort. We are in the initialisation |
| 11879 | * process of HAProxy, this abort() is tolerated. |
| 11880 | */ |
| 11881 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11882 | /* Call post initialisation function in safe environment. */ |
| 11883 | if (setjmp(safe_ljmp_env) != 0) { |
| 11884 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11885 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11886 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 11887 | else |
| 11888 | error_msg = "critical error"; |
| 11889 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 11890 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11891 | } else { |
| 11892 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 11893 | } |
| 11894 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11895 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11896 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 11897 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 11898 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 11899 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11900 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 11901 | #endif |
| 11902 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11903 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 11904 | #endif |
| 11905 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 11906 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11907 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11908 | /* Apply configured prepend path */ |
| 11909 | list_for_each_entry(pp, &prepend_path_list, l) |
| 11910 | hlua_prepend_path(L, pp->type, pp->path); |
| 11911 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11912 | /* |
| 11913 | * |
| 11914 | * Create "core" object. |
| 11915 | * |
| 11916 | */ |
| 11917 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 11918 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11919 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11920 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11921 | /* set the thread id */ |
| 11922 | hlua_class_const_int(L, "thread", thread_num); |
| 11923 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11924 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 11925 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11926 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11927 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11928 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11929 | hlua_class_function(L, "register_init", hlua_register_init); |
| 11930 | hlua_class_function(L, "register_task", hlua_register_task); |
| 11931 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 11932 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 11933 | hlua_class_function(L, "register_action", hlua_register_action); |
| 11934 | hlua_class_function(L, "register_service", hlua_register_service); |
| 11935 | hlua_class_function(L, "register_cli", hlua_register_cli); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11936 | hlua_class_function(L, "register_filter", hlua_register_filter); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11937 | hlua_class_function(L, "yield", hlua_yield); |
| 11938 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 11939 | hlua_class_function(L, "sleep", hlua_sleep); |
| 11940 | hlua_class_function(L, "msleep", hlua_msleep); |
| 11941 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 11942 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 11943 | hlua_class_function(L, "set_map", hlua_set_map); |
| 11944 | hlua_class_function(L, "del_map", hlua_del_map); |
| 11945 | hlua_class_function(L, "tcp", hlua_socket_new); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 11946 | hlua_class_function(L, "httpclient", hlua_httpclient_new); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11947 | hlua_class_function(L, "log", hlua_log); |
| 11948 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 11949 | hlua_class_function(L, "Info", hlua_log_info); |
| 11950 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 11951 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 11952 | hlua_class_function(L, "done", hlua_done); |
| 11953 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11954 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11955 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 11956 | |
| 11957 | /* |
| 11958 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11959 | * Create "act" object. |
| 11960 | * |
| 11961 | */ |
| 11962 | |
| 11963 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11964 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11965 | |
| 11966 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11967 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 11968 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 11969 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 11970 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 11971 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 11972 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 11973 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 11974 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11975 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11976 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 11977 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11978 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11979 | |
| 11980 | /* |
| 11981 | * |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11982 | * Create "Filter" object. |
| 11983 | * |
| 11984 | */ |
| 11985 | |
| 11986 | /* This table entry is the object "filter" base. */ |
| 11987 | lua_newtable(L); |
| 11988 | |
| 11989 | /* push flags and constants */ |
| 11990 | hlua_class_const_int(L, "CONTINUE", 1); |
| 11991 | hlua_class_const_int(L, "WAIT", 0); |
| 11992 | hlua_class_const_int(L, "ERROR", -1); |
| 11993 | |
| 11994 | hlua_class_const_int(L, "FLT_CFG_FL_HTX", FLT_CFG_FL_HTX); |
| 11995 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11996 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
| 11997 | hlua_class_function(L, "register_data_filter", hlua_register_data_filter); |
| 11998 | hlua_class_function(L, "unregister_data_filter", hlua_unregister_data_filter); |
| 11999 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12000 | lua_setglobal(L, "filter"); |
| 12001 | |
| 12002 | /* |
| 12003 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12004 | * Register class Map |
| 12005 | * |
| 12006 | */ |
| 12007 | |
| 12008 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12009 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12010 | |
| 12011 | /* register pattern types. */ |
| 12012 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12013 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 12014 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12015 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12016 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 12017 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12018 | |
| 12019 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12020 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12021 | |
| 12022 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12023 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12024 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12025 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12026 | lua_pushstring(L, "__index"); |
| 12027 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12028 | |
| 12029 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12030 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 12031 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12032 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12033 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12034 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 12035 | /* Register previous table in the registry with reference and named entry. |
| 12036 | * The function hlua_register_metatable() pops the stack, so we |
| 12037 | * previously create a copy of the table. |
| 12038 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12039 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 12040 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12041 | |
| 12042 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12043 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12044 | |
| 12045 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12046 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12047 | |
| 12048 | /* |
| 12049 | * |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12050 | * Register "CertCache" class |
| 12051 | * |
| 12052 | */ |
| 12053 | |
| 12054 | /* Create and fill the metatable. */ |
| 12055 | lua_newtable(L); |
| 12056 | /* Register */ |
| 12057 | hlua_class_function(L, "set", hlua_ckch_set); |
| 12058 | lua_setglobal(L, CLASS_CERTCACHE); /* Create global object called Regex */ |
| 12059 | |
| 12060 | /* |
| 12061 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12062 | * Register class Channel |
| 12063 | * |
| 12064 | */ |
| 12065 | |
| 12066 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12067 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12068 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12069 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12070 | lua_pushstring(L, "__index"); |
| 12071 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12072 | |
| 12073 | /* Register . */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12074 | hlua_class_function(L, "data", hlua_channel_get_data); |
| 12075 | hlua_class_function(L, "line", hlua_channel_get_line); |
| 12076 | hlua_class_function(L, "set", hlua_channel_set_data); |
| 12077 | hlua_class_function(L, "remove", hlua_channel_del_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12078 | hlua_class_function(L, "append", hlua_channel_append); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12079 | hlua_class_function(L, "prepend", hlua_channel_prepend); |
| 12080 | hlua_class_function(L, "insert", hlua_channel_insert_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12081 | hlua_class_function(L, "send", hlua_channel_send); |
| 12082 | hlua_class_function(L, "forward", hlua_channel_forward); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12083 | hlua_class_function(L, "input", hlua_channel_get_in_len); |
| 12084 | hlua_class_function(L, "output", hlua_channel_get_out_len); |
| 12085 | hlua_class_function(L, "may_recv", hlua_channel_may_recv); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12086 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 12087 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12088 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12089 | /* Deprecated API */ |
| 12090 | hlua_class_function(L, "get", hlua_channel_get); |
| 12091 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 12092 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 12093 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 12094 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 12095 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12096 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12097 | |
| 12098 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12099 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12100 | |
| 12101 | /* |
| 12102 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12103 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12104 | * |
| 12105 | */ |
| 12106 | |
| 12107 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12108 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12109 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12110 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12111 | lua_pushstring(L, "__index"); |
| 12112 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12113 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12114 | /* Browse existing fetches and create the associated |
| 12115 | * object method. |
| 12116 | */ |
| 12117 | sf = NULL; |
| 12118 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12119 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 12120 | * by an underscore. |
| 12121 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 12122 | strlcpy2(trash.area, sf->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12123 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12124 | if (*p == '.' || *p == '-' || *p == '+') |
| 12125 | *p = '_'; |
| 12126 | |
| 12127 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12128 | lua_pushstring(L, trash.area); |
| 12129 | lua_pushlightuserdata(L, sf); |
| 12130 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 12131 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12132 | } |
| 12133 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12134 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12135 | |
| 12136 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12137 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12138 | |
| 12139 | /* |
| 12140 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12141 | * Register class Converters |
| 12142 | * |
| 12143 | */ |
| 12144 | |
| 12145 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12146 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12147 | |
| 12148 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12149 | lua_pushstring(L, "__index"); |
| 12150 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12151 | |
| 12152 | /* Browse existing converters and create the associated |
| 12153 | * object method. |
| 12154 | */ |
| 12155 | sc = NULL; |
| 12156 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12157 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 12158 | * by an underscore. |
| 12159 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 12160 | strlcpy2(trash.area, sc->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12161 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12162 | if (*p == '.' || *p == '-' || *p == '+') |
| 12163 | *p = '_'; |
| 12164 | |
| 12165 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12166 | lua_pushstring(L, trash.area); |
| 12167 | lua_pushlightuserdata(L, sc); |
| 12168 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 12169 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12170 | } |
| 12171 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12172 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12173 | |
| 12174 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12175 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12176 | |
| 12177 | /* |
| 12178 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12179 | * Register class HTTP |
| 12180 | * |
| 12181 | */ |
| 12182 | |
| 12183 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12184 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12185 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12186 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12187 | lua_pushstring(L, "__index"); |
| 12188 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12189 | |
| 12190 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12191 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 12192 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 12193 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 12194 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 12195 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 12196 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 12197 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 12198 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 12199 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 12200 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12201 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12202 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 12203 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 12204 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 12205 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 12206 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 12207 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 12208 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12209 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12210 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12211 | |
| 12212 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12213 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12214 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 12215 | /* |
| 12216 | * |
| 12217 | * Register class HTTPMessage |
| 12218 | * |
| 12219 | */ |
| 12220 | |
| 12221 | /* Create and fill the metatable. */ |
| 12222 | lua_newtable(L); |
| 12223 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 12224 | /* Create and fill the __index entry. */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 12225 | lua_pushstring(L, "__index"); |
| 12226 | lua_newtable(L); |
| 12227 | |
| 12228 | /* Register Lua functions. */ |
| 12229 | hlua_class_function(L, "is_resp", hlua_http_msg_is_resp); |
| 12230 | hlua_class_function(L, "get_stline", hlua_http_msg_get_stline); |
| 12231 | hlua_class_function(L, "get_headers", hlua_http_msg_get_headers); |
| 12232 | hlua_class_function(L, "del_header", hlua_http_msg_del_hdr); |
| 12233 | hlua_class_function(L, "rep_header", hlua_http_msg_rep_hdr); |
| 12234 | hlua_class_function(L, "rep_value", hlua_http_msg_rep_val); |
| 12235 | hlua_class_function(L, "add_header", hlua_http_msg_add_hdr); |
| 12236 | hlua_class_function(L, "set_header", hlua_http_msg_set_hdr); |
| 12237 | hlua_class_function(L, "set_method", hlua_http_msg_set_meth); |
| 12238 | hlua_class_function(L, "set_path", hlua_http_msg_set_path); |
| 12239 | hlua_class_function(L, "set_query", hlua_http_msg_set_query); |
| 12240 | hlua_class_function(L, "set_uri", hlua_http_msg_set_uri); |
| 12241 | hlua_class_function(L, "set_status", hlua_http_msg_set_status); |
| 12242 | hlua_class_function(L, "is_full", hlua_http_msg_is_full); |
| 12243 | hlua_class_function(L, "may_recv", hlua_http_msg_may_recv); |
| 12244 | hlua_class_function(L, "eom", hlua_http_msg_is_eom); |
| 12245 | hlua_class_function(L, "input", hlua_http_msg_get_in_len); |
| 12246 | hlua_class_function(L, "output", hlua_http_msg_get_out_len); |
| 12247 | |
| 12248 | hlua_class_function(L, "body", hlua_http_msg_get_body); |
| 12249 | hlua_class_function(L, "set", hlua_http_msg_set_data); |
| 12250 | hlua_class_function(L, "remove", hlua_http_msg_del_data); |
| 12251 | hlua_class_function(L, "append", hlua_http_msg_append); |
| 12252 | hlua_class_function(L, "prepend", hlua_http_msg_prepend); |
| 12253 | hlua_class_function(L, "insert", hlua_http_msg_insert_data); |
| 12254 | hlua_class_function(L, "set_eom", hlua_http_msg_set_eom); |
| 12255 | hlua_class_function(L, "unset_eom", hlua_http_msg_unset_eom); |
| 12256 | |
| 12257 | hlua_class_function(L, "send", hlua_http_msg_send); |
| 12258 | hlua_class_function(L, "forward", hlua_http_msg_forward); |
| 12259 | |
| 12260 | lua_rawset(L, -3); |
| 12261 | |
| 12262 | /* Register previous table in the registry with reference and named entry. */ |
| 12263 | class_http_msg_ref = hlua_register_metatable(L, CLASS_HTTP_MSG); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12264 | |
| 12265 | /* |
| 12266 | * |
| 12267 | * Register class HTTPClient |
| 12268 | * |
| 12269 | */ |
| 12270 | |
| 12271 | /* Create and fill the metatable. */ |
| 12272 | lua_newtable(L); |
| 12273 | lua_pushstring(L, "__index"); |
| 12274 | lua_newtable(L); |
| 12275 | hlua_class_function(L, "get", hlua_httpclient_get); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 12276 | hlua_class_function(L, "head", hlua_httpclient_head); |
| 12277 | hlua_class_function(L, "put", hlua_httpclient_put); |
| 12278 | hlua_class_function(L, "post", hlua_httpclient_post); |
| 12279 | hlua_class_function(L, "delete", hlua_httpclient_delete); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12280 | lua_settable(L, -3); /* Sets the __index entry. */ |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 12281 | /* Register the garbage collector entry. */ |
| 12282 | lua_pushstring(L, "__gc"); |
| 12283 | lua_pushcclosure(L, hlua_httpclient_gc, 0); |
| 12284 | lua_settable(L, -3); /* Push the last 2 entries in the table at index -3 */ |
| 12285 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12286 | |
| 12287 | |
| 12288 | class_httpclient_ref = hlua_register_metatable(L, CLASS_HTTPCLIENT); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12289 | /* |
| 12290 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12291 | * Register class AppletTCP |
| 12292 | * |
| 12293 | */ |
| 12294 | |
| 12295 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12296 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12297 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12298 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12299 | lua_pushstring(L, "__index"); |
| 12300 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12301 | |
| 12302 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12303 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 12304 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 12305 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 12306 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 12307 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 12308 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 12309 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 12310 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12311 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12312 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12313 | |
| 12314 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12315 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12316 | |
| 12317 | /* |
| 12318 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12319 | * Register class AppletHTTP |
| 12320 | * |
| 12321 | */ |
| 12322 | |
| 12323 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12324 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12325 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12326 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12327 | lua_pushstring(L, "__index"); |
| 12328 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12329 | |
| 12330 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12331 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 12332 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 12333 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 12334 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 12335 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 12336 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 12337 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 12338 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 12339 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 12340 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 12341 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12342 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12343 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12344 | |
| 12345 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12346 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12347 | |
| 12348 | /* |
| 12349 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12350 | * Register class TXN |
| 12351 | * |
| 12352 | */ |
| 12353 | |
| 12354 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12355 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12356 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12357 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12358 | lua_pushstring(L, "__index"); |
| 12359 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12360 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 12361 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12362 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 12363 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 12364 | hlua_class_function(L, "set_var", hlua_set_var); |
| 12365 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 12366 | hlua_class_function(L, "get_var", hlua_get_var); |
| 12367 | hlua_class_function(L, "done", hlua_txn_done); |
| 12368 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 12369 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 12370 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 12371 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 12372 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 12373 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 12374 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 12375 | hlua_class_function(L, "log", hlua_txn_log); |
| 12376 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 12377 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 12378 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 12379 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 12380 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12381 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12382 | |
| 12383 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12384 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12385 | |
| 12386 | /* |
| 12387 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 12388 | * Register class reply |
| 12389 | * |
| 12390 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12391 | lua_newtable(L); |
| 12392 | lua_pushstring(L, "__index"); |
| 12393 | lua_newtable(L); |
| 12394 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 12395 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 12396 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 12397 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 12398 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 12399 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 12400 | |
| 12401 | |
| 12402 | /* |
| 12403 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12404 | * Register class Socket |
| 12405 | * |
| 12406 | */ |
| 12407 | |
| 12408 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12409 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12410 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12411 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12412 | lua_pushstring(L, "__index"); |
| 12413 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12414 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 12415 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12416 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 12417 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12418 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 12419 | hlua_class_function(L, "send", hlua_socket_send); |
| 12420 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 12421 | hlua_class_function(L, "close", hlua_socket_close); |
| 12422 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 12423 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 12424 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 12425 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12426 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12427 | 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] | 12428 | |
| 12429 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12430 | lua_pushstring(L, "__gc"); |
| 12431 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 12432 | 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] | 12433 | |
| 12434 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12435 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12436 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12437 | lua_atpanic(L, hlua_panic_safe); |
| 12438 | |
| 12439 | return L; |
| 12440 | } |
| 12441 | |
| 12442 | void hlua_init(void) { |
| 12443 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 12444 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12445 | #ifdef USE_OPENSSL |
| 12446 | struct srv_kw *kw; |
| 12447 | int tmp_error; |
| 12448 | char *error; |
| 12449 | char *args[] = { /* SSL client configuration. */ |
| 12450 | "ssl", |
| 12451 | "verify", |
| 12452 | "none", |
| 12453 | NULL |
| 12454 | }; |
| 12455 | #endif |
| 12456 | |
| 12457 | /* Init post init function list head */ |
| 12458 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 12459 | LIST_INIT(&hlua_init_functions[i]); |
| 12460 | |
| 12461 | /* Init state for common/shared lua parts */ |
| 12462 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12463 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12464 | hlua_states[0] = hlua_init_state(0); |
| 12465 | |
| 12466 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 12467 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12468 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12469 | hlua_states[1] = hlua_init_state(1); |
| 12470 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12471 | /* Proxy and server configuration initialisation. */ |
William Lallemand | 6bb77b9 | 2021-07-28 15:48:16 +0200 | [diff] [blame] | 12472 | 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] | 12473 | if (!socket_proxy) { |
| 12474 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 12475 | exit(1); |
| 12476 | } |
| 12477 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12478 | |
| 12479 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12480 | socket_tcp = new_server(socket_proxy); |
| 12481 | if (!socket_tcp) { |
| 12482 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 12483 | exit(1); |
| 12484 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12485 | |
| 12486 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12487 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12488 | socket_ssl = new_server(socket_proxy); |
| 12489 | if (!socket_ssl) { |
| 12490 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 12491 | exit(1); |
| 12492 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12493 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12494 | socket_ssl->use_ssl = 1; |
| 12495 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12496 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 12497 | for (i = 0; args[i] != NULL; i++) { |
| 12498 | 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] | 12499 | /* |
| 12500 | * |
| 12501 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 12502 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12503 | * features like client certificates and ssl_verify. |
| 12504 | * |
| 12505 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12506 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12507 | if (tmp_error != 0) { |
| 12508 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 12509 | abort(); /* This must be never arrives because the command line |
| 12510 | not editable by the user. */ |
| 12511 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 12512 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12513 | } |
| 12514 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12515 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 12516 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 12517 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12518 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 12519 | static void hlua_deinit() |
| 12520 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 12521 | int thr; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12522 | struct hlua_reg_filter *reg_flt, *reg_flt_bck; |
| 12523 | |
| 12524 | list_for_each_entry_safe(reg_flt, reg_flt_bck, &referenced_filters, l) |
| 12525 | release_hlua_reg_filter(reg_flt); |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 12526 | |
| 12527 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 12528 | if (hlua_states[thr]) |
| 12529 | lua_close(hlua_states[thr]); |
| 12530 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 12531 | |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 12532 | srv_drop(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 12533 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 12534 | #ifdef USE_OPENSSL |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 12535 | srv_drop(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 12536 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 12537 | |
| 12538 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 12539 | } |
| 12540 | |
| 12541 | REGISTER_POST_DEINIT(hlua_deinit); |
| 12542 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12543 | static void hlua_register_build_options(void) |
| 12544 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12545 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12546 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12547 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 12548 | hap_register_build_opts(ptr, 1); |
| 12549 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12550 | |
| 12551 | INITCALL0(STG_REGISTER, hlua_register_build_options); |