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 | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 62 | #include <haproxy/stream_interface.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 63 | #include <haproxy/task.h> |
Willy Tarreau | 8b550af | 2020-06-04 17:42:48 +0200 | [diff] [blame] | 64 | #include <haproxy/tcp_rules.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 65 | #include <haproxy/thread.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 66 | #include <haproxy/tools.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 67 | #include <haproxy/vars.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 68 | #include <haproxy/xref.h> |
| 69 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 70 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 71 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 72 | * macro is used only for identifying the function that can |
| 73 | * not return because a longjmp is executed. |
| 74 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 75 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 76 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 77 | */ |
| 78 | #define __LJMP |
Willy Tarreau | 4e7cc33 | 2018-10-20 17:45:48 +0200 | [diff] [blame] | 79 | #define WILL_LJMP(func) do { func; my_unreachable(); } while(0) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 80 | #define MAY_LJMP(func) func |
| 81 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 82 | /* This couple of function executes securely some Lua calls outside of |
| 83 | * the lua runtime environment. Each Lua call can return a longjmp |
| 84 | * if it encounter a memory error. |
| 85 | * |
| 86 | * Lua documentation extract: |
| 87 | * |
| 88 | * If an error happens outside any protected environment, Lua calls |
| 89 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 90 | * exiting the host application. Your panic function can avoid this |
| 91 | * exit by never returning (e.g., doing a long jump to your own |
| 92 | * recovery point outside Lua). |
| 93 | * |
| 94 | * The panic function runs as if it were a message handler (see |
Willy Tarreau | 3dfb7da | 2022-03-02 22:33:39 +0100 | [diff] [blame] | 95 | * #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] | 96 | * stack. However, there is no guarantee about stack space. To push |
| 97 | * anything on the stack, the panic function must first check the |
Willy Tarreau | 3dfb7da | 2022-03-02 22:33:39 +0100 | [diff] [blame] | 98 | * available space (see #4.2). |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 99 | * |
| 100 | * We must check all the Lua entry point. This includes: |
| 101 | * - The include/proto/hlua.h exported functions |
| 102 | * - the task wrapper function |
| 103 | * - The action wrapper function |
| 104 | * - The converters wrapper function |
| 105 | * - The sample-fetch wrapper functions |
| 106 | * |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 107 | * It is tolerated that the initialisation function returns an abort. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 108 | * Before each Lua abort, an error message is written on stderr. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 109 | * |
| 110 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 111 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 112 | * because they must be exists in the program stack when the longjmp |
| 113 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 114 | * |
| 115 | * Note that the Lua processing is not really thread safe. It provides |
| 116 | * heavy system which consists to add our own lock function in the Lua |
| 117 | * code and recompile the library. This system will probably not accepted |
| 118 | * by maintainers of various distribs. |
| 119 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 120 | * 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] | 121 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 122 | * of function and a lua_unlock() at the end of the function. So I |
| 123 | * conclude that the Lua thread safe mode just perform a mutex around |
| 124 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 125 | * easier for distro maintainers. |
| 126 | * |
| 127 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 128 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 129 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 130 | */ |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 131 | __decl_spinlock(hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 132 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 133 | static int hlua_panic_safe(lua_State *L) { return 0; } |
Willy Tarreau | 6a51090 | 2021-07-14 19:41:25 +0200 | [diff] [blame] | 134 | 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] | 135 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 136 | /* This is the chained list of struct hlua_function referenced |
| 137 | * for haproxy action, sample-fetches, converters, cli and |
| 138 | * applet bindings. It is used for a post-initialisation control. |
| 139 | */ |
| 140 | static struct list referenced_functions = LIST_HEAD_INIT(referenced_functions); |
| 141 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 142 | /* This variable is used only during initialization to identify the Lua state |
| 143 | * currently being initialized. 0 is the common lua state, 1 to n are the Lua |
| 144 | * states dedicated to each thread (in this case hlua_state_id==tid+1). |
| 145 | */ |
| 146 | static int hlua_state_id; |
| 147 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 148 | /* This is a NULL-terminated list of lua file which are referenced to load per thread */ |
| 149 | static char **per_thread_load = NULL; |
| 150 | |
| 151 | lua_State *hlua_init_state(int thread_id); |
| 152 | |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 153 | /* This function takes the Lua global lock. Keep this function's visibility |
| 154 | * global so that it can appear in stack dumps and performance profiles! |
| 155 | */ |
| 156 | void lua_take_global_lock() |
| 157 | { |
| 158 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
| 159 | } |
| 160 | |
| 161 | static inline void lua_drop_global_lock() |
| 162 | { |
| 163 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
| 164 | } |
| 165 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 166 | #define SET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 167 | ({ \ |
| 168 | int ret; \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 169 | if ((__HLUA)->state_id == 0) \ |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 170 | lua_take_global_lock(); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 171 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 172 | lua_atpanic(__L, hlua_panic_safe); \ |
| 173 | ret = 0; \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 174 | if ((__HLUA)->state_id == 0) \ |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 175 | lua_drop_global_lock(); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 176 | } else { \ |
| 177 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 178 | ret = 1; \ |
| 179 | } \ |
| 180 | ret; \ |
| 181 | }) |
| 182 | |
| 183 | /* If we are the last function catching Lua errors, we |
| 184 | * must reset the panic function. |
| 185 | */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 186 | #define RESET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 187 | do { \ |
| 188 | lua_atpanic(__L, hlua_panic_safe); \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 189 | if ((__HLUA)->state_id == 0) \ |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 190 | lua_drop_global_lock(); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 191 | } while(0) |
| 192 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 193 | #define SET_SAFE_LJMP(__HLUA) \ |
| 194 | SET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 195 | |
| 196 | #define RESET_SAFE_LJMP(__HLUA) \ |
| 197 | RESET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 198 | |
| 199 | #define SET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 200 | SET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 201 | |
| 202 | #define RESET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 203 | RESET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 204 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 205 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 206 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 207 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 208 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 209 | /* unused: 0x08, 0x10 */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 210 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 211 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 212 | |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 213 | /* The main Lua execution context. The 0 index is the |
| 214 | * common state shared by all threads. |
| 215 | */ |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 216 | static lua_State *hlua_states[MAX_THREADS + 1]; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 217 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 218 | #define HLUA_FLT_CB_FINAL 0x00000001 |
| 219 | #define HLUA_FLT_CB_RETVAL 0x00000002 |
| 220 | #define HLUA_FLT_CB_ARG_CHN 0x00000004 |
| 221 | #define HLUA_FLT_CB_ARG_HTTP_MSG 0x00000008 |
| 222 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 223 | #define HLUA_FLT_CTX_FL_PAYLOAD 0x00000001 |
| 224 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 225 | struct hlua_reg_filter { |
| 226 | char *name; |
| 227 | int flt_ref[MAX_THREADS + 1]; |
| 228 | int fun_ref[MAX_THREADS + 1]; |
| 229 | struct list l; |
| 230 | }; |
| 231 | |
| 232 | struct hlua_flt_config { |
| 233 | struct hlua_reg_filter *reg; |
| 234 | int ref[MAX_THREADS + 1]; |
| 235 | char **args; |
| 236 | }; |
| 237 | |
| 238 | struct hlua_flt_ctx { |
| 239 | int ref; /* ref to the filter lua object */ |
| 240 | struct hlua *hlua[2]; /* lua runtime context (0: request, 1: response) */ |
| 241 | unsigned int cur_off[2]; /* current offset (0: request, 1: response) */ |
| 242 | unsigned int cur_len[2]; /* current forwardable length (0: request, 1: response) */ |
| 243 | unsigned int flags; /* HLUA_FLT_CTX_FL_* */ |
| 244 | }; |
| 245 | |
| 246 | DECLARE_STATIC_POOL(pool_head_hlua_flt_ctx, "hlua_flt_ctx", sizeof(struct hlua_flt_ctx)); |
| 247 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 248 | static int hlua_filter_from_payload(struct filter *filter); |
| 249 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 250 | /* This is the chained list of struct hlua_flt referenced |
| 251 | * for haproxy filters. It is used for a post-initialisation control. |
| 252 | */ |
| 253 | static struct list referenced_filters = LIST_HEAD_INIT(referenced_filters); |
| 254 | |
| 255 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 256 | /* This is the memory pool containing struct lua for applets |
| 257 | * (including cli). |
| 258 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 259 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 260 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 261 | /* Used for Socket connection. */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 262 | static struct proxy *socket_proxy; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 263 | static struct server *socket_tcp; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 264 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 265 | static struct server *socket_ssl; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 266 | #endif |
| 267 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 268 | /* List head of the function called at the initialisation time. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 269 | struct list hlua_init_functions[MAX_THREADS + 1]; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 270 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 271 | /* The following variables contains the reference of the different |
| 272 | * Lua classes. These references are useful for identify metadata |
| 273 | * associated with an object. |
| 274 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 275 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 276 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 277 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 278 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 279 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 280 | static int class_http_ref; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 281 | static int class_http_msg_ref; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 282 | static int class_httpclient_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 283 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 284 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 285 | static int class_applet_http_ref; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 286 | static int class_txn_reply_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 287 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 288 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 289 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 290 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 291 | * because a task may remain alive during all the haproxy execution. |
| 292 | */ |
| 293 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 294 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 295 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 296 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 297 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 298 | * it is used for preventing infinite loops. |
| 299 | * |
| 300 | * I test the scheer with an infinite loop containing one incrementation |
| 301 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 302 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 303 | * to one interrupt each 10 000 instructions. |
| 304 | * |
| 305 | * configured | Number of |
| 306 | * instructions | loops executed |
| 307 | * between two | in milions |
| 308 | * forced yields | |
| 309 | * ---------------+--------------- |
| 310 | * 10 | 160 |
| 311 | * 500 | 670 |
| 312 | * 1000 | 680 |
| 313 | * 5000 | 700 |
| 314 | * 7000 | 700 |
| 315 | * 8000 | 700 |
| 316 | * 9000 | 710 <- ceil |
| 317 | * 10000 | 710 |
| 318 | * 100000 | 710 |
| 319 | * 1000000 | 710 |
| 320 | * |
| 321 | */ |
| 322 | static unsigned int hlua_nb_instruction = 10000; |
| 323 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 324 | /* Descriptor for the memory allocation state. The limit is pre-initialised to |
| 325 | * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it |
| 326 | * is replaced with ~0 during post_init after everything was loaded. This way |
| 327 | * it is guaranteed that if limit is ~0 the boot is complete and that if it's |
| 328 | * zero it's not yet limited and proper accounting is required. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 329 | */ |
| 330 | struct hlua_mem_allocator { |
| 331 | size_t allocated; |
| 332 | size_t limit; |
| 333 | }; |
| 334 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 335 | static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 336 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 337 | /* These functions converts types between HAProxy internal args or |
| 338 | * sample and LUA types. Another function permits to check if the |
| 339 | * LUA stack contains arguments according with an required ARG_T |
| 340 | * format. |
| 341 | */ |
| 342 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 343 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 344 | __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] | 345 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 346 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 347 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 348 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 349 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 350 | __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] | 351 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 352 | struct prepend_path { |
| 353 | struct list l; |
| 354 | char *type; |
| 355 | char *path; |
| 356 | }; |
| 357 | |
| 358 | static struct list prepend_path_list = LIST_HEAD_INIT(prepend_path_list); |
| 359 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 360 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 361 | do { \ |
| 362 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 363 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 364 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 365 | } while (0) |
| 366 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 367 | static inline struct hlua_function *new_hlua_function() |
| 368 | { |
| 369 | struct hlua_function *fcn; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 370 | int i; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 371 | |
| 372 | fcn = calloc(1, sizeof(*fcn)); |
| 373 | if (!fcn) |
| 374 | return NULL; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 375 | LIST_APPEND(&referenced_functions, &fcn->l); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 376 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 377 | fcn->function_ref[i] = -1; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 378 | return fcn; |
| 379 | } |
| 380 | |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 381 | static inline void release_hlua_function(struct hlua_function *fcn) |
| 382 | { |
| 383 | if (!fcn) |
| 384 | return; |
| 385 | if (fcn->name) |
| 386 | ha_free(&fcn->name); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 387 | LIST_DELETE(&fcn->l); |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 388 | ha_free(&fcn); |
| 389 | } |
| 390 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 391 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 392 | static inline int fcn_ref_to_stack_id(struct hlua_function *fcn) |
| 393 | { |
| 394 | if (fcn->function_ref[0] == -1) |
| 395 | return tid + 1; |
| 396 | return 0; |
| 397 | } |
| 398 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 399 | /* Create a new registered filter. Only its name is filled */ |
| 400 | static inline struct hlua_reg_filter *new_hlua_reg_filter(const char *name) |
| 401 | { |
| 402 | struct hlua_reg_filter *reg_flt; |
| 403 | int i; |
| 404 | |
| 405 | reg_flt = calloc(1, sizeof(*reg_flt)); |
| 406 | if (!reg_flt) |
| 407 | return NULL; |
| 408 | reg_flt->name = strdup(name); |
| 409 | if (!reg_flt->name) { |
| 410 | free(reg_flt); |
| 411 | return NULL; |
| 412 | } |
| 413 | LIST_APPEND(&referenced_filters, ®_flt->l); |
| 414 | for (i = 0; i < MAX_THREADS + 1; i++) { |
| 415 | reg_flt->flt_ref[i] = -1; |
| 416 | reg_flt->fun_ref[i] = -1; |
| 417 | } |
| 418 | return reg_flt; |
| 419 | } |
| 420 | |
| 421 | /* Release a registered filter */ |
| 422 | static inline void release_hlua_reg_filter(struct hlua_reg_filter *reg_flt) |
| 423 | { |
| 424 | if (!reg_flt) |
| 425 | return; |
| 426 | if (reg_flt->name) |
| 427 | ha_free(®_flt->name); |
| 428 | LIST_DELETE(®_flt->l); |
| 429 | ha_free(®_flt); |
| 430 | } |
| 431 | |
| 432 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 433 | static inline int reg_flt_to_stack_id(struct hlua_reg_filter *reg_flt) |
| 434 | { |
| 435 | if (reg_flt->fun_ref[0] == -1) |
| 436 | return tid + 1; |
| 437 | return 0; |
| 438 | } |
| 439 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 440 | /* Used to check an Lua function type in the stack. It creates and |
| 441 | * returns a reference of the function. This function throws an |
| 442 | * error if the rgument is not a "function". |
| 443 | */ |
| 444 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 445 | { |
| 446 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 447 | 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] | 448 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 449 | } |
| 450 | lua_pushvalue(L, argno); |
| 451 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 452 | } |
| 453 | |
Christopher Faulet | ba9e21d | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 454 | /* Used to check an Lua table type in the stack. It creates and |
| 455 | * returns a reference of the table. This function throws an |
| 456 | * error if the rgument is not a "table". |
| 457 | */ |
| 458 | __LJMP unsigned int hlua_checktable(lua_State *L, int argno) |
| 459 | { |
| 460 | if (!lua_istable(L, argno)) { |
| 461 | const char *msg = lua_pushfstring(L, "table expected, got %s", luaL_typename(L, argno)); |
| 462 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 463 | } |
| 464 | lua_pushvalue(L, argno); |
| 465 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 466 | } |
| 467 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 468 | /* Return the string that is of the top of the stack. */ |
| 469 | const char *hlua_get_top_error_string(lua_State *L) |
| 470 | { |
| 471 | if (lua_gettop(L) < 1) |
| 472 | return "unknown error"; |
| 473 | if (lua_type(L, -1) != LUA_TSTRING) |
| 474 | return "unknown error"; |
| 475 | return lua_tostring(L, -1); |
| 476 | } |
| 477 | |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 478 | __LJMP const char *hlua_traceback(lua_State *L, const char* sep) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 479 | { |
| 480 | lua_Debug ar; |
| 481 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 482 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 483 | |
| 484 | while (lua_getstack(L, level++, &ar)) { |
| 485 | |
| 486 | /* Add separator */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 487 | if (b_data(msg)) |
| 488 | chunk_appendf(msg, "%s", sep); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 489 | |
| 490 | /* Fill fields: |
| 491 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 492 | * 'l': fills in the field currentline; |
| 493 | * 'n': fills in the field name and namewhat; |
| 494 | * 't': fills in the field istailcall; |
| 495 | */ |
| 496 | lua_getinfo(L, "Slnt", &ar); |
| 497 | |
| 498 | /* Append code localisation */ |
| 499 | if (ar.currentline > 0) |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 500 | chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 501 | else |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 502 | chunk_appendf(msg, "%s: ", ar.short_src); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 503 | |
| 504 | /* |
| 505 | * Get function name |
| 506 | * |
| 507 | * if namewhat is no empty, name is defined. |
| 508 | * what contains "Lua" for Lua function, "C" for C function, |
| 509 | * or "main" for main code. |
| 510 | */ |
| 511 | 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] | 512 | chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */ |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 513 | |
| 514 | 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] | 515 | chunk_appendf(msg, "in main chunk"); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 516 | |
| 517 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 518 | chunk_appendf(msg, "in function line %d", ar.linedefined); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 519 | |
| 520 | else /* nothing left... */ |
| 521 | chunk_appendf(msg, "?"); |
| 522 | |
| 523 | |
| 524 | /* Display tailed call */ |
| 525 | if (ar.istailcall) |
| 526 | chunk_appendf(msg, " ..."); |
| 527 | } |
| 528 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 529 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 533 | /* This function check the number of arguments available in the |
| 534 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 535 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 536 | */ |
| 537 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 538 | { |
| 539 | if (lua_gettop(L) == nb) |
| 540 | return; |
| 541 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 542 | } |
| 543 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 544 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 545 | * and the line number where the error is encountered. |
| 546 | */ |
| 547 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 548 | { |
| 549 | va_list argp; |
| 550 | va_start(argp, fmt); |
| 551 | luaL_where(L, 1); |
| 552 | lua_pushvfstring(L, fmt, argp); |
| 553 | va_end(argp); |
| 554 | lua_concat(L, 2); |
| 555 | return 1; |
| 556 | } |
| 557 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 558 | /* This functions is used with sample fetch and converters. It |
| 559 | * converts the HAProxy configuration argument in a lua stack |
| 560 | * values. |
| 561 | * |
| 562 | * It takes an array of "arg", and each entry of the array is |
| 563 | * converted and pushed in the LUA stack. |
| 564 | */ |
| 565 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 566 | { |
| 567 | switch (arg->type) { |
| 568 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 569 | case ARGT_TIME: |
| 570 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 571 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 572 | break; |
| 573 | |
| 574 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 575 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 576 | break; |
| 577 | |
| 578 | case ARGT_IPV4: |
| 579 | case ARGT_IPV6: |
| 580 | case ARGT_MSK4: |
| 581 | case ARGT_MSK6: |
| 582 | case ARGT_FE: |
| 583 | case ARGT_BE: |
| 584 | case ARGT_TAB: |
| 585 | case ARGT_SRV: |
| 586 | case ARGT_USR: |
| 587 | case ARGT_MAP: |
| 588 | default: |
| 589 | lua_pushnil(L); |
| 590 | break; |
| 591 | } |
| 592 | return 1; |
| 593 | } |
| 594 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 595 | /* 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] | 596 | * 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] | 597 | * with sample fetch wrappers. The input arguments are given to the |
| 598 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 599 | */ |
| 600 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 601 | { |
| 602 | switch (lua_type(L, ud)) { |
| 603 | |
| 604 | case LUA_TNUMBER: |
| 605 | case LUA_TBOOLEAN: |
| 606 | arg->type = ARGT_SINT; |
| 607 | arg->data.sint = lua_tointeger(L, ud); |
| 608 | break; |
| 609 | |
| 610 | case LUA_TSTRING: |
| 611 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 612 | 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] | 613 | /* 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] | 614 | 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] | 615 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 616 | break; |
| 617 | |
| 618 | case LUA_TUSERDATA: |
| 619 | case LUA_TNIL: |
| 620 | case LUA_TTABLE: |
| 621 | case LUA_TFUNCTION: |
| 622 | case LUA_TTHREAD: |
| 623 | case LUA_TLIGHTUSERDATA: |
| 624 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 625 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 626 | break; |
| 627 | } |
| 628 | return 1; |
| 629 | } |
| 630 | |
| 631 | /* the following functions are used to convert a struct sample |
| 632 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 633 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 634 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 635 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 636 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 637 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 638 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 639 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 640 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 641 | break; |
| 642 | |
| 643 | case SMP_T_BIN: |
| 644 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 645 | 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] | 646 | break; |
| 647 | |
| 648 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 649 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 650 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 651 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 652 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 653 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 654 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 655 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 656 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 657 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 658 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 659 | 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] | 660 | break; |
| 661 | default: |
| 662 | lua_pushnil(L); |
| 663 | break; |
| 664 | } |
| 665 | break; |
| 666 | |
| 667 | case SMP_T_IPV4: |
| 668 | case SMP_T_IPV6: |
| 669 | 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] | 670 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 671 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 672 | 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] | 673 | else |
| 674 | lua_pushnil(L); |
| 675 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 676 | default: |
| 677 | lua_pushnil(L); |
| 678 | break; |
| 679 | } |
| 680 | return 1; |
| 681 | } |
| 682 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 683 | /* the following functions are used to convert a struct sample |
| 684 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 685 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 686 | */ |
| 687 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 688 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 689 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 690 | |
| 691 | case SMP_T_BIN: |
| 692 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 693 | 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] | 694 | break; |
| 695 | |
| 696 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 697 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 698 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 699 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 700 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 701 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 702 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 703 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 704 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 705 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 706 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 707 | 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] | 708 | break; |
| 709 | default: |
| 710 | lua_pushstring(L, ""); |
| 711 | break; |
| 712 | } |
| 713 | break; |
| 714 | |
| 715 | case SMP_T_SINT: |
| 716 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 717 | case SMP_T_IPV4: |
| 718 | case SMP_T_IPV6: |
| 719 | 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] | 720 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 721 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 722 | 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] | 723 | else |
| 724 | lua_pushstring(L, ""); |
| 725 | break; |
| 726 | default: |
| 727 | lua_pushstring(L, ""); |
| 728 | break; |
| 729 | } |
| 730 | return 1; |
| 731 | } |
| 732 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 733 | /* the following functions are used to convert an Lua type in a |
| 734 | * struct sample. This is useful to provide data from a converter |
| 735 | * to the LUA code. |
| 736 | */ |
| 737 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 738 | { |
| 739 | switch (lua_type(L, ud)) { |
| 740 | |
| 741 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 742 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 743 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 744 | break; |
| 745 | |
| 746 | |
| 747 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 748 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 749 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 750 | break; |
| 751 | |
| 752 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 753 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 754 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 755 | 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] | 756 | /* 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] | 757 | 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] | 758 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 759 | break; |
| 760 | |
| 761 | case LUA_TUSERDATA: |
| 762 | case LUA_TNIL: |
| 763 | case LUA_TTABLE: |
| 764 | case LUA_TFUNCTION: |
| 765 | case LUA_TTHREAD: |
| 766 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 767 | case LUA_TNONE: |
| 768 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 769 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 770 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 771 | break; |
| 772 | } |
| 773 | return 1; |
| 774 | } |
| 775 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 776 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 777 | * 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] | 778 | * 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] | 779 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 780 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 781 | * 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] | 782 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 783 | __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] | 784 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 785 | { |
| 786 | int min_arg; |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 787 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 788 | struct proxy *px; |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 789 | struct userlist *ul; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 790 | struct my_regex *reg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 791 | const char *msg = NULL; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 792 | char *sname, *pname, *err = NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 793 | |
| 794 | idx = 0; |
| 795 | min_arg = ARGM(mask); |
| 796 | mask >>= ARGM_BITS; |
| 797 | |
| 798 | while (1) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 799 | struct buffer tmp = BUF_NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 800 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 801 | /* Check for mandatory arguments. */ |
| 802 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 803 | if (idx < min_arg) { |
| 804 | |
| 805 | /* If miss other argument than the first one, we return an error. */ |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 806 | if (idx > 0) { |
| 807 | msg = "Mandatory argument expected"; |
| 808 | goto error; |
| 809 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 810 | |
| 811 | /* If first argument have a certain type, some default values |
| 812 | * may be used. See the function smp_resolve_args(). |
| 813 | */ |
| 814 | switch (mask & ARGT_MASK) { |
| 815 | |
| 816 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 817 | if (!(p->cap & PR_CAP_FE)) { |
| 818 | msg = "Mandatory argument expected"; |
| 819 | goto error; |
| 820 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 821 | argp[idx].data.prx = p; |
| 822 | argp[idx].type = ARGT_FE; |
| 823 | argp[idx+1].type = ARGT_STOP; |
| 824 | break; |
| 825 | |
| 826 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 827 | if (!(p->cap & PR_CAP_BE)) { |
| 828 | msg = "Mandatory argument expected"; |
| 829 | goto error; |
| 830 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 831 | argp[idx].data.prx = p; |
| 832 | argp[idx].type = ARGT_BE; |
| 833 | argp[idx+1].type = ARGT_STOP; |
| 834 | break; |
| 835 | |
| 836 | case ARGT_TAB: |
| 837 | argp[idx].data.prx = p; |
| 838 | argp[idx].type = ARGT_TAB; |
| 839 | argp[idx+1].type = ARGT_STOP; |
| 840 | break; |
| 841 | |
| 842 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 843 | msg = "Mandatory argument expected"; |
| 844 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 845 | break; |
| 846 | } |
| 847 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 848 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 849 | } |
| 850 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 851 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 852 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 853 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 854 | msg = "Last argument expected"; |
| 855 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 856 | } |
| 857 | |
| 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 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 861 | } |
| 862 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 863 | /* Convert some argument types. All string in argp[] are for not |
| 864 | * duplicated yet. |
| 865 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 866 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 867 | case ARGT_SINT: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 868 | if (argp[idx].type != ARGT_SINT) { |
| 869 | msg = "integer expected"; |
| 870 | goto error; |
| 871 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 872 | argp[idx].type = ARGT_SINT; |
| 873 | break; |
| 874 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 875 | case ARGT_TIME: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 876 | if (argp[idx].type != ARGT_SINT) { |
| 877 | msg = "integer expected"; |
| 878 | goto error; |
| 879 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 880 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 881 | break; |
| 882 | |
| 883 | case ARGT_SIZE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 884 | if (argp[idx].type != ARGT_SINT) { |
| 885 | msg = "integer expected"; |
| 886 | goto error; |
| 887 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 888 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 889 | break; |
| 890 | |
| 891 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 892 | if (argp[idx].type != ARGT_STR) { |
| 893 | msg = "string expected"; |
| 894 | goto error; |
| 895 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 896 | 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] | 897 | if (!argp[idx].data.prx) { |
| 898 | msg = "frontend doesn't exist"; |
| 899 | goto error; |
| 900 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 901 | argp[idx].type = ARGT_FE; |
| 902 | break; |
| 903 | |
| 904 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 905 | if (argp[idx].type != ARGT_STR) { |
| 906 | msg = "string expected"; |
| 907 | goto error; |
| 908 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 909 | 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] | 910 | if (!argp[idx].data.prx) { |
| 911 | msg = "backend doesn't exist"; |
| 912 | goto error; |
| 913 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 914 | argp[idx].type = ARGT_BE; |
| 915 | break; |
| 916 | |
| 917 | case ARGT_TAB: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 918 | if (argp[idx].type != ARGT_STR) { |
| 919 | msg = "string expected"; |
| 920 | goto error; |
| 921 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 922 | 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] | 923 | if (!argp[idx].data.t) { |
| 924 | msg = "table doesn't exist"; |
| 925 | goto error; |
| 926 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 927 | argp[idx].type = ARGT_TAB; |
| 928 | break; |
| 929 | |
| 930 | case ARGT_SRV: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 931 | if (argp[idx].type != ARGT_STR) { |
| 932 | msg = "string expected"; |
| 933 | goto error; |
| 934 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 935 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 936 | if (sname) { |
| 937 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 938 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 939 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 940 | if (!px) { |
| 941 | msg = "backend doesn't exist"; |
| 942 | goto error; |
| 943 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 944 | } |
| 945 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 946 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 947 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 948 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 949 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 950 | if (!argp[idx].data.srv) { |
| 951 | msg = "server doesn't exist"; |
| 952 | goto error; |
| 953 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 954 | argp[idx].type = ARGT_SRV; |
| 955 | break; |
| 956 | |
| 957 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 958 | if (argp[idx].type != ARGT_STR) { |
| 959 | msg = "string expected"; |
| 960 | goto error; |
| 961 | } |
| 962 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 963 | msg = "invalid IPv4 address"; |
| 964 | goto error; |
| 965 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 966 | argp[idx].type = ARGT_IPV4; |
| 967 | break; |
| 968 | |
| 969 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 970 | if (argp[idx].type == ARGT_SINT) |
| 971 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 972 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 973 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 974 | msg = "invalid IPv4 mask"; |
| 975 | goto error; |
| 976 | } |
| 977 | } |
| 978 | else { |
| 979 | msg = "integer or string expected"; |
| 980 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 981 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 982 | argp[idx].type = ARGT_MSK4; |
| 983 | break; |
| 984 | |
| 985 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 986 | if (argp[idx].type != ARGT_STR) { |
| 987 | msg = "string expected"; |
| 988 | goto error; |
| 989 | } |
| 990 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 991 | msg = "invalid IPv6 address"; |
| 992 | goto error; |
| 993 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 994 | argp[idx].type = ARGT_IPV6; |
| 995 | break; |
| 996 | |
| 997 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 998 | if (argp[idx].type == ARGT_SINT) |
| 999 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 1000 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1001 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 1002 | msg = "invalid IPv6 mask"; |
| 1003 | goto error; |
| 1004 | } |
| 1005 | } |
| 1006 | else { |
| 1007 | msg = "integer or string expected"; |
| 1008 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1009 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 1010 | argp[idx].type = ARGT_MSK6; |
| 1011 | break; |
| 1012 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 1013 | case ARGT_REG: |
| 1014 | if (argp[idx].type != ARGT_STR) { |
| 1015 | msg = "string expected"; |
| 1016 | goto error; |
| 1017 | } |
| 1018 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 1019 | if (!reg) { |
| 1020 | msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'", |
| 1021 | argp[idx].data.str.area, err); |
| 1022 | free(err); |
| 1023 | goto error; |
| 1024 | } |
| 1025 | argp[idx].type = ARGT_REG; |
| 1026 | argp[idx].data.reg = reg; |
| 1027 | break; |
| 1028 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1029 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1030 | if (argp[idx].type != ARGT_STR) { |
| 1031 | msg = "string expected"; |
| 1032 | goto error; |
| 1033 | } |
| 1034 | if (p->uri_auth && p->uri_auth->userlist && |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1035 | strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0) |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1036 | ul = p->uri_auth->userlist; |
| 1037 | else |
| 1038 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 1039 | |
| 1040 | if (!ul) { |
| 1041 | msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area); |
| 1042 | goto error; |
| 1043 | } |
| 1044 | argp[idx].type = ARGT_USR; |
| 1045 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1046 | break; |
| 1047 | |
| 1048 | case ARGT_STR: |
| 1049 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 1050 | msg = "unable to duplicate string arg"; |
| 1051 | goto error; |
| 1052 | } |
| 1053 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1054 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1055 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1056 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1057 | msg = "type not yet supported"; |
| 1058 | goto error; |
| 1059 | break; |
| 1060 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | /* Check for type of argument. */ |
| 1064 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1065 | msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 1066 | arg_type_names[(mask & ARGT_MASK)], |
| 1067 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 1068 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | /* Next argument. */ |
| 1072 | mask >>= ARGT_BITS; |
| 1073 | idx++; |
| 1074 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1075 | return 0; |
| 1076 | |
| 1077 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 1078 | free_args(argp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1079 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 1080 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1081 | } |
| 1082 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1083 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1084 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1085 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1086 | * |
| 1087 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1088 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 1089 | */ |
| 1090 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 1091 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1092 | struct hlua **hlua = lua_getextraspace(L); |
| 1093 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1094 | } |
| 1095 | static inline void hlua_sethlua(struct hlua *hlua) |
| 1096 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1097 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 1098 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1099 | } |
| 1100 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1101 | /* This function is used to send logs. It try to send on screen (stderr) |
| 1102 | * and on the default syslog server. |
| 1103 | */ |
| 1104 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 1105 | { |
| 1106 | struct tm tm; |
| 1107 | char *p; |
| 1108 | |
| 1109 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1110 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1111 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1112 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 1113 | /* Break the message if exceed the buffer size. */ |
| 1114 | *(p-4) = ' '; |
| 1115 | *(p-3) = '.'; |
| 1116 | *(p-2) = '.'; |
| 1117 | *(p-1) = '.'; |
| 1118 | break; |
| 1119 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1120 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1121 | *p = *msg; |
| 1122 | else |
| 1123 | *p = '.'; |
| 1124 | } |
| 1125 | *p = '\0'; |
| 1126 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1127 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1128 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 1129 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 1130 | return; |
| 1131 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 1132 | get_localtime(date.tv_sec, &tm); |
| 1133 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1134 | 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] | 1135 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1136 | fflush(stderr); |
| 1137 | } |
| 1138 | } |
| 1139 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1140 | /* This function just ensure that the yield will be always |
| 1141 | * returned with a timeout and permit to set some flags |
| 1142 | */ |
| 1143 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1144 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1145 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1146 | struct hlua *hlua; |
| 1147 | |
| 1148 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1149 | hlua = hlua_gethlua(L); |
| 1150 | if (!hlua) { |
| 1151 | return; |
| 1152 | } |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1153 | |
| 1154 | /* Set the wake timeout. If timeout is required, we set |
| 1155 | * the expiration time. |
| 1156 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1157 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1158 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1159 | hlua->flags |= flags; |
| 1160 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1161 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1162 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1163 | } |
| 1164 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1165 | /* This function initialises the Lua environment stored in the stream. |
| 1166 | * 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] | 1167 | * 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] | 1168 | * |
| 1169 | * This function is particular. it initialises a new Lua thread. If the |
| 1170 | * initialisation fails (example: out of memory error), the lua function |
| 1171 | * throws an error (longjmp). |
| 1172 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1173 | * 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] | 1174 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1175 | * threads appear, the safe environment set a lock to ensure only one |
| 1176 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1177 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1178 | * |
| 1179 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1180 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1181 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1182 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1183 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1184 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1185 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1186 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1187 | 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] | 1188 | { |
| 1189 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1190 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1191 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1192 | lua->wake_time = TICK_ETERNITY; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1193 | lua->state_id = state_id; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1194 | LIST_INIT(&lua->com); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1195 | if (!already_safe) { |
| 1196 | if (!SET_SAFE_LJMP_PARENT(lua)) { |
| 1197 | lua->Tref = LUA_REFNIL; |
| 1198 | return 0; |
| 1199 | } |
| 1200 | } |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1201 | lua->T = lua_newthread(hlua_states[state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1202 | if (!lua->T) { |
| 1203 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1204 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1205 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1206 | return 0; |
| 1207 | } |
| 1208 | hlua_sethlua(lua); |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1209 | lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1210 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1211 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1212 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1213 | return 1; |
| 1214 | } |
| 1215 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1216 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1217 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 1218 | * is not freed. |
| 1219 | */ |
| 1220 | void hlua_ctx_destroy(struct hlua *lua) |
| 1221 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1222 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1223 | return; |
| 1224 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1225 | if (!lua->T) |
| 1226 | goto end; |
| 1227 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1228 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1229 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1230 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1231 | if (!SET_SAFE_LJMP(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1232 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1233 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1234 | RESET_SAFE_LJMP(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1235 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1236 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1237 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1238 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1239 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1240 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1241 | * without error, we run the GC on the thread pointer. Its freed all |
| 1242 | * the unused memory. |
| 1243 | * If the thread is finnish with an error or is currently yielded, |
| 1244 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1245 | * so e run the GC on the main thread. |
| 1246 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1247 | * the garbage collection. |
| 1248 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1249 | if (lua->gc_count) { |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1250 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1251 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1252 | lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1253 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1254 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1255 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1256 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1257 | |
| 1258 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1259 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | /* This function is used to restore the Lua context when a coroutine |
| 1263 | * fails. This function copy the common memory between old coroutine |
| 1264 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1265 | * replaced by the new coroutine. |
| 1266 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1267 | * as string error message and it is copied in the new stack. |
| 1268 | */ |
| 1269 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1270 | { |
| 1271 | lua_State *T; |
| 1272 | int new_ref; |
| 1273 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1274 | /* New Lua coroutine. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1275 | T = lua_newthread(hlua_states[lua->state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1276 | if (!T) |
| 1277 | return 0; |
| 1278 | |
| 1279 | /* Copy last error message. */ |
| 1280 | if (keep_msg) |
| 1281 | lua_xmove(lua->T, T, 1); |
| 1282 | |
| 1283 | /* Copy data between the coroutines. */ |
| 1284 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1285 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1286 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1287 | |
| 1288 | /* Destroy old data. */ |
| 1289 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1290 | |
| 1291 | /* The thread is garbage collected by Lua. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1292 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1293 | |
| 1294 | /* Fill the struct with the new coroutine values. */ |
| 1295 | lua->Mref = new_ref; |
| 1296 | lua->T = T; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1297 | lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1298 | |
| 1299 | /* Set context. */ |
| 1300 | hlua_sethlua(lua); |
| 1301 | |
| 1302 | return 1; |
| 1303 | } |
| 1304 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1305 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1306 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1307 | struct hlua *hlua; |
| 1308 | |
| 1309 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1310 | hlua = hlua_gethlua(L); |
| 1311 | if (!hlua) |
| 1312 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1313 | |
| 1314 | /* Lua cannot yield when its returning from a function, |
| 1315 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1316 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1317 | */ |
| 1318 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1319 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1320 | return; |
| 1321 | } |
| 1322 | |
| 1323 | /* restore the interrupt condition. */ |
| 1324 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1325 | |
| 1326 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1327 | * If the state is not yieldable, trying yield causes an error. |
| 1328 | */ |
| 1329 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1330 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1331 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1332 | /* If we cannot yield, update the clock and check the timeout. */ |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 1333 | clock_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1334 | hlua->run_time += now_ms - hlua->start_time; |
| 1335 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1336 | lua_pushfstring(L, "execution timeout"); |
| 1337 | WILL_LJMP(lua_error(L)); |
| 1338 | } |
| 1339 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1340 | /* Update the start time. */ |
| 1341 | hlua->start_time = now_ms; |
| 1342 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1343 | /* Try to interrupt the process at the end of the current |
| 1344 | * unyieldable function. |
| 1345 | */ |
| 1346 | 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] | 1347 | } |
| 1348 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1349 | /* This function start or resumes the Lua stack execution. If the flag |
| 1350 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1351 | * The function return an error. |
| 1352 | * |
| 1353 | * The function can returns 4 values: |
| 1354 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1355 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1356 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1357 | * - 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] | 1358 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1359 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1360 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1361 | * 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] | 1362 | * LUA code. |
| 1363 | */ |
| 1364 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1365 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1366 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1367 | int nres; |
| 1368 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1369 | int ret; |
| 1370 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1371 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1372 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1373 | /* Initialise run time counter. */ |
| 1374 | if (!HLUA_IS_RUNNING(lua)) |
| 1375 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1376 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1377 | /* Lock the whole Lua execution. This lock must be before the |
| 1378 | * label "resume_execution". |
| 1379 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1380 | if (lua->state_id == 0) |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 1381 | lua_take_global_lock(); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1382 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1383 | resume_execution: |
| 1384 | |
| 1385 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1386 | * instructions. it is used for preventing infinite loops. |
| 1387 | */ |
| 1388 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1389 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1390 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1391 | HLUA_SET_RUN(lua); |
| 1392 | HLUA_CLR_CTRLYIELD(lua); |
| 1393 | HLUA_CLR_WAKERESWR(lua); |
| 1394 | HLUA_CLR_WAKEREQWR(lua); |
Christopher Faulet | 1f43a34 | 2021-08-04 17:58:21 +0200 | [diff] [blame] | 1395 | HLUA_CLR_NOYIELD(lua); |
| 1396 | if (!yield_allowed) |
| 1397 | HLUA_SET_NOYIELD(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1398 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1399 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1400 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1401 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1402 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1403 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1404 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1405 | 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] | 1406 | #else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1407 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1408 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1409 | switch (ret) { |
| 1410 | |
| 1411 | case LUA_OK: |
| 1412 | ret = HLUA_E_OK; |
| 1413 | break; |
| 1414 | |
| 1415 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1416 | /* Check if the execution timeout is expired. It it is the case, we |
| 1417 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1418 | */ |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 1419 | clock_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1420 | lua->run_time += now_ms - lua->start_time; |
| 1421 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1422 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1423 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1424 | break; |
| 1425 | } |
| 1426 | /* Process the forced yield. if the general yield is not allowed or |
| 1427 | * if no task were associated this the current Lua execution |
| 1428 | * coroutine, we resume the execution. Else we want to return in the |
| 1429 | * scheduler and we want to be waked up again, to continue the |
| 1430 | * current Lua execution. So we schedule our own task. |
| 1431 | */ |
| 1432 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1433 | if (!yield_allowed || !lua->task) |
| 1434 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1435 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1436 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1437 | if (!yield_allowed) { |
| 1438 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1439 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1440 | break; |
| 1441 | } |
| 1442 | ret = HLUA_E_AGAIN; |
| 1443 | break; |
| 1444 | |
| 1445 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1446 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1447 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1448 | * because the errors ares the only one mean to return immediately |
| 1449 | * from and lua execution. |
| 1450 | */ |
| 1451 | if (lua->flags & HLUA_EXIT) { |
| 1452 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1453 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1454 | break; |
| 1455 | } |
| 1456 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1457 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1458 | if (!lua_checkstack(lua->T, 1)) { |
| 1459 | ret = HLUA_E_ERR; |
| 1460 | break; |
| 1461 | } |
| 1462 | msg = lua_tostring(lua->T, -1); |
| 1463 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1464 | lua_pop(lua->T, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 1465 | trace = hlua_traceback(lua->T, ", "); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1466 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1467 | 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] | 1468 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1469 | 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] | 1470 | ret = HLUA_E_ERRMSG; |
| 1471 | break; |
| 1472 | |
| 1473 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1474 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1475 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1476 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1477 | break; |
| 1478 | |
| 1479 | case LUA_ERRERR: |
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 | if (!lua_checkstack(lua->T, 1)) { |
| 1482 | ret = HLUA_E_ERR; |
| 1483 | break; |
| 1484 | } |
| 1485 | msg = lua_tostring(lua->T, -1); |
| 1486 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1487 | lua_pop(lua->T, 1); |
| 1488 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1489 | 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] | 1490 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1491 | 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] | 1492 | ret = HLUA_E_ERRMSG; |
| 1493 | break; |
| 1494 | |
| 1495 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1496 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1497 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1498 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1499 | break; |
| 1500 | } |
| 1501 | |
| 1502 | switch (ret) { |
| 1503 | case HLUA_E_AGAIN: |
| 1504 | break; |
| 1505 | |
| 1506 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1507 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1508 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1509 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1510 | break; |
| 1511 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1512 | case HLUA_E_ETMOUT: |
| 1513 | case HLUA_E_NOMEM: |
| 1514 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1515 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1516 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1517 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1518 | hlua_ctx_renew(lua, 0); |
| 1519 | break; |
| 1520 | |
| 1521 | case HLUA_E_OK: |
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 | break; |
| 1525 | } |
| 1526 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1527 | /* This is the main exit point, remove the Lua lock. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1528 | if (lua->state_id == 0) |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 1529 | lua_drop_global_lock(); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1530 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1531 | return ret; |
| 1532 | } |
| 1533 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1534 | /* This function exit the current code. */ |
| 1535 | __LJMP static int hlua_done(lua_State *L) |
| 1536 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1537 | struct hlua *hlua; |
| 1538 | |
| 1539 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1540 | hlua = hlua_gethlua(L); |
| 1541 | if (!hlua) |
| 1542 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1543 | |
| 1544 | hlua->flags |= HLUA_EXIT; |
| 1545 | WILL_LJMP(lua_error(L)); |
| 1546 | |
| 1547 | return 0; |
| 1548 | } |
| 1549 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1550 | /* This function is an LUA binding. It provides a function |
| 1551 | * for deleting ACL from a referenced ACL file. |
| 1552 | */ |
| 1553 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1554 | { |
| 1555 | const char *name; |
| 1556 | const char *key; |
| 1557 | struct pat_ref *ref; |
| 1558 | |
| 1559 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1560 | |
| 1561 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1562 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1563 | |
| 1564 | ref = pat_ref_lookup(name); |
| 1565 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1566 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1567 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1568 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1569 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1570 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1571 | return 0; |
| 1572 | } |
| 1573 | |
| 1574 | /* This function is an LUA binding. It provides a function |
| 1575 | * for deleting map entry from a referenced map file. |
| 1576 | */ |
| 1577 | static int hlua_del_map(lua_State *L) |
| 1578 | { |
| 1579 | const char *name; |
| 1580 | const char *key; |
| 1581 | struct pat_ref *ref; |
| 1582 | |
| 1583 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1584 | |
| 1585 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1586 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1587 | |
| 1588 | ref = pat_ref_lookup(name); |
| 1589 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1590 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1591 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1592 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1593 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1594 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1595 | return 0; |
| 1596 | } |
| 1597 | |
| 1598 | /* This function is an LUA binding. It provides a function |
| 1599 | * for adding ACL pattern from a referenced ACL file. |
| 1600 | */ |
| 1601 | static int hlua_add_acl(lua_State *L) |
| 1602 | { |
| 1603 | const char *name; |
| 1604 | const char *key; |
| 1605 | struct pat_ref *ref; |
| 1606 | |
| 1607 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1608 | |
| 1609 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1610 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1611 | |
| 1612 | ref = pat_ref_lookup(name); |
| 1613 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1614 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1615 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1616 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1617 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1618 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1619 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1620 | return 0; |
| 1621 | } |
| 1622 | |
| 1623 | /* This function is an LUA binding. It provides a function |
| 1624 | * for setting map pattern and sample from a referenced map |
| 1625 | * file. |
| 1626 | */ |
| 1627 | static int hlua_set_map(lua_State *L) |
| 1628 | { |
| 1629 | const char *name; |
| 1630 | const char *key; |
| 1631 | const char *value; |
| 1632 | struct pat_ref *ref; |
| 1633 | |
| 1634 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1635 | |
| 1636 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1637 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1638 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1639 | |
| 1640 | ref = pat_ref_lookup(name); |
| 1641 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1642 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1643 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1644 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1645 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1646 | pat_ref_set(ref, key, value, NULL); |
| 1647 | else |
| 1648 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1649 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1650 | return 0; |
| 1651 | } |
| 1652 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1653 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1654 | * 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] | 1655 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1656 | * the name of the object (_G[<name>] = <metable> ). |
| 1657 | * |
| 1658 | * A metable is a table that modify the standard behavior of a standard |
| 1659 | * access to the associated data. The entries of this new metatable are |
| 1660 | * defined as is: |
| 1661 | * |
| 1662 | * http://lua-users.org/wiki/MetatableEvents |
| 1663 | * |
| 1664 | * __index |
| 1665 | * |
| 1666 | * we access an absent field in a table, the result is nil. This is |
| 1667 | * true, but it is not the whole truth. Actually, such access triggers |
| 1668 | * the interpreter to look for an __index metamethod: If there is no |
| 1669 | * such method, as usually happens, then the access results in nil; |
| 1670 | * otherwise, the metamethod will provide the result. |
| 1671 | * |
| 1672 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1673 | * the key does not appear in the table, but the metatable has an __index |
| 1674 | * property: |
| 1675 | * |
| 1676 | * - if the value is a function, the function is called, passing in the |
| 1677 | * table and the key; the return value of that function is returned as |
| 1678 | * the result. |
| 1679 | * |
| 1680 | * - if the value is another table, the value of the key in that table is |
| 1681 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1682 | * table's metatable has an __index property, then it continues on up) |
| 1683 | * |
| 1684 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1685 | * |
| 1686 | * http://www.lua.org/pil/13.4.1.html |
| 1687 | * |
| 1688 | * __newindex |
| 1689 | * |
| 1690 | * Like __index, but control property assignment. |
| 1691 | * |
| 1692 | * __mode - Control weak references. A string value with one or both |
| 1693 | * of the characters 'k' and 'v' which specifies that the the |
| 1694 | * keys and/or values in the table are weak references. |
| 1695 | * |
| 1696 | * __call - Treat a table like a function. When a table is followed by |
| 1697 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1698 | * a __call key pointing to a function, that function is invoked |
| 1699 | * (passing any specified arguments) and the return value is |
| 1700 | * returned. |
| 1701 | * |
| 1702 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1703 | * called, if the metatable for myTable has a __metatable |
| 1704 | * key, the value of that key is returned instead of the |
| 1705 | * actual metatable. |
| 1706 | * |
| 1707 | * __tostring - Control string representation. When the builtin |
| 1708 | * "tostring( myTable )" function is called, if the metatable |
| 1709 | * for myTable has a __tostring property set to a function, |
| 1710 | * that function is invoked (passing myTable to it) and the |
| 1711 | * return value is used as the string representation. |
| 1712 | * |
| 1713 | * __len - Control table length. When the table length is requested using |
| 1714 | * the length operator ( '#' ), if the metatable for myTable has |
| 1715 | * a __len key pointing to a function, that function is invoked |
| 1716 | * (passing myTable to it) and the return value used as the value |
| 1717 | * of "#myTable". |
| 1718 | * |
| 1719 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1720 | * collected, if the metatable has a __gc field pointing to a |
| 1721 | * function, that function is first invoked, passing the userdata |
| 1722 | * to it. The __gc metamethod is not called for tables. |
| 1723 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1724 | * |
| 1725 | * Special metamethods for redefining standard operators: |
| 1726 | * http://www.lua.org/pil/13.1.html |
| 1727 | * |
| 1728 | * __add "+" |
| 1729 | * __sub "-" |
| 1730 | * __mul "*" |
| 1731 | * __div "/" |
| 1732 | * __unm "!" |
| 1733 | * __pow "^" |
| 1734 | * __concat ".." |
| 1735 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1736 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1737 | * http://www.lua.org/pil/13.2.html |
| 1738 | * |
| 1739 | * __eq "==" |
| 1740 | * __lt "<" |
| 1741 | * __le "<=" |
| 1742 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1743 | |
| 1744 | /* |
| 1745 | * |
| 1746 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1747 | * Class Map |
| 1748 | * |
| 1749 | * |
| 1750 | */ |
| 1751 | |
| 1752 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1753 | * a class session, otherwise it throws an error. |
| 1754 | */ |
| 1755 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1756 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1757 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1758 | } |
| 1759 | |
| 1760 | /* This function is the map constructor. It don't need |
| 1761 | * the class Map object. It creates and return a new Map |
| 1762 | * object. It must be called only during "body" or "init" |
| 1763 | * context because it process some filesystem accesses. |
| 1764 | */ |
| 1765 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1766 | { |
| 1767 | const char *fn; |
| 1768 | int match = PAT_MATCH_STR; |
| 1769 | struct sample_conv conv; |
| 1770 | const char *file = ""; |
| 1771 | int line = 0; |
| 1772 | lua_Debug ar; |
| 1773 | char *err = NULL; |
| 1774 | struct arg args[2]; |
| 1775 | |
| 1776 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1777 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1778 | |
| 1779 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1780 | |
| 1781 | if (lua_gettop(L) >= 2) { |
| 1782 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1783 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1784 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1785 | } |
| 1786 | |
| 1787 | /* Get Lua filename and line number. */ |
| 1788 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1789 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1790 | if (ar.currentline > 0) { /* is there info? */ |
| 1791 | file = ar.short_src; |
| 1792 | line = ar.currentline; |
| 1793 | } |
| 1794 | } |
| 1795 | |
| 1796 | /* fill fake sample_conv struct. */ |
| 1797 | conv.kw = ""; /* unused. */ |
| 1798 | conv.process = NULL; /* unused. */ |
| 1799 | conv.arg_mask = 0; /* unused. */ |
| 1800 | conv.val_args = NULL; /* unused. */ |
| 1801 | conv.out_type = SMP_T_STR; |
| 1802 | conv.private = (void *)(long)match; |
| 1803 | switch (match) { |
| 1804 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1805 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1806 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1807 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1808 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1809 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1810 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1811 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1812 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1813 | default: |
| 1814 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1815 | } |
| 1816 | |
| 1817 | /* fill fake args. */ |
| 1818 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1819 | args[0].data.str.area = strdup(fn); |
| 1820 | args[0].data.str.data = strlen(fn); |
| 1821 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1822 | args[1].type = ARGT_STOP; |
| 1823 | |
| 1824 | /* load the map. */ |
| 1825 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1826 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1827 | * free the err variable. |
| 1828 | */ |
| 1829 | luaL_where(L, 1); |
| 1830 | lua_pushfstring(L, "'new': %s.", err); |
| 1831 | lua_concat(L, 2); |
| 1832 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1833 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1834 | WILL_LJMP(lua_error(L)); |
| 1835 | } |
| 1836 | |
| 1837 | /* create the lua object. */ |
| 1838 | lua_newtable(L); |
| 1839 | lua_pushlightuserdata(L, args[0].data.map); |
| 1840 | lua_rawseti(L, -2, 0); |
| 1841 | |
| 1842 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1843 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1844 | lua_setmetatable(L, -2); |
| 1845 | |
| 1846 | |
| 1847 | return 1; |
| 1848 | } |
| 1849 | |
| 1850 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1851 | { |
| 1852 | struct map_descriptor *desc; |
| 1853 | struct pattern *pat; |
| 1854 | struct sample smp; |
| 1855 | |
| 1856 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1857 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1858 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1859 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1860 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1861 | } |
| 1862 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1863 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1864 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1865 | 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] | 1866 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1867 | } |
| 1868 | |
| 1869 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1870 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1871 | if (str) |
| 1872 | lua_pushstring(L, ""); |
| 1873 | else |
| 1874 | lua_pushnil(L); |
| 1875 | return 1; |
| 1876 | } |
| 1877 | |
| 1878 | /* 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] | 1879 | 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] | 1880 | return 1; |
| 1881 | } |
| 1882 | |
| 1883 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1884 | { |
| 1885 | return _hlua_map_lookup(L, 0); |
| 1886 | } |
| 1887 | |
| 1888 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1889 | { |
| 1890 | return _hlua_map_lookup(L, 1); |
| 1891 | } |
| 1892 | |
| 1893 | /* |
| 1894 | * |
| 1895 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1896 | * Class Socket |
| 1897 | * |
| 1898 | * |
| 1899 | */ |
| 1900 | |
| 1901 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1902 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1903 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1904 | } |
| 1905 | |
| 1906 | /* 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] | 1907 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1908 | * received. |
| 1909 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1910 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1911 | { |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1912 | struct conn_stream *cs = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1913 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1914 | if (appctx->ctx.hlua_cosocket.die) { |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame] | 1915 | cs_shutw(cs); |
| 1916 | cs_shutr(cs); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1917 | cs_ic(cs)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1918 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1919 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1920 | stream_shutdown(__cs_strm(cs), SF_ERR_KILLED); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1921 | } |
| 1922 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1923 | /* If we can't write, wakeup the pending write signals. */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1924 | if (channel_output_closed(cs_ic(cs))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1925 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1926 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1927 | /* If we can't read, wakeup the pending read signals. */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1928 | if (channel_input_closed(cs_oc(cs))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1929 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1930 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1931 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1932 | * to be notified whenever the connection completes. |
| 1933 | */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1934 | if (cs_opposite(cs)->state < CS_ST_EST) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 1935 | cs_cant_get(cs); |
| 1936 | cs_rx_conn_blk(cs); |
| 1937 | cs_rx_endp_more(cs); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1938 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1939 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1940 | |
| 1941 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1942 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1943 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1944 | /* 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] | 1945 | if (channel_may_recv(cs_ic(cs))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1946 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1947 | |
| 1948 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1949 | if (!channel_is_empty(cs_oc(cs))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1950 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1951 | |
| 1952 | /* Some data were injected in the buffer, notify the stream |
| 1953 | * interface. |
| 1954 | */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1955 | if (!channel_is_empty(cs_ic(cs))) |
Christopher Faulet | 13045f0 | 2022-04-01 14:23:38 +0200 | [diff] [blame] | 1956 | cs_update(cs); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1957 | |
| 1958 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1959 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1960 | */ |
| 1961 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 1962 | cs_rx_endp_more(cs); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1963 | } |
| 1964 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1965 | /* This function is called when the "struct stream" is destroyed. |
| 1966 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1967 | * Wake all the pending signals. |
| 1968 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1969 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1970 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1971 | struct xref *peer; |
| 1972 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1973 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1974 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1975 | if (peer) |
| 1976 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1977 | |
| 1978 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1979 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1980 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1981 | } |
| 1982 | |
| 1983 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1984 | * uses this object. If the stream does not exists, just quit. |
| 1985 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1986 | * pending signal can rest in the read and write lists. destroy |
| 1987 | * it. |
| 1988 | */ |
| 1989 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1990 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1991 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1992 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1993 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1994 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1995 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1996 | |
| 1997 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1998 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1999 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2000 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2001 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2002 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2003 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2004 | appctx->ctx.hlua_cosocket.die = 1; |
| 2005 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2006 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2007 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2008 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2009 | return 0; |
| 2010 | } |
| 2011 | |
| 2012 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2013 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2014 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2015 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2016 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2017 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2018 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2019 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2020 | struct hlua *hlua; |
| 2021 | |
| 2022 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2023 | hlua = hlua_gethlua(L); |
| 2024 | if (!hlua) |
| 2025 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2026 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2027 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2028 | |
| 2029 | /* Check if we run on the same thread than the xreator thread. |
| 2030 | * We cannot access to the socket if the thread is different. |
| 2031 | */ |
| 2032 | if (socket->tid != tid) |
| 2033 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2034 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2035 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2036 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2037 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2038 | |
| 2039 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2040 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2041 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2042 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2043 | appctx->ctx.hlua_cosocket.die = 1; |
| 2044 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2045 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2046 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2047 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2048 | return 0; |
| 2049 | } |
| 2050 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2051 | /* The close function calls close_helper. |
| 2052 | */ |
| 2053 | __LJMP static int hlua_socket_close(lua_State *L) |
| 2054 | { |
| 2055 | MAY_LJMP(check_args(L, 1, "close")); |
| 2056 | return hlua_socket_close_helper(L); |
| 2057 | } |
| 2058 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2059 | /* This Lua function assumes that the stack contain three parameters. |
| 2060 | * 1 - USERDATA containing a struct socket |
| 2061 | * 2 - INTEGER with values of the macro defined below |
| 2062 | * If the integer is -1, we must read at most one line. |
| 2063 | * If the integer is -2, we ust read all the data until the |
| 2064 | * end of the stream. |
| 2065 | * If the integer is positive value, we must read a number of |
| 2066 | * bytes corresponding to this value. |
| 2067 | */ |
| 2068 | #define HLSR_READ_LINE (-1) |
| 2069 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2070 | __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] | 2071 | { |
| 2072 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2073 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2074 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2075 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2076 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2077 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2078 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2079 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2080 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2081 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2082 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 2083 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2084 | struct stream *s; |
| 2085 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2086 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2087 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2088 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2089 | hlua = hlua_gethlua(L); |
| 2090 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2091 | /* Check if this lua stack is schedulable. */ |
| 2092 | if (!hlua || !hlua->task) |
| 2093 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 2094 | "'frontend', 'backend' or 'task'")); |
| 2095 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2096 | /* Check if we run on the same thread than the xreator thread. |
| 2097 | * We cannot access to the socket if the thread is different. |
| 2098 | */ |
| 2099 | if (socket->tid != tid) |
| 2100 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2101 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2102 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2103 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2104 | if (!peer) |
| 2105 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2106 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2107 | s = __cs_strm(appctx->owner); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2108 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2109 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2110 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2111 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2112 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2113 | if (nblk < 0) /* Connection close. */ |
| 2114 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2115 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2116 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2117 | |
| 2118 | /* remove final \r\n. */ |
| 2119 | if (nblk == 1) { |
| 2120 | if (blk1[len1-1] == '\n') { |
| 2121 | len1--; |
| 2122 | skip_at_end++; |
| 2123 | if (blk1[len1-1] == '\r') { |
| 2124 | len1--; |
| 2125 | skip_at_end++; |
| 2126 | } |
| 2127 | } |
| 2128 | } |
| 2129 | else { |
| 2130 | if (blk2[len2-1] == '\n') { |
| 2131 | len2--; |
| 2132 | skip_at_end++; |
| 2133 | if (blk2[len2-1] == '\r') { |
| 2134 | len2--; |
| 2135 | skip_at_end++; |
| 2136 | } |
| 2137 | } |
| 2138 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2139 | } |
| 2140 | |
| 2141 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2142 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2143 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2144 | if (nblk < 0) /* Connection close. */ |
| 2145 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2146 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2147 | goto connection_empty; |
| 2148 | } |
| 2149 | |
| 2150 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2151 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2152 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2153 | if (nblk < 0) /* Connection close. */ |
| 2154 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2155 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2156 | goto connection_empty; |
| 2157 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2158 | missing_bytes = wanted - socket->b.n; |
| 2159 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2160 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2161 | len1 = missing_bytes; |
| 2162 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2163 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2164 | } |
| 2165 | |
| 2166 | len = len1; |
| 2167 | |
| 2168 | luaL_addlstring(&socket->b, blk1, len1); |
| 2169 | if (nblk == 2) { |
| 2170 | len += len2; |
| 2171 | luaL_addlstring(&socket->b, blk2, len2); |
| 2172 | } |
| 2173 | |
| 2174 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2175 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2176 | |
| 2177 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2178 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2179 | |
| 2180 | /* If the pattern reclaim to read all the data |
| 2181 | * in the connection, got out. |
| 2182 | */ |
| 2183 | if (wanted == HLSR_READ_ALL) |
| 2184 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2185 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2186 | goto connection_empty; |
| 2187 | |
| 2188 | /* Return result. */ |
| 2189 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2190 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2191 | return 1; |
| 2192 | |
| 2193 | connection_closed: |
| 2194 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2195 | xref_unlock(&socket->xref, peer); |
| 2196 | |
| 2197 | no_peer: |
| 2198 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2199 | /* If the buffer containds data. */ |
| 2200 | if (socket->b.n > 0) { |
| 2201 | luaL_pushresult(&socket->b); |
| 2202 | return 1; |
| 2203 | } |
| 2204 | lua_pushnil(L); |
| 2205 | lua_pushstring(L, "connection closed."); |
| 2206 | return 2; |
| 2207 | |
| 2208 | connection_empty: |
| 2209 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2210 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 2211 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2212 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2213 | } |
| 2214 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2215 | 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] | 2216 | return 0; |
| 2217 | } |
| 2218 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2219 | /* This Lua function gets two parameters. The first one can be string |
| 2220 | * or a number. If the string is "*l", the user requires one line. If |
| 2221 | * 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] | 2222 | * If the value is a number, the user require a number of bytes equal |
| 2223 | * to the value. The default value is "*l" (a line). |
| 2224 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2225 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2226 | * integer takes this values: |
| 2227 | * -1 : read a line |
| 2228 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2229 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2230 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2231 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2232 | * concatenated with the read data. |
| 2233 | */ |
| 2234 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2235 | { |
| 2236 | int wanted = HLSR_READ_LINE; |
| 2237 | const char *pattern; |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2238 | int lastarg, type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2239 | char *error; |
| 2240 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2241 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2242 | |
| 2243 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2244 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2245 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2246 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2247 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2248 | /* Check if we run on the same thread than the xreator thread. |
| 2249 | * We cannot access to the socket if the thread is different. |
| 2250 | */ |
| 2251 | if (socket->tid != tid) |
| 2252 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2253 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2254 | /* check for pattern. */ |
| 2255 | if (lua_gettop(L) >= 2) { |
| 2256 | type = lua_type(L, 2); |
| 2257 | if (type == LUA_TSTRING) { |
| 2258 | pattern = lua_tostring(L, 2); |
| 2259 | if (strcmp(pattern, "*a") == 0) |
| 2260 | wanted = HLSR_READ_ALL; |
| 2261 | else if (strcmp(pattern, "*l") == 0) |
| 2262 | wanted = HLSR_READ_LINE; |
| 2263 | else { |
| 2264 | wanted = strtoll(pattern, &error, 10); |
| 2265 | if (*error != '\0') |
| 2266 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2267 | } |
| 2268 | } |
| 2269 | else if (type == LUA_TNUMBER) { |
| 2270 | wanted = lua_tointeger(L, 2); |
| 2271 | if (wanted < 0) |
| 2272 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2273 | } |
| 2274 | } |
| 2275 | |
| 2276 | /* Set pattern. */ |
| 2277 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2278 | |
| 2279 | /* Check if we would replace the top by itself. */ |
| 2280 | if (lua_gettop(L) != 2) |
| 2281 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2282 | |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2283 | /* Save index of the top of the stack because since buffers are used, it |
| 2284 | * may change |
| 2285 | */ |
| 2286 | lastarg = lua_gettop(L); |
| 2287 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2288 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2289 | luaL_buffinit(L, &socket->b); |
| 2290 | |
| 2291 | /* Check prefix. */ |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2292 | if (lastarg >= 3) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2293 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2294 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2295 | pattern = lua_tolstring(L, 3, &len); |
| 2296 | luaL_addlstring(&socket->b, pattern, len); |
| 2297 | } |
| 2298 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2299 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2300 | } |
| 2301 | |
| 2302 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2303 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2304 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2305 | 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] | 2306 | { |
| 2307 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2308 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2309 | struct appctx *appctx; |
| 2310 | size_t buf_len; |
| 2311 | const char *buf; |
| 2312 | int len; |
| 2313 | int send_len; |
| 2314 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2315 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2316 | struct stream *s; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2317 | struct conn_stream *cs; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2318 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2319 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2320 | hlua = hlua_gethlua(L); |
| 2321 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2322 | /* Check if this lua stack is schedulable. */ |
| 2323 | if (!hlua || !hlua->task) |
| 2324 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2325 | "'frontend', 'backend' or 'task'")); |
| 2326 | |
| 2327 | /* Get object */ |
| 2328 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2329 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2330 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2331 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2332 | /* Check if we run on the same thread than the xreator thread. |
| 2333 | * We cannot access to the socket if the thread is different. |
| 2334 | */ |
| 2335 | if (socket->tid != tid) |
| 2336 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2337 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2338 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2339 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2340 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2341 | lua_pushinteger(L, -1); |
| 2342 | return 1; |
| 2343 | } |
| 2344 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2345 | cs = appctx->owner; |
| 2346 | s = __cs_strm(cs); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2347 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2348 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2349 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2350 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2351 | lua_pushinteger(L, -1); |
| 2352 | return 1; |
| 2353 | } |
| 2354 | |
| 2355 | /* Update the input buffer data. */ |
| 2356 | buf += sent; |
| 2357 | send_len = buf_len - sent; |
| 2358 | |
| 2359 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2360 | if (sent >= buf_len) { |
| 2361 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2362 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2363 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2364 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2365 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2366 | * the request buffer if its not required. |
| 2367 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2368 | if (s->req.buf.size == 0) { |
Christopher Faulet | 8f45eec | 2022-04-01 17:19:36 +0200 | [diff] [blame] | 2369 | if (!cs_alloc_ibuf(cs, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2370 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2371 | } |
| 2372 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2373 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2374 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2375 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2376 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2377 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2378 | |
| 2379 | /* send data */ |
| 2380 | if (len < send_len) |
| 2381 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2382 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2383 | |
| 2384 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2385 | * the data" (-2) are not expected because the available length |
| 2386 | * is tested. |
| 2387 | * Other unknown error are also not expected. |
| 2388 | */ |
| 2389 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2390 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2391 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2392 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2393 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2394 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2395 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2396 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2397 | return 1; |
| 2398 | } |
| 2399 | |
| 2400 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2401 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2402 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2403 | s->req.rex = TICK_ETERNITY; |
| 2404 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2405 | |
| 2406 | /* Update length sent. */ |
| 2407 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2408 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2409 | |
| 2410 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2411 | if (sent + len >= buf_len) { |
| 2412 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2413 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2414 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2415 | |
| 2416 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2417 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2418 | xref_unlock(&socket->xref, peer); |
| 2419 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2420 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2421 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2422 | 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] | 2423 | return 0; |
| 2424 | } |
| 2425 | |
| 2426 | /* This function initiate the send of data. It just check the input |
| 2427 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2428 | * 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] | 2429 | * "hlua_socket_write_yield" that can yield. |
| 2430 | * |
| 2431 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2432 | * the associated object. The second is a string buffer. The third is |
| 2433 | * a facultative integer that represents where is the buffer position |
| 2434 | * of the start of the data that can send. The first byte is the |
| 2435 | * position "1". The default value is "1". The fourth argument is a |
| 2436 | * facultative integer that represents where is the buffer position |
| 2437 | * of the end of the data that can send. The default is the last byte. |
| 2438 | */ |
| 2439 | static int hlua_socket_send(struct lua_State *L) |
| 2440 | { |
| 2441 | int i; |
| 2442 | int j; |
| 2443 | const char *buf; |
| 2444 | size_t buf_len; |
| 2445 | |
| 2446 | /* Check number of arguments. */ |
| 2447 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2448 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2449 | |
| 2450 | /* Get the string. */ |
| 2451 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2452 | |
| 2453 | /* Get and check j. */ |
| 2454 | if (lua_gettop(L) == 4) { |
| 2455 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2456 | if (j < 0) |
| 2457 | j = buf_len + j + 1; |
| 2458 | if (j > buf_len) |
| 2459 | j = buf_len + 1; |
| 2460 | lua_pop(L, 1); |
| 2461 | } |
| 2462 | else |
| 2463 | j = buf_len; |
| 2464 | |
| 2465 | /* Get and check i. */ |
| 2466 | if (lua_gettop(L) == 3) { |
| 2467 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2468 | if (i < 0) |
| 2469 | i = buf_len + i + 1; |
| 2470 | if (i > buf_len) |
| 2471 | i = buf_len + 1; |
| 2472 | lua_pop(L, 1); |
| 2473 | } else |
| 2474 | i = 1; |
| 2475 | |
| 2476 | /* Check bth i and j. */ |
| 2477 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2478 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2479 | return 1; |
| 2480 | } |
| 2481 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2482 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2483 | return 1; |
| 2484 | } |
| 2485 | if (i == 0) |
| 2486 | i = 1; |
| 2487 | if (j == 0) |
| 2488 | j = 1; |
| 2489 | |
| 2490 | /* Pop the string. */ |
| 2491 | lua_pop(L, 1); |
| 2492 | |
| 2493 | /* Update the buffer length. */ |
| 2494 | buf += i - 1; |
| 2495 | buf_len = j - i + 1; |
| 2496 | lua_pushlstring(L, buf, buf_len); |
| 2497 | |
| 2498 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2499 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2500 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2501 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2502 | } |
| 2503 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2504 | #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] | 2505 | __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] | 2506 | { |
| 2507 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2508 | int ret; |
| 2509 | int len; |
| 2510 | char *p; |
| 2511 | |
| 2512 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2513 | if (ret <= 0) { |
| 2514 | lua_pushnil(L); |
| 2515 | return 1; |
| 2516 | } |
| 2517 | |
| 2518 | if (ret == AF_UNIX) { |
| 2519 | lua_pushstring(L, buffer+1); |
| 2520 | return 1; |
| 2521 | } |
| 2522 | else if (ret == AF_INET6) { |
| 2523 | buffer[0] = '['; |
| 2524 | len = strlen(buffer); |
| 2525 | buffer[len] = ']'; |
| 2526 | len++; |
| 2527 | buffer[len] = ':'; |
| 2528 | len++; |
| 2529 | p = buffer; |
| 2530 | } |
| 2531 | else if (ret == AF_INET) { |
| 2532 | p = buffer + 1; |
| 2533 | len = strlen(p); |
| 2534 | p[len] = ':'; |
| 2535 | len++; |
| 2536 | } |
| 2537 | else { |
| 2538 | lua_pushnil(L); |
| 2539 | return 1; |
| 2540 | } |
| 2541 | |
| 2542 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2543 | lua_pushnil(L); |
| 2544 | return 1; |
| 2545 | } |
| 2546 | |
| 2547 | lua_pushstring(L, p); |
| 2548 | return 1; |
| 2549 | } |
| 2550 | |
| 2551 | /* Returns information about the peer of the connection. */ |
| 2552 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2553 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2554 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2555 | struct xref *peer; |
| 2556 | struct appctx *appctx; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2557 | struct conn_stream *cs; |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2558 | const struct sockaddr_storage *dst; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2559 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2560 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2561 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2562 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2563 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2564 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2565 | /* Check if we run on the same thread than the xreator thread. |
| 2566 | * We cannot access to the socket if the thread is different. |
| 2567 | */ |
| 2568 | if (socket->tid != tid) |
| 2569 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2570 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2571 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2572 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2573 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2574 | lua_pushnil(L); |
| 2575 | return 1; |
| 2576 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2577 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2578 | cs = appctx->owner; |
Christopher Faulet | 8da67aa | 2022-03-29 17:53:09 +0200 | [diff] [blame] | 2579 | dst = cs_dst(cs_opposite(cs)); |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2580 | if (!dst) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2581 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2582 | lua_pushnil(L); |
| 2583 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2584 | } |
| 2585 | |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2586 | ret = MAY_LJMP(hlua_socket_info(L, dst)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2587 | xref_unlock(&socket->xref, peer); |
| 2588 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2589 | } |
| 2590 | |
| 2591 | /* Returns information about my connection side. */ |
| 2592 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2593 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2594 | struct hlua_socket *socket; |
| 2595 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2596 | struct appctx *appctx; |
| 2597 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2598 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2599 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2600 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2601 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2602 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2603 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2604 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2605 | /* Check if we run on the same thread than the xreator thread. |
| 2606 | * We cannot access to the socket if the thread is different. |
| 2607 | */ |
| 2608 | if (socket->tid != tid) |
| 2609 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2610 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2611 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2612 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2613 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2614 | lua_pushnil(L); |
| 2615 | return 1; |
| 2616 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2617 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2618 | s = __cs_strm(appctx->owner); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2619 | |
Christopher Faulet | 95a61e8 | 2021-12-22 14:22:03 +0100 | [diff] [blame] | 2620 | conn = cs_conn(s->csb); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2621 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2622 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2623 | lua_pushnil(L); |
| 2624 | return 1; |
| 2625 | } |
| 2626 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2627 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2628 | xref_unlock(&socket->xref, peer); |
| 2629 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2630 | } |
| 2631 | |
| 2632 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2633 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2634 | .obj_type = OBJ_TYPE_APPLET, |
| 2635 | .name = "<LUA_TCP>", |
| 2636 | .fct = hlua_socket_handler, |
| 2637 | .release = hlua_socket_release, |
| 2638 | }; |
| 2639 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2640 | __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] | 2641 | { |
| 2642 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2643 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2644 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2645 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2646 | struct stream *s; |
| 2647 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2648 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2649 | hlua = hlua_gethlua(L); |
| 2650 | if (!hlua) |
| 2651 | return 0; |
| 2652 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2653 | /* Check if we run on the same thread than the xreator thread. |
| 2654 | * We cannot access to the socket if the thread is different. |
| 2655 | */ |
| 2656 | if (socket->tid != tid) |
| 2657 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2658 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2659 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2660 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2661 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2662 | lua_pushnil(L); |
| 2663 | lua_pushstring(L, "Can't connect"); |
| 2664 | return 2; |
| 2665 | } |
| 2666 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2667 | s = __cs_strm(appctx->owner); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2668 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2669 | /* Check if we run on the same thread than the xreator thread. |
| 2670 | * We cannot access to the socket if the thread is different. |
| 2671 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2672 | if (socket->tid != tid) { |
| 2673 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2674 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2675 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2676 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2677 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2678 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2679 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2680 | lua_pushnil(L); |
| 2681 | lua_pushstring(L, "Can't connect"); |
| 2682 | return 2; |
| 2683 | } |
| 2684 | |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 2685 | appctx = __cs_appctx(s->csf); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2686 | |
| 2687 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2688 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2689 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2690 | lua_pushinteger(L, 1); |
| 2691 | return 1; |
| 2692 | } |
| 2693 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2694 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2695 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2696 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2697 | } |
| 2698 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2699 | 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] | 2700 | return 0; |
| 2701 | } |
| 2702 | |
| 2703 | /* This function fail or initite the connection. */ |
| 2704 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2705 | { |
| 2706 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2707 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2708 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2709 | struct hlua *hlua; |
| 2710 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2711 | int low, high; |
| 2712 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2713 | struct xref *peer; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2714 | struct conn_stream *cs; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2715 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2716 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2717 | if (lua_gettop(L) < 2) |
| 2718 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2719 | |
| 2720 | /* Get args. */ |
| 2721 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2722 | |
| 2723 | /* Check if we run on the same thread than the xreator thread. |
| 2724 | * We cannot access to the socket if the thread is different. |
| 2725 | */ |
| 2726 | if (socket->tid != tid) |
| 2727 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2728 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2729 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2730 | if (lua_gettop(L) >= 3) { |
| 2731 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2732 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2733 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2734 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2735 | * that are not enclosed within square brackets. |
| 2736 | */ |
| 2737 | if (port > 0) { |
| 2738 | luaL_buffinit(L, &b); |
| 2739 | luaL_addstring(&b, ip); |
| 2740 | luaL_addchar(&b, ':'); |
| 2741 | luaL_pushresult(&b); |
| 2742 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2743 | } |
| 2744 | } |
| 2745 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2746 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2747 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2748 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2749 | lua_pushnil(L); |
| 2750 | return 1; |
| 2751 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2752 | |
| 2753 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2754 | 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] | 2755 | if (!addr) { |
| 2756 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2757 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2758 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2759 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2760 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2761 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2762 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2763 | if (port == -1) { |
| 2764 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2765 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2766 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2767 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2768 | } else if (addr->ss_family == AF_INET6) { |
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_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2774 | } |
| 2775 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2776 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2777 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2778 | cs = appctx->owner; |
| 2779 | s = __cs_strm(cs); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2780 | |
Christopher Faulet | 8da67aa | 2022-03-29 17:53:09 +0200 | [diff] [blame] | 2781 | if (!sockaddr_alloc(&cs_opposite(cs)->dst, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2782 | xref_unlock(&socket->xref, peer); |
| 2783 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2784 | } |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2785 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2786 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2787 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2788 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2789 | if (!hlua) |
| 2790 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2791 | |
| 2792 | /* inform the stream that we want to be notified whenever the |
| 2793 | * connection completes. |
| 2794 | */ |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 2795 | cs_cant_get(s->csf); |
| 2796 | cs_rx_endp_more(s->csf); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2797 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2798 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2799 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2800 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2801 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2802 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2803 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2804 | } |
| 2805 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2806 | |
| 2807 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2808 | /* Return yield waiting for connection. */ |
| 2809 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2810 | 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] | 2811 | |
| 2812 | return 0; |
| 2813 | } |
| 2814 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2815 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2816 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2817 | { |
| 2818 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2819 | struct xref *peer; |
| 2820 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2821 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2822 | |
| 2823 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2824 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2825 | |
| 2826 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2827 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2828 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2829 | lua_pushnil(L); |
| 2830 | return 1; |
| 2831 | } |
| 2832 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2833 | s = __cs_strm(appctx->owner); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2834 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2835 | s->target = &socket_ssl->obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2836 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2837 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2838 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2839 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2840 | |
| 2841 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2842 | { |
| 2843 | return 0; |
| 2844 | } |
| 2845 | |
| 2846 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2847 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2848 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2849 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2850 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2851 | struct xref *peer; |
| 2852 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2853 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2854 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2855 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2856 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2857 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2858 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2859 | /* convert the timeout to millis */ |
| 2860 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2861 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2862 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2863 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2864 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2865 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2866 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2867 | 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] | 2868 | |
| 2869 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2870 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2871 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2872 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2873 | /* Check if we run on the same thread than the xreator thread. |
| 2874 | * We cannot access to the socket if the thread is different. |
| 2875 | */ |
| 2876 | if (socket->tid != tid) |
| 2877 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2878 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2879 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2880 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2881 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2882 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2883 | WILL_LJMP(lua_error(L)); |
| 2884 | return 0; |
| 2885 | } |
| 2886 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2887 | s = __cs_strm(appctx->owner); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2888 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2889 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2890 | s->req.rto = tmout; |
| 2891 | s->req.wto = tmout; |
| 2892 | s->res.rto = tmout; |
| 2893 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2894 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2895 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2896 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2897 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2898 | |
| 2899 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2900 | task_queue(s->task); |
| 2901 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2902 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2903 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2904 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2905 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2906 | } |
| 2907 | |
| 2908 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2909 | { |
| 2910 | struct hlua_socket *socket; |
| 2911 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2912 | struct session *sess; |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 2913 | struct conn_stream *cs; |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 2914 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2915 | |
| 2916 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2917 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2918 | hlua_pusherror(L, "socket: full stack"); |
| 2919 | goto out_fail_conf; |
| 2920 | } |
| 2921 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2922 | /* Create the object: obj[0] = userdata. */ |
| 2923 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2924 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2925 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2926 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2927 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2928 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2929 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2930 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2931 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2932 | goto out_fail_conf; |
| 2933 | } |
| 2934 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2935 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2936 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2937 | lua_setmetatable(L, -2); |
| 2938 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2939 | /* Create the applet context */ |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 2940 | appctx = appctx_new(&update_applet, NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2941 | if (!appctx) { |
| 2942 | hlua_pusherror(L, "socket: out of memory"); |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 2943 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2944 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2945 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2946 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2947 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2948 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2949 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2950 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2951 | /* Now create a session, task and stream for this applet */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 2952 | sess = session_new(socket_proxy, NULL, &appctx->obj_type); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2953 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2954 | hlua_pusherror(L, "socket: out of memory"); |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 2955 | goto out_fail_appctx; |
| 2956 | } |
| 2957 | |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 2958 | cs = cs_new_from_applet(appctx->endp, sess, &BUF_NULL); |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 2959 | if (!cs) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2960 | hlua_pusherror(L, "socket: out of memory"); |
Christopher Faulet | 2479e5f | 2022-01-19 14:50:11 +0100 | [diff] [blame] | 2961 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2962 | } |
| 2963 | |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 2964 | s = DISGUISE(cs_strm(cs)); |
Christopher Faulet | 2479e5f | 2022-01-19 14:50:11 +0100 | [diff] [blame] | 2965 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2966 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2967 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2968 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2969 | /* Configure "right" stream interface. this "si" is used to connect |
| 2970 | * and retrieve data from the server. The connection is initialized |
| 2971 | * with the "struct server". |
| 2972 | */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 2973 | cs_set_state(s->csb, CS_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2974 | |
| 2975 | /* Force destination server. */ |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 2976 | s->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
| 2977 | s->target = &socket_tcp->obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2978 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2979 | return 1; |
| 2980 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2981 | out_fail_sess: |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 2982 | session_free(sess); |
| 2983 | out_fail_appctx: |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2984 | appctx_free(appctx); |
| 2985 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2986 | WILL_LJMP(lua_error(L)); |
| 2987 | return 0; |
| 2988 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2989 | |
| 2990 | /* |
| 2991 | * |
| 2992 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2993 | * Class Channel |
| 2994 | * |
| 2995 | * |
| 2996 | */ |
| 2997 | |
| 2998 | /* Returns the struct hlua_channel join to the class channel in the |
| 2999 | * stack entry "ud" or throws an argument error. |
| 3000 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3001 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3002 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3003 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3004 | } |
| 3005 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3006 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 3007 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3008 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 3009 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3010 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3011 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3012 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3013 | return 0; |
| 3014 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3015 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3016 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3017 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3018 | |
| 3019 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3020 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 3021 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3022 | return 1; |
| 3023 | } |
| 3024 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3025 | /* Helper function returning a filter attached to a channel at the position <ud> |
| 3026 | * 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] | 3027 | * filter is attached, NULL is returned and <offset> and <len> are not |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3028 | * initialized. |
| 3029 | */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3030 | 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] | 3031 | { |
| 3032 | struct filter *filter = NULL; |
| 3033 | |
| 3034 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 3035 | struct hlua_flt_ctx *flt_ctx; |
| 3036 | |
| 3037 | filter = lua_touserdata (L, -1); |
| 3038 | flt_ctx = filter->ctx; |
| 3039 | if (hlua_filter_from_payload(filter)) { |
| 3040 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 3041 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 3042 | } |
| 3043 | } |
| 3044 | |
| 3045 | lua_pop(L, 1); |
| 3046 | return filter; |
| 3047 | } |
| 3048 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3049 | /* Copies <len> bytes of data present in the channel's buffer, starting at the |
| 3050 | * 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] | 3051 | * responsibility to ensure <len> and <offset> are valid. It always return the |
| 3052 | * length of the built string. <len> may be 0, in this case, an empty string is |
| 3053 | * created and 0 is returned. |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3054 | */ |
| 3055 | 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] | 3056 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3057 | size_t block1, block2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3058 | luaL_Buffer b; |
| 3059 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3060 | block1 = len; |
| 3061 | if (block1 > b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset))) |
| 3062 | block1 = b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset)); |
| 3063 | block2 = len - block1; |
| 3064 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3065 | luaL_buffinit(L, &b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3066 | luaL_addlstring(&b, b_peek(&chn->buf, offset), block1); |
| 3067 | if (block2) |
| 3068 | luaL_addlstring(&b, b_orig(&chn->buf), block2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3069 | luaL_pushresult(&b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3070 | return len; |
| 3071 | } |
| 3072 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3073 | /* Inserts the string <str> to the channel's buffer at the offset <offset>. This |
| 3074 | * function returns -1 if data cannot be copied. Otherwise, it returns the |
| 3075 | * number of bytes copied. |
| 3076 | */ |
| 3077 | static int _hlua_channel_insert(struct channel *chn, lua_State *L, struct ist str, size_t offset) |
| 3078 | { |
| 3079 | int ret = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3080 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3081 | /* Nothing to do, just return */ |
| 3082 | if (unlikely(istlen(str) == 0)) |
| 3083 | goto end; |
| 3084 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3085 | if (istlen(str) > c_room(chn)) { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3086 | ret = -1; |
| 3087 | goto end; |
| 3088 | } |
| 3089 | ret = b_insert_blk(&chn->buf, offset, istptr(str), istlen(str)); |
| 3090 | |
| 3091 | end: |
| 3092 | return ret; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3093 | } |
| 3094 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3095 | /* Removes <len> bytes of data at the absolute position <offset>. |
| 3096 | */ |
| 3097 | static void _hlua_channel_delete(struct channel *chn, size_t offset, size_t len) |
| 3098 | { |
| 3099 | size_t end = offset + len; |
| 3100 | |
| 3101 | if (b_peek(&chn->buf, end) != b_tail(&chn->buf)) |
| 3102 | b_move(&chn->buf, b_peek_ofs(&chn->buf, end), |
| 3103 | b_data(&chn->buf) - end, -len); |
| 3104 | b_sub(&chn->buf, len); |
| 3105 | } |
| 3106 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3107 | /* Copies input data in the channel's buffer. It is possible to set a specific |
| 3108 | * offset (0 by default) and a length (all remaining input data starting for the |
| 3109 | * offset by default). If there is not enough input data and more data can be |
| 3110 | * received, this function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3111 | * |
| 3112 | * From an action, All input data are considered. For a filter, the offset and |
| 3113 | * 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] | 3114 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3115 | __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] | 3116 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3117 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3118 | struct filter *filter; |
| 3119 | size_t input, output; |
| 3120 | int offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3121 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3122 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3123 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3124 | output = co_data(chn); |
| 3125 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3126 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3127 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3128 | if (filter && !hlua_filter_from_payload(filter)) |
| 3129 | WILL_LJMP(lua_error(L)); |
| 3130 | |
| 3131 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3132 | if (lua_gettop(L) > 1) { |
| 3133 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3134 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3135 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3136 | offset += output; |
| 3137 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3138 | lua_pushfstring(L, "offset out of range."); |
| 3139 | WILL_LJMP(lua_error(L)); |
| 3140 | } |
| 3141 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3142 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3143 | if (lua_gettop(L) == 3) { |
| 3144 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3145 | if (!len) |
| 3146 | goto dup; |
| 3147 | if (len == -1) |
| 3148 | len = global.tune.bufsize; |
| 3149 | if (len < 0) { |
| 3150 | lua_pushfstring(L, "length out of range."); |
| 3151 | WILL_LJMP(lua_error(L)); |
| 3152 | } |
| 3153 | } |
| 3154 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3155 | if (offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3156 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3157 | /* Yield waiting for more data, as requested */ |
| 3158 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_data_yield, TICK_ETERNITY, 0)); |
| 3159 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3160 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3161 | } |
| 3162 | |
| 3163 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3164 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3165 | return 1; |
| 3166 | } |
| 3167 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3168 | /* Copies the first line (including the trailing LF) of input data in the |
| 3169 | * channel's buffer. It is possible to set a specific offset (0 by default) and |
| 3170 | * a length (all remaining input data starting for the offset by default). If |
| 3171 | * there is not enough input data and more data can be received, the function |
| 3172 | * yields. If a length is explicitly specified, no more data are |
| 3173 | * copied. Otherwise, if no LF is found and more data can be received, this |
| 3174 | * function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3175 | * |
| 3176 | * From an action, All input data are considered. For a filter, the offset and |
| 3177 | * 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] | 3178 | */ |
| 3179 | __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] | 3180 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3181 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3182 | struct filter *filter; |
| 3183 | size_t l, input, output; |
| 3184 | int offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3185 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3186 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3187 | output = co_data(chn); |
| 3188 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3189 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3190 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3191 | if (filter && !hlua_filter_from_payload(filter)) |
| 3192 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3193 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3194 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3195 | if (lua_gettop(L) > 1) { |
| 3196 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3197 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3198 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3199 | offset += output; |
| 3200 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3201 | lua_pushfstring(L, "offset out of range."); |
| 3202 | WILL_LJMP(lua_error(L)); |
| 3203 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3204 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3205 | |
| 3206 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3207 | if (lua_gettop(L) == 3) { |
| 3208 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3209 | if (!len) |
| 3210 | goto dup; |
| 3211 | if (len == -1) |
| 3212 | len = global.tune.bufsize; |
| 3213 | if (len < 0) { |
| 3214 | lua_pushfstring(L, "length out of range."); |
| 3215 | WILL_LJMP(lua_error(L)); |
| 3216 | } |
| 3217 | } |
| 3218 | |
| 3219 | for (l = 0; l < len; l++) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3220 | if (l + offset >= output + input) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3221 | break; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3222 | if (*(b_peek(&chn->buf, offset + l)) == '\n') { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3223 | len = l+1; |
| 3224 | goto dup; |
| 3225 | } |
| 3226 | } |
| 3227 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3228 | if (offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3229 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3230 | /* Yield waiting for more data */ |
| 3231 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_line_yield, TICK_ETERNITY, 0)); |
| 3232 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3233 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3234 | } |
| 3235 | |
| 3236 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3237 | _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3238 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3239 | } |
| 3240 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3241 | /* [ DEPRECATED ] |
| 3242 | * |
| 3243 | * Duplicate all input data foud in the channel's buffer. The data are not |
| 3244 | * removed from the buffer. This function relies on _hlua_channel_dup(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3245 | * |
| 3246 | * From an action, All input data are considered. For a filter, the offset and |
| 3247 | * 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] | 3248 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3249 | __LJMP static int hlua_channel_dup(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3250 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3251 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3252 | struct filter *filter; |
| 3253 | size_t offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3254 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3255 | MAY_LJMP(check_args(L, 1, "dup")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3256 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3257 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3258 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3259 | WILL_LJMP(lua_error(L)); |
| 3260 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3261 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3262 | offset = co_data(chn); |
| 3263 | len = ci_data(chn); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3264 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3265 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3266 | if (filter && !hlua_filter_from_payload(filter)) |
| 3267 | WILL_LJMP(lua_error(L)); |
| 3268 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3269 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3270 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3271 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3272 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3273 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3274 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3275 | return 1; |
| 3276 | } |
| 3277 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3278 | /* [ DEPRECATED ] |
| 3279 | * |
| 3280 | * Get all input data foud in the channel's buffer. The data are removed from |
| 3281 | * the buffer after the copy. This function relies on _hlua_channel_dup() and |
| 3282 | * _hlua_channel_delete(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3283 | * |
| 3284 | * From an action, All input data are considered. For a filter, the offset and |
| 3285 | * 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] | 3286 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3287 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3288 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3289 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3290 | struct filter *filter; |
| 3291 | size_t offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3292 | int ret; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3293 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3294 | MAY_LJMP(check_args(L, 1, "get")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3295 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3296 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3297 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3298 | WILL_LJMP(lua_error(L)); |
| 3299 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3300 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3301 | offset = co_data(chn); |
| 3302 | len = ci_data(chn); |
| 3303 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3304 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3305 | if (filter && !hlua_filter_from_payload(filter)) |
| 3306 | WILL_LJMP(lua_error(L)); |
| 3307 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3308 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3309 | lua_pushnil(L); |
| 3310 | return 1; |
| 3311 | } |
| 3312 | |
| 3313 | ret = _hlua_channel_dup(chn, L, offset, len); |
| 3314 | _hlua_channel_delete(chn, offset, ret); |
| 3315 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3316 | } |
| 3317 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3318 | /* This functions consumes and returns one line. If the channel is closed, |
| 3319 | * 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] | 3320 | * 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] | 3321 | * value. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3322 | * |
| 3323 | * From an action, All input data are considered. For a filter, the offset and |
| 3324 | * 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] | 3325 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3326 | __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] | 3327 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3328 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3329 | struct filter *filter; |
| 3330 | size_t l, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3331 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3332 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3333 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3334 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3335 | offset = co_data(chn); |
| 3336 | len = ci_data(chn); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3337 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3338 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3339 | if (filter && !hlua_filter_from_payload(filter)) |
| 3340 | WILL_LJMP(lua_error(L)); |
| 3341 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3342 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3343 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3344 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3345 | } |
| 3346 | |
| 3347 | for (l = 0; l < len; l++) { |
| 3348 | if (*(b_peek(&chn->buf, offset+l)) == '\n') { |
| 3349 | len = l+1; |
| 3350 | goto dup; |
| 3351 | } |
| 3352 | } |
| 3353 | |
| 3354 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3355 | /* Yield waiting for more data */ |
| 3356 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0)); |
| 3357 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3358 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3359 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3360 | ret = _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3361 | _hlua_channel_delete(chn, offset, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3362 | return 1; |
| 3363 | } |
| 3364 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3365 | /* [ DEPRECATED ] |
| 3366 | * |
| 3367 | * Check arguments for the function "hlua_channel_getline_yield". |
| 3368 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3369 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3370 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3371 | struct channel *chn; |
| 3372 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3373 | MAY_LJMP(check_args(L, 1, "getline")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3374 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3375 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3376 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3377 | WILL_LJMP(lua_error(L)); |
| 3378 | } |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3379 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3380 | } |
| 3381 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3382 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3383 | * available input data are returned. The offset may be negactive to start from |
| 3384 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3385 | * size. |
| 3386 | */ |
| 3387 | __LJMP static int hlua_channel_get_data(lua_State *L) |
| 3388 | { |
| 3389 | struct channel *chn; |
| 3390 | |
| 3391 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3392 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 3393 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3394 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3395 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3396 | WILL_LJMP(lua_error(L)); |
| 3397 | } |
| 3398 | return MAY_LJMP(hlua_channel_get_data_yield(L, 0, 0)); |
| 3399 | } |
| 3400 | |
| 3401 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3402 | * available input data are returned. The offset may be negactive to start from |
| 3403 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3404 | * size. |
| 3405 | */ |
| 3406 | __LJMP static int hlua_channel_get_line(lua_State *L) |
| 3407 | { |
| 3408 | struct channel *chn; |
| 3409 | |
| 3410 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3411 | WILL_LJMP(luaL_error(L, "'line' expects at most 2 arguments")); |
| 3412 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3413 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3414 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3415 | WILL_LJMP(lua_error(L)); |
| 3416 | } |
| 3417 | return MAY_LJMP(hlua_channel_get_line_yield(L, 0, 0)); |
| 3418 | } |
| 3419 | |
| 3420 | /* Appends a string into the input side of channel. It returns the length of the |
| 3421 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3422 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3423 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3424 | * |
| 3425 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3426 | */ |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3427 | __LJMP static int hlua_channel_append(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3428 | { |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3429 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3430 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3431 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3432 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3433 | int ret; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3434 | |
| 3435 | MAY_LJMP(check_args(L, 2, "append")); |
| 3436 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3437 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3438 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3439 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3440 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3441 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3442 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3443 | offset = co_data(chn); |
| 3444 | len = ci_data(chn); |
| 3445 | |
| 3446 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3447 | if (filter && !hlua_filter_from_payload(filter)) |
| 3448 | WILL_LJMP(lua_error(L)); |
| 3449 | |
| 3450 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3451 | if (ret > 0 && filter) { |
| 3452 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3453 | |
| 3454 | flt_update_offsets(filter, chn, ret); |
| 3455 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3456 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3457 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3458 | return 1; |
| 3459 | } |
| 3460 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3461 | /* Prepends a string into the input side of channel. It returns the length of the |
| 3462 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3463 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3464 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3465 | * |
| 3466 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3467 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3468 | __LJMP static int hlua_channel_prepend(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3469 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3470 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3471 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3472 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3473 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3474 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3475 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3476 | MAY_LJMP(check_args(L, 2, "prepend")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3477 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3478 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3479 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3480 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3481 | WILL_LJMP(lua_error(L)); |
| 3482 | } |
| 3483 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3484 | offset = co_data(chn); |
| 3485 | len = ci_data(chn); |
| 3486 | |
| 3487 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3488 | if (filter && !hlua_filter_from_payload(filter)) |
| 3489 | WILL_LJMP(lua_error(L)); |
| 3490 | |
| 3491 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3492 | if (ret > 0 && filter) { |
| 3493 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3494 | |
| 3495 | flt_update_offsets(filter, chn, ret); |
| 3496 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3497 | } |
| 3498 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3499 | lua_pushinteger(L, ret); |
| 3500 | return 1; |
| 3501 | } |
| 3502 | |
| 3503 | /* Inserts a given amount of input data at the given offset by a string |
| 3504 | * content. By default the string is appended at the end of input data. It |
| 3505 | * returns the length of the written string, or -1 if the channel is closed or |
| 3506 | * if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3507 | * |
| 3508 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3509 | */ |
| 3510 | __LJMP static int hlua_channel_insert_data(lua_State *L) |
| 3511 | { |
| 3512 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3513 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3514 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3515 | size_t sz, input, output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3516 | int ret, offset; |
| 3517 | |
| 3518 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 3519 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
| 3520 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3521 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3522 | |
| 3523 | output = co_data(chn); |
| 3524 | input = ci_data(chn); |
| 3525 | |
| 3526 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3527 | if (filter && !hlua_filter_from_payload(filter)) |
| 3528 | WILL_LJMP(lua_error(L)); |
| 3529 | |
| 3530 | offset = input + output; |
| 3531 | if (lua_gettop(L) > 2) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3532 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3533 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3534 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3535 | offset += output; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3536 | if (offset < output || offset > output + input) { |
| 3537 | lua_pushfstring(L, "offset out of range."); |
| 3538 | WILL_LJMP(lua_error(L)); |
| 3539 | } |
| 3540 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3541 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3542 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3543 | WILL_LJMP(lua_error(L)); |
| 3544 | } |
| 3545 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3546 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3547 | if (ret > 0 && filter) { |
| 3548 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3549 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3550 | flt_update_offsets(filter, chn, ret); |
| 3551 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3552 | } |
| 3553 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3554 | lua_pushinteger(L, ret); |
| 3555 | return 1; |
| 3556 | } |
| 3557 | /* Replaces a given amount of input data at the given offset by a string |
| 3558 | * content. By default all remaining data are removed (offset = 0 and len = |
| 3559 | * -1). It returns the length of the written string, or -1 if the channel is |
| 3560 | * closed or if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3561 | * |
| 3562 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3563 | */ |
| 3564 | __LJMP static int hlua_channel_set_data(lua_State *L) |
| 3565 | { |
| 3566 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3567 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3568 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3569 | size_t sz, input, output; |
| 3570 | int ret, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3571 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3572 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 3573 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 3574 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3575 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3576 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3577 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3578 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3579 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3580 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3581 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3582 | output = co_data(chn); |
| 3583 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3584 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3585 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3586 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3587 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3588 | |
| 3589 | offset = output; |
| 3590 | if (lua_gettop(L) > 2) { |
| 3591 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3592 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3593 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3594 | offset += output; |
| 3595 | if (offset < output || offset > input + output) { |
| 3596 | lua_pushfstring(L, "offset out of range."); |
| 3597 | WILL_LJMP(lua_error(L)); |
| 3598 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3599 | } |
| 3600 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3601 | len = output + input - offset; |
| 3602 | if (lua_gettop(L) == 4) { |
| 3603 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 3604 | if (!len) |
| 3605 | goto set; |
| 3606 | if (len == -1) |
| 3607 | len = output + input - offset; |
| 3608 | if (len < 0 || offset + len > output + input) { |
| 3609 | lua_pushfstring(L, "length out of range."); |
| 3610 | WILL_LJMP(lua_error(L)); |
| 3611 | } |
| 3612 | } |
| 3613 | |
| 3614 | set: |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3615 | /* 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] | 3616 | if (sz > c_room(chn) + len) |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3617 | lua_pushinteger(L, -1); |
| 3618 | else { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3619 | _hlua_channel_delete(chn, offset, len); |
| 3620 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3621 | if (filter) { |
| 3622 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3623 | |
| 3624 | len -= (ret > 0 ? ret : 0); |
| 3625 | flt_update_offsets(filter, chn, -len); |
| 3626 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3627 | } |
| 3628 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3629 | lua_pushinteger(L, ret); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3630 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3631 | return 1; |
| 3632 | } |
| 3633 | |
| 3634 | /* 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] | 3635 | * 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] | 3636 | * the removed data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3637 | * |
| 3638 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3639 | */ |
| 3640 | __LJMP static int hlua_channel_del_data(lua_State *L) |
| 3641 | { |
| 3642 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3643 | struct filter *filter; |
| 3644 | size_t input, output; |
| 3645 | int offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3646 | |
| 3647 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3648 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
| 3649 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3650 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3651 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3652 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3653 | WILL_LJMP(lua_error(L)); |
| 3654 | } |
| 3655 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3656 | output = co_data(chn); |
| 3657 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3658 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3659 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3660 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3661 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3662 | |
| 3663 | offset = output; |
| 3664 | if (lua_gettop(L) > 2) { |
| 3665 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3666 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3667 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3668 | offset += output; |
| 3669 | if (offset < output || offset > input + output) { |
| 3670 | lua_pushfstring(L, "offset out of range."); |
| 3671 | WILL_LJMP(lua_error(L)); |
| 3672 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3673 | } |
| 3674 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3675 | len = output + input - offset; |
| 3676 | if (lua_gettop(L) == 4) { |
| 3677 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 3678 | if (!len) |
| 3679 | goto end; |
| 3680 | if (len == -1) |
| 3681 | len = output + input - offset; |
| 3682 | if (len < 0 || offset + len > output + input) { |
| 3683 | lua_pushfstring(L, "length out of range."); |
| 3684 | WILL_LJMP(lua_error(L)); |
| 3685 | } |
| 3686 | } |
| 3687 | |
| 3688 | _hlua_channel_delete(chn, offset, len); |
| 3689 | if (filter) { |
| 3690 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3691 | |
| 3692 | flt_update_offsets(filter, chn, -len); |
| 3693 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3694 | } |
| 3695 | |
| 3696 | end: |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3697 | lua_pushinteger(L, len); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3698 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3699 | } |
| 3700 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3701 | /* Append data in the output side of the buffer. This data is immediately |
| 3702 | * sent. The function returns the amount of data written. If the buffer |
| 3703 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3704 | * if the channel is closed. |
| 3705 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3706 | __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] | 3707 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3708 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3709 | struct filter *filter; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3710 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3711 | size_t offset, len, sz; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3712 | int l, ret; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3713 | struct hlua *hlua; |
| 3714 | |
| 3715 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3716 | hlua = hlua_gethlua(L); |
| 3717 | if (!hlua) { |
| 3718 | lua_pushnil(L); |
| 3719 | return 1; |
| 3720 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3721 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3722 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3723 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3724 | l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3725 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3726 | offset = co_data(chn); |
| 3727 | len = ci_data(chn); |
| 3728 | |
| 3729 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3730 | if (filter && !hlua_filter_from_payload(filter)) |
| 3731 | WILL_LJMP(lua_error(L)); |
| 3732 | |
| 3733 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3734 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3735 | lua_pushinteger(L, -1); |
| 3736 | return 1; |
| 3737 | } |
| 3738 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3739 | len = c_room(chn); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3740 | if (len > sz -l) { |
| 3741 | if (filter) { |
| 3742 | lua_pushinteger(L, -1); |
| 3743 | return 1; |
| 3744 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3745 | len = sz - l; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3746 | } |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3747 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3748 | ret = _hlua_channel_insert(chn, L, ist2(str, len), offset); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3749 | if (ret == -1) { |
| 3750 | lua_pop(L, 1); |
| 3751 | lua_pushinteger(L, -1); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3752 | return 1; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3753 | } |
| 3754 | if (ret) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3755 | if (filter) { |
| 3756 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3757 | |
| 3758 | |
| 3759 | flt_update_offsets(filter, chn, ret); |
| 3760 | FLT_OFF(filter, chn) += ret; |
| 3761 | flt_ctx->cur_off[CHN_IDX(chn)] += ret; |
| 3762 | } |
| 3763 | else |
| 3764 | c_adv(chn, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3765 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3766 | l += ret; |
| 3767 | lua_pop(L, 1); |
| 3768 | lua_pushinteger(L, l); |
| 3769 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3770 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3771 | if (l < sz) { |
| 3772 | /* Yield only if the channel's output is not empty. |
| 3773 | * Otherwise it means we cannot add more data. */ |
| 3774 | if (co_data(chn) == 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3775 | return 1; |
| 3776 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3777 | /* If we are waiting for space in the response buffer, we |
| 3778 | * must set the flag WAKERESWR. This flag required the task |
| 3779 | * wake up if any activity is detected on the response buffer. |
| 3780 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3781 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3782 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3783 | else |
| 3784 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3785 | 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] | 3786 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3787 | |
| 3788 | return 1; |
| 3789 | } |
| 3790 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3791 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3792 | * yield the LUA process, and resume it without checking the |
| 3793 | * input arguments. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3794 | * |
| 3795 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3796 | */ |
| 3797 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3798 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3799 | struct channel *chn; |
| 3800 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3801 | MAY_LJMP(check_args(L, 2, "send")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3802 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3803 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3804 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3805 | WILL_LJMP(lua_error(L)); |
| 3806 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3807 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3808 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3809 | } |
| 3810 | |
| 3811 | /* This function forward and amount of butes. The data pass from |
| 3812 | * the input side of the buffer to the output side, and can be |
| 3813 | * forwarded. This function never fails. |
| 3814 | * |
| 3815 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3816 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3817 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3818 | __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] | 3819 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3820 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3821 | struct filter *filter; |
| 3822 | size_t offset, len, fwd; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3823 | int l, max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3824 | struct hlua *hlua; |
| 3825 | |
| 3826 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3827 | hlua = hlua_gethlua(L); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3828 | if (!hlua) { |
| 3829 | lua_pushnil(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3830 | return 1; |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3831 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3832 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3833 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3834 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3835 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3836 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3837 | offset = co_data(chn); |
| 3838 | len = ci_data(chn); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3839 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3840 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3841 | if (filter && !hlua_filter_from_payload(filter)) |
| 3842 | WILL_LJMP(lua_error(L)); |
| 3843 | |
| 3844 | max = fwd - l; |
| 3845 | if (max > len) |
| 3846 | max = len; |
| 3847 | |
| 3848 | if (filter) { |
| 3849 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3850 | |
| 3851 | FLT_OFF(filter, chn) += max; |
| 3852 | flt_ctx->cur_off[CHN_IDX(chn)] += max; |
| 3853 | flt_ctx->cur_len[CHN_IDX(chn)] -= max; |
| 3854 | } |
| 3855 | else |
| 3856 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3857 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3858 | l += max; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3859 | lua_pop(L, 1); |
| 3860 | lua_pushinteger(L, l); |
| 3861 | |
| 3862 | /* Check if it miss bytes to forward. */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3863 | if (l < fwd) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3864 | /* The the input channel or the output channel are closed, we |
| 3865 | * must return the amount of data forwarded. |
| 3866 | */ |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3867 | 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] | 3868 | return 1; |
| 3869 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3870 | /* If we are waiting for space data in the response buffer, we |
| 3871 | * must set the flag WAKERESWR. This flag required the task |
| 3872 | * wake up if any activity is detected on the response buffer. |
| 3873 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3874 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3875 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3876 | else |
| 3877 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3878 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3879 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3880 | 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] | 3881 | } |
| 3882 | |
| 3883 | return 1; |
| 3884 | } |
| 3885 | |
| 3886 | /* Just check the input and prepare the stack for the previous |
| 3887 | * function "hlua_channel_forward_yield" |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3888 | * |
| 3889 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3890 | */ |
| 3891 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3892 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3893 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3894 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3895 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3896 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3897 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3898 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3899 | WILL_LJMP(lua_error(L)); |
| 3900 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3901 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3902 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3903 | } |
| 3904 | |
| 3905 | /* Just returns the number of bytes available in the input |
| 3906 | * side of the buffer. This function never fails. |
| 3907 | */ |
| 3908 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3909 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3910 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3911 | struct filter *filter; |
| 3912 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3913 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3914 | MAY_LJMP(check_args(L, 1, "input")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3915 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3916 | |
| 3917 | output = co_data(chn); |
| 3918 | input = ci_data(chn); |
| 3919 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3920 | if (filter || !IS_HTX_STRM(chn_strm(chn))) |
| 3921 | lua_pushinteger(L, input); |
| 3922 | else { |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3923 | struct htx *htx = htxbuf(&chn->buf); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3924 | |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3925 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3926 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3927 | return 1; |
| 3928 | } |
| 3929 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3930 | /* Returns true if the channel is full. */ |
| 3931 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3932 | { |
| 3933 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3934 | |
| 3935 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3936 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3937 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3938 | * applet). |
| 3939 | */ |
| 3940 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3941 | return 1; |
| 3942 | } |
| 3943 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3944 | /* Returns true if the channel may still receive data. */ |
| 3945 | __LJMP static int hlua_channel_may_recv(lua_State *L) |
| 3946 | { |
| 3947 | struct channel *chn; |
| 3948 | |
| 3949 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 3950 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3951 | lua_pushboolean(L, (!channel_input_closed(chn) && channel_may_recv(chn))); |
| 3952 | return 1; |
| 3953 | } |
| 3954 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3955 | /* Returns true if the channel is the response channel. */ |
| 3956 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3957 | { |
| 3958 | struct channel *chn; |
| 3959 | |
| 3960 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3961 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3962 | |
| 3963 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3964 | return 1; |
| 3965 | } |
| 3966 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3967 | /* Just returns the number of bytes available in the output |
| 3968 | * side of the buffer. This function never fails. |
| 3969 | */ |
| 3970 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3971 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3972 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3973 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3974 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3975 | MAY_LJMP(check_args(L, 1, "output")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3976 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3977 | |
| 3978 | output = co_data(chn); |
| 3979 | input = ci_data(chn); |
| 3980 | hlua_channel_filter(L, 1, chn, &output, &input); |
| 3981 | |
| 3982 | lua_pushinteger(L, output); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3983 | return 1; |
| 3984 | } |
| 3985 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3986 | /* |
| 3987 | * |
| 3988 | * |
| 3989 | * Class Fetches |
| 3990 | * |
| 3991 | * |
| 3992 | */ |
| 3993 | |
| 3994 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3995 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3996 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3997 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3998 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3999 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4000 | } |
| 4001 | |
| 4002 | /* This function creates and push in the stack a fetch object according |
| 4003 | * with a current TXN. |
| 4004 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4005 | 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] | 4006 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4007 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4008 | |
| 4009 | /* Check stack size. */ |
| 4010 | if (!lua_checkstack(L, 3)) |
| 4011 | return 0; |
| 4012 | |
| 4013 | /* Create the object: obj[0] = userdata. |
| 4014 | * Note that the base of the Fetches object is the |
| 4015 | * transaction object. |
| 4016 | */ |
| 4017 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4018 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4019 | lua_rawseti(L, -2, 0); |
| 4020 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4021 | hsmp->s = txn->s; |
| 4022 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4023 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4024 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4025 | |
| 4026 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 4027 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 4028 | lua_setmetatable(L, -2); |
| 4029 | |
| 4030 | return 1; |
| 4031 | } |
| 4032 | |
| 4033 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 4034 | * It uses closure argument to store the associated sample-fetch. It |
| 4035 | * returns only one argument or throws an error. An error is thrown |
| 4036 | * only if an error is encountered during the argument parsing. If |
| 4037 | * the "sample-fetch" function fails, nil is returned. |
| 4038 | */ |
| 4039 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 4040 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4041 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4042 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4043 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4044 | int i; |
| 4045 | struct sample smp; |
| 4046 | |
| 4047 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4048 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4049 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4050 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4051 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4052 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 4053 | /* Check execution authorization. */ |
| 4054 | if (f->use & SMP_USE_HTTP_ANY && |
| 4055 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 4056 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 4057 | "is not available in Lua services", f->kw); |
| 4058 | WILL_LJMP(lua_error(L)); |
| 4059 | } |
| 4060 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4061 | /* Get extra arguments. */ |
| 4062 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 4063 | if (i >= ARGM_NBARGS) |
| 4064 | break; |
| 4065 | hlua_lua2arg(L, i + 2, &args[i]); |
| 4066 | } |
| 4067 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4068 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4069 | |
| 4070 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4071 | 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] | 4072 | |
| 4073 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4074 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4075 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4076 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4077 | } |
| 4078 | |
| 4079 | /* Initialise the sample. */ |
| 4080 | memset(&smp, 0, sizeof(smp)); |
| 4081 | |
| 4082 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4083 | 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] | 4084 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4085 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4086 | lua_pushstring(L, ""); |
| 4087 | else |
| 4088 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4089 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4090 | } |
| 4091 | |
| 4092 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4093 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4094 | hlua_smp2lua_str(L, &smp); |
| 4095 | else |
| 4096 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4097 | |
| 4098 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4099 | free_args(args); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4100 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4101 | |
| 4102 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4103 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4104 | WILL_LJMP(lua_error(L)); |
| 4105 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4106 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4107 | |
| 4108 | /* |
| 4109 | * |
| 4110 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4111 | * Class Converters |
| 4112 | * |
| 4113 | * |
| 4114 | */ |
| 4115 | |
| 4116 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4117 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4118 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4119 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4120 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4121 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4122 | } |
| 4123 | |
| 4124 | /* This function creates and push in the stack a Converters object |
| 4125 | * according with a current TXN. |
| 4126 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4127 | 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] | 4128 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4129 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4130 | |
| 4131 | /* Check stack size. */ |
| 4132 | if (!lua_checkstack(L, 3)) |
| 4133 | return 0; |
| 4134 | |
| 4135 | /* Create the object: obj[0] = userdata. |
| 4136 | * Note that the base of the Converters object is the |
| 4137 | * same than the TXN object. |
| 4138 | */ |
| 4139 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4140 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4141 | lua_rawseti(L, -2, 0); |
| 4142 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4143 | hsmp->s = txn->s; |
| 4144 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4145 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4146 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4147 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4148 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4149 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 4150 | lua_setmetatable(L, -2); |
| 4151 | |
| 4152 | return 1; |
| 4153 | } |
| 4154 | |
| 4155 | /* This function is an LUA binding. It is called with each converter. |
| 4156 | * It uses closure argument to store the associated converter. It |
| 4157 | * returns only one argument or throws an error. An error is thrown |
| 4158 | * only if an error is encountered during the argument parsing. If |
| 4159 | * the converter function function fails, nil is returned. |
| 4160 | */ |
| 4161 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 4162 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4163 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4164 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4165 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4166 | int i; |
| 4167 | struct sample smp; |
| 4168 | |
| 4169 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4170 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4171 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4172 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4173 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4174 | |
| 4175 | /* Get extra arguments. */ |
| 4176 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 4177 | if (i >= ARGM_NBARGS) |
| 4178 | break; |
| 4179 | hlua_lua2arg(L, i + 3, &args[i]); |
| 4180 | } |
| 4181 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4182 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4183 | |
| 4184 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4185 | 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] | 4186 | |
| 4187 | /* Run the special args checker. */ |
| 4188 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 4189 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4190 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4191 | } |
| 4192 | |
| 4193 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4194 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4195 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 4196 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4197 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4198 | } |
| 4199 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4200 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 4201 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4202 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4203 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4204 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4205 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4206 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4207 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4208 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 4209 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4210 | hlua_pusherror(L, "error during the input argument casting"); |
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 | } |
| 4213 | |
| 4214 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4215 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4216 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4217 | lua_pushstring(L, ""); |
| 4218 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4219 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4220 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4221 | } |
| 4222 | |
| 4223 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4224 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4225 | hlua_smp2lua_str(L, &smp); |
| 4226 | else |
| 4227 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4228 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4229 | free_args(args); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4230 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4231 | |
| 4232 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4233 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4234 | WILL_LJMP(lua_error(L)); |
| 4235 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4236 | } |
| 4237 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4238 | /* |
| 4239 | * |
| 4240 | * |
| 4241 | * Class AppletTCP |
| 4242 | * |
| 4243 | * |
| 4244 | */ |
| 4245 | |
| 4246 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4247 | * a class stream, otherwise it throws an error. |
| 4248 | */ |
| 4249 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 4250 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4251 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4252 | } |
| 4253 | |
| 4254 | /* This function creates and push in the stack an Applet object |
| 4255 | * according with a current TXN. |
| 4256 | */ |
| 4257 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 4258 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4259 | struct hlua_appctx *luactx; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4260 | struct stream *s = __cs_strm(ctx->owner); |
Christopher Faulet | 2da02ae | 2022-02-24 13:45:27 +0100 | [diff] [blame] | 4261 | struct proxy *p; |
| 4262 | |
| 4263 | ALREADY_CHECKED(s); |
| 4264 | p = s->be; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4265 | |
| 4266 | /* Check stack size. */ |
| 4267 | if (!lua_checkstack(L, 3)) |
| 4268 | return 0; |
| 4269 | |
| 4270 | /* Create the object: obj[0] = userdata. |
| 4271 | * Note that the base of the Converters object is the |
| 4272 | * same than the TXN object. |
| 4273 | */ |
| 4274 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4275 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4276 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4277 | luactx->appctx = ctx; |
| 4278 | luactx->htxn.s = s; |
| 4279 | luactx->htxn.p = p; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4280 | |
| 4281 | /* Create the "f" field that contains a list of fetches. */ |
| 4282 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4283 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4284 | return 0; |
| 4285 | lua_settable(L, -3); |
| 4286 | |
| 4287 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4288 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4289 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4290 | return 0; |
| 4291 | lua_settable(L, -3); |
| 4292 | |
| 4293 | /* Create the "c" field that contains a list of converters. */ |
| 4294 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4295 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4296 | return 0; |
| 4297 | lua_settable(L, -3); |
| 4298 | |
| 4299 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4300 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4301 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4302 | return 0; |
| 4303 | lua_settable(L, -3); |
| 4304 | |
| 4305 | /* Pop a class stream metatable and affect it to the table. */ |
| 4306 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 4307 | lua_setmetatable(L, -2); |
| 4308 | |
| 4309 | return 1; |
| 4310 | } |
| 4311 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4312 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 4313 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4314 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4315 | struct stream *s; |
| 4316 | const char *name; |
| 4317 | size_t len; |
| 4318 | struct sample smp; |
| 4319 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4320 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4321 | 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] | 4322 | |
| 4323 | /* It is useles to retrieve the stream, but this function |
| 4324 | * runs only in a stream context. |
| 4325 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4326 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4327 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4328 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4329 | |
| 4330 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4331 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4332 | hlua_lua2smp(L, 3, &smp); |
| 4333 | |
| 4334 | /* Store the sample in a variable. */ |
| 4335 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4336 | |
| 4337 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4338 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4339 | else |
| 4340 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4341 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4342 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4343 | } |
| 4344 | |
| 4345 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 4346 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4347 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4348 | struct stream *s; |
| 4349 | const char *name; |
| 4350 | size_t len; |
| 4351 | struct sample smp; |
| 4352 | |
| 4353 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4354 | |
| 4355 | /* It is useles to retrieve the stream, but this function |
| 4356 | * runs only in a stream context. |
| 4357 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4358 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4359 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4360 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4361 | |
| 4362 | /* Unset the variable. */ |
| 4363 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4364 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4365 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4366 | } |
| 4367 | |
| 4368 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 4369 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4370 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4371 | struct stream *s; |
| 4372 | const char *name; |
| 4373 | size_t len; |
| 4374 | struct sample smp; |
| 4375 | |
| 4376 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4377 | |
| 4378 | /* It is useles to retrieve the stream, but this function |
| 4379 | * runs only in a stream context. |
| 4380 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4381 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4382 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4383 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4384 | |
| 4385 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 4386 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4387 | lua_pushnil(L); |
| 4388 | return 1; |
| 4389 | } |
| 4390 | |
| 4391 | return hlua_smp2lua(L, &smp); |
| 4392 | } |
| 4393 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4394 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 4395 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4396 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4397 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4398 | struct hlua *hlua; |
| 4399 | |
| 4400 | /* 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] | 4401 | if (!s->hlua) |
| 4402 | return 0; |
| 4403 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4404 | |
| 4405 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4406 | |
| 4407 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4408 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4409 | |
| 4410 | /* Get and store new value. */ |
| 4411 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4412 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4413 | |
| 4414 | return 0; |
| 4415 | } |
| 4416 | |
| 4417 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 4418 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4419 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4420 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4421 | struct hlua *hlua; |
| 4422 | |
| 4423 | /* 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] | 4424 | if (!s->hlua) { |
| 4425 | lua_pushnil(L); |
| 4426 | return 1; |
| 4427 | } |
| 4428 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4429 | |
| 4430 | /* Push configuration index in the stack. */ |
| 4431 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4432 | |
| 4433 | return 1; |
| 4434 | } |
| 4435 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4436 | /* If expected data not yet available, it returns a yield. This function |
| 4437 | * consumes the data in the buffer. It returns a string containing the |
| 4438 | * data. This string can be empty. |
| 4439 | */ |
| 4440 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4441 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4442 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4443 | struct conn_stream *cs = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4444 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4445 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4446 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4447 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4448 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4449 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4450 | /* Read the maximum amount of data available. */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4451 | ret = co_getline_nc(cs_oc(cs), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4452 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4453 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4454 | if (ret == 0) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 4455 | cs_cant_get(cs); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4456 | 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] | 4457 | } |
| 4458 | |
| 4459 | /* End of data: commit the total strings and return. */ |
| 4460 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4461 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4462 | return 1; |
| 4463 | } |
| 4464 | |
| 4465 | /* Ensure that the block 2 length is usable. */ |
| 4466 | if (ret == 1) |
| 4467 | len2 = 0; |
| 4468 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4469 | /* don't check the max length read and don't check. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4470 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4471 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4472 | |
| 4473 | /* Consume input channel output buffer data. */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4474 | co_skip(cs_oc(cs), len1 + len2); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4475 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4476 | return 1; |
| 4477 | } |
| 4478 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4479 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4480 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 4481 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4482 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4483 | |
| 4484 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4485 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4486 | |
| 4487 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 4488 | } |
| 4489 | |
| 4490 | /* If expected data not yet available, it returns a yield. This function |
| 4491 | * consumes the data in the buffer. It returns a string containing the |
| 4492 | * data. This string can be empty. |
| 4493 | */ |
| 4494 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4495 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4496 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4497 | struct conn_stream *cs = luactx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4498 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4499 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4500 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4501 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4502 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4503 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4504 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4505 | /* Read the maximum amount of data available. */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4506 | ret = co_getblk_nc(cs_oc(cs), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4507 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4508 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4509 | if (ret == 0) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 4510 | cs_cant_get(cs); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4511 | 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] | 4512 | } |
| 4513 | |
| 4514 | /* End of data: commit the total strings and return. */ |
| 4515 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4516 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4517 | return 1; |
| 4518 | } |
| 4519 | |
| 4520 | /* Ensure that the block 2 length is usable. */ |
| 4521 | if (ret == 1) |
| 4522 | len2 = 0; |
| 4523 | |
| 4524 | if (len == -1) { |
| 4525 | |
| 4526 | /* If len == -1, catenate all the data avalaile and |
| 4527 | * yield because we want to get all the data until |
| 4528 | * the end of data stream. |
| 4529 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4530 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4531 | luaL_addlstring(&luactx->b, blk2, len2); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4532 | co_skip(cs_oc(cs), len1 + len2); |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 4533 | cs_cant_get(cs); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4534 | 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] | 4535 | |
| 4536 | } else { |
| 4537 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4538 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4539 | if (len1 > len) |
| 4540 | len1 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4541 | luaL_addlstring(&luactx->b, blk1, len1); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4542 | len -= len1; |
| 4543 | |
| 4544 | /* Copy the second block. */ |
| 4545 | if (len2 > len) |
| 4546 | len2 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4547 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4548 | len -= len2; |
| 4549 | |
| 4550 | /* Consume input channel output buffer data. */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4551 | co_skip(cs_oc(cs), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4552 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4553 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4554 | if (len > 0) { |
| 4555 | lua_pushinteger(L, len); |
| 4556 | lua_replace(L, 2); |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 4557 | cs_cant_get(cs); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4558 | 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] | 4559 | } |
| 4560 | |
| 4561 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4562 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4563 | return 1; |
| 4564 | } |
| 4565 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4566 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4567 | hlua_pusherror(L, "Lua: internal error"); |
| 4568 | WILL_LJMP(lua_error(L)); |
| 4569 | return 0; |
| 4570 | } |
| 4571 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4572 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4573 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 4574 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4575 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4576 | int len = -1; |
| 4577 | |
| 4578 | if (lua_gettop(L) > 2) |
| 4579 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4580 | if (lua_gettop(L) >= 2) { |
| 4581 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4582 | lua_pop(L, 1); |
| 4583 | } |
| 4584 | |
| 4585 | /* Confirm or set the required length */ |
| 4586 | lua_pushinteger(L, len); |
| 4587 | |
| 4588 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4589 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4590 | |
| 4591 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4592 | } |
| 4593 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4594 | /* Append data in the output side of the buffer. This data is immediately |
| 4595 | * sent. The function returns the amount of data written. If the buffer |
| 4596 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4597 | * if the channel is closed. |
| 4598 | */ |
| 4599 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4600 | { |
| 4601 | size_t len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4602 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4603 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4604 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4605 | struct conn_stream *cs = luactx->appctx->owner; |
| 4606 | struct channel *chn = cs_ic(cs); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4607 | int max; |
| 4608 | |
| 4609 | /* Get the max amount of data which can write as input in the channel. */ |
| 4610 | max = channel_recv_max(chn); |
| 4611 | if (max > (len - l)) |
| 4612 | max = len - l; |
| 4613 | |
| 4614 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4615 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4616 | |
| 4617 | /* update counters. */ |
| 4618 | l += max; |
| 4619 | lua_pop(L, 1); |
| 4620 | lua_pushinteger(L, l); |
| 4621 | |
| 4622 | /* If some data is not send, declares the situation to the |
| 4623 | * applet, and returns a yield. |
| 4624 | */ |
| 4625 | if (l < len) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 4626 | cs_rx_room_blk(cs); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4627 | 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] | 4628 | } |
| 4629 | |
| 4630 | return 1; |
| 4631 | } |
| 4632 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4633 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4634 | * yield the LUA process, and resume it without checking the |
| 4635 | * input arguments. |
| 4636 | */ |
| 4637 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4638 | { |
| 4639 | MAY_LJMP(check_args(L, 2, "send")); |
| 4640 | lua_pushinteger(L, 0); |
| 4641 | |
| 4642 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4643 | } |
| 4644 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4645 | /* |
| 4646 | * |
| 4647 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4648 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4649 | * |
| 4650 | * |
| 4651 | */ |
| 4652 | |
| 4653 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4654 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4655 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4656 | __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] | 4657 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4658 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4659 | } |
| 4660 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4661 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4662 | * according with a current TXN. |
| 4663 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4664 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4665 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4666 | struct hlua_appctx *luactx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4667 | struct hlua_txn htxn; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4668 | struct stream *s = __cs_strm(ctx->owner); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4669 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4670 | struct htx *htx; |
| 4671 | struct htx_blk *blk; |
| 4672 | struct htx_sl *sl; |
| 4673 | struct ist path; |
| 4674 | unsigned long long len = 0; |
| 4675 | int32_t pos; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4676 | struct http_uri_parser parser; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4677 | |
| 4678 | /* Check stack size. */ |
| 4679 | if (!lua_checkstack(L, 3)) |
| 4680 | return 0; |
| 4681 | |
| 4682 | /* Create the object: obj[0] = userdata. |
| 4683 | * Note that the base of the Converters object is the |
| 4684 | * same than the TXN object. |
| 4685 | */ |
| 4686 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4687 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4688 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4689 | luactx->appctx = ctx; |
| 4690 | luactx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
| 4691 | luactx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */ |
| 4692 | luactx->htxn.s = s; |
| 4693 | luactx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4694 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4695 | /* Create the "f" field that contains a list of fetches. */ |
| 4696 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4697 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4698 | return 0; |
| 4699 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4700 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4701 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4702 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4703 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4704 | return 0; |
| 4705 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4706 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4707 | /* Create the "c" field that contains a list of converters. */ |
| 4708 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4709 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4710 | return 0; |
| 4711 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4712 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4713 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4714 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4715 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4716 | return 0; |
| 4717 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4718 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4719 | htx = htxbuf(&s->req.buf); |
| 4720 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4721 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4722 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4723 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4724 | /* Stores the request method. */ |
| 4725 | lua_pushstring(L, "method"); |
| 4726 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4727 | lua_settable(L, -3); |
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 http version. */ |
| 4730 | lua_pushstring(L, "version"); |
| 4731 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(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 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4735 | * the array on the top of the stack. |
| 4736 | */ |
| 4737 | lua_pushstring(L, "headers"); |
| 4738 | htxn.s = s; |
| 4739 | htxn.p = px; |
| 4740 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4741 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4742 | return 0; |
| 4743 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4744 | |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4745 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 4746 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4747 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4748 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4749 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4750 | p = path.ptr; |
Tim Duesterhus | 4c8f75f | 2021-11-06 15:14:44 +0100 | [diff] [blame] | 4751 | end = istend(path); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4752 | q = p; |
| 4753 | while (q < end && *q != '?') |
| 4754 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4755 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4756 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4757 | lua_pushstring(L, "path"); |
| 4758 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4759 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4760 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4761 | /* Stores the query string. */ |
| 4762 | lua_pushstring(L, "qs"); |
| 4763 | if (*q == '?') |
| 4764 | q++; |
| 4765 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4766 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4767 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4768 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4769 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4770 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4771 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4772 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4773 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4774 | break; |
| 4775 | if (type == HTX_BLK_DATA) |
| 4776 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4777 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4778 | if (htx->extra != ULLONG_MAX) |
| 4779 | len += htx->extra; |
| 4780 | |
| 4781 | /* Stores the request path. */ |
| 4782 | lua_pushstring(L, "length"); |
| 4783 | lua_pushinteger(L, len); |
| 4784 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4785 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4786 | /* Create an empty array of HTTP request headers. */ |
| 4787 | lua_pushstring(L, "response"); |
| 4788 | lua_newtable(L); |
| 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 | /* Pop a class stream metatable and affect it to the table. */ |
| 4792 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4793 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4794 | |
| 4795 | return 1; |
| 4796 | } |
| 4797 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4798 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4799 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4800 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4801 | struct stream *s; |
| 4802 | const char *name; |
| 4803 | size_t len; |
| 4804 | struct sample smp; |
| 4805 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4806 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4807 | 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] | 4808 | |
| 4809 | /* It is useles to retrieve the stream, but this function |
| 4810 | * runs only in a stream context. |
| 4811 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4812 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4813 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4814 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4815 | |
| 4816 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4817 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4818 | hlua_lua2smp(L, 3, &smp); |
| 4819 | |
| 4820 | /* Store the sample in a variable. */ |
| 4821 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4822 | |
| 4823 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4824 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4825 | else |
| 4826 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4827 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4828 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4829 | } |
| 4830 | |
| 4831 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4832 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4833 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4834 | struct stream *s; |
| 4835 | const char *name; |
| 4836 | size_t len; |
| 4837 | struct sample smp; |
| 4838 | |
| 4839 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4840 | |
| 4841 | /* It is useles to retrieve the stream, but this function |
| 4842 | * runs only in a stream context. |
| 4843 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4844 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4845 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4846 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4847 | |
| 4848 | /* Unset the variable. */ |
| 4849 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4850 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4851 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4852 | } |
| 4853 | |
| 4854 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4855 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4856 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4857 | struct stream *s; |
| 4858 | const char *name; |
| 4859 | size_t len; |
| 4860 | struct sample smp; |
| 4861 | |
| 4862 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4863 | |
| 4864 | /* It is useles to retrieve the stream, but this function |
| 4865 | * runs only in a stream context. |
| 4866 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4867 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4868 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4869 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4870 | |
| 4871 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 4872 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4873 | lua_pushnil(L); |
| 4874 | return 1; |
| 4875 | } |
| 4876 | |
| 4877 | return hlua_smp2lua(L, &smp); |
| 4878 | } |
| 4879 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4880 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4881 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4882 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4883 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4884 | struct hlua *hlua; |
| 4885 | |
| 4886 | /* 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] | 4887 | if (!s->hlua) |
| 4888 | return 0; |
| 4889 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4890 | |
| 4891 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4892 | |
| 4893 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4894 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4895 | |
| 4896 | /* Get and store new value. */ |
| 4897 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4898 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4899 | |
| 4900 | return 0; |
| 4901 | } |
| 4902 | |
| 4903 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4904 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4905 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4906 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4907 | struct hlua *hlua; |
| 4908 | |
| 4909 | /* 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] | 4910 | if (!s->hlua) { |
| 4911 | lua_pushnil(L); |
| 4912 | return 1; |
| 4913 | } |
| 4914 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4915 | |
| 4916 | /* Push configuration index in the stack. */ |
| 4917 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4918 | |
| 4919 | return 1; |
| 4920 | } |
| 4921 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4922 | /* If expected data not yet available, it returns a yield. This function |
| 4923 | * consumes the data in the buffer. It returns a string containing the |
| 4924 | * data. This string can be empty. |
| 4925 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4926 | __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] | 4927 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4928 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 4929 | struct conn_stream *cs = luactx->appctx->owner; |
| 4930 | struct channel *req = cs_oc(cs); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4931 | struct htx *htx; |
| 4932 | struct htx_blk *blk; |
| 4933 | size_t count; |
| 4934 | int stop = 0; |
| 4935 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4936 | htx = htx_from_buf(&req->buf); |
| 4937 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4938 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4939 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4940 | while (count && !stop && blk) { |
| 4941 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4942 | uint32_t sz = htx_get_blksz(blk); |
| 4943 | struct ist v; |
| 4944 | uint32_t vlen; |
| 4945 | char *nl; |
| 4946 | |
| 4947 | vlen = sz; |
| 4948 | if (vlen > count) { |
| 4949 | if (type != HTX_BLK_DATA) |
| 4950 | break; |
| 4951 | vlen = count; |
| 4952 | } |
| 4953 | |
| 4954 | switch (type) { |
| 4955 | case HTX_BLK_UNUSED: |
| 4956 | break; |
| 4957 | |
| 4958 | case HTX_BLK_DATA: |
| 4959 | v = htx_get_blk_value(htx, blk); |
| 4960 | v.len = vlen; |
| 4961 | nl = istchr(v, '\n'); |
| 4962 | if (nl != NULL) { |
| 4963 | stop = 1; |
| 4964 | vlen = nl - v.ptr + 1; |
| 4965 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4966 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4967 | break; |
| 4968 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4969 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4970 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4971 | stop = 1; |
| 4972 | break; |
| 4973 | |
| 4974 | default: |
| 4975 | break; |
| 4976 | } |
| 4977 | |
Willy Tarreau | 8424004 | 2022-02-28 16:51:23 +0100 | [diff] [blame] | 4978 | c_rew(req, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4979 | count -= vlen; |
| 4980 | if (sz == vlen) |
| 4981 | blk = htx_remove_blk(htx, blk); |
| 4982 | else { |
| 4983 | htx_cut_data_blk(htx, blk, vlen); |
| 4984 | break; |
| 4985 | } |
| 4986 | } |
| 4987 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4988 | /* The message was fully consumed and no more data are expected |
| 4989 | * (EOM flag set). |
| 4990 | */ |
| 4991 | if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) |
| 4992 | stop = 1; |
| 4993 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4994 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4995 | if (!stop) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 4996 | cs_cant_get(cs); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4997 | 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] | 4998 | } |
| 4999 | |
| 5000 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5001 | luaL_pushresult(&luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5002 | return 1; |
| 5003 | } |
| 5004 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5005 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5006 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5007 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5008 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5009 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5010 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5011 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5012 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5013 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5014 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5015 | } |
| 5016 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5017 | /* If expected data not yet available, it returns a yield. This function |
| 5018 | * consumes the data in the buffer. It returns a string containing the |
| 5019 | * data. This string can be empty. |
| 5020 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5021 | __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] | 5022 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5023 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 5024 | struct conn_stream *cs = luactx->appctx->owner; |
| 5025 | struct channel *req = cs_oc(cs); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5026 | struct htx *htx; |
| 5027 | struct htx_blk *blk; |
| 5028 | size_t count; |
| 5029 | int len; |
| 5030 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5031 | htx = htx_from_buf(&req->buf); |
| 5032 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5033 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 5034 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5035 | while (count && len && blk) { |
| 5036 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5037 | uint32_t sz = htx_get_blksz(blk); |
| 5038 | struct ist v; |
| 5039 | uint32_t vlen; |
| 5040 | |
| 5041 | vlen = sz; |
| 5042 | if (len > 0 && vlen > len) |
| 5043 | vlen = len; |
| 5044 | if (vlen > count) { |
| 5045 | if (type != HTX_BLK_DATA) |
| 5046 | break; |
| 5047 | vlen = count; |
| 5048 | } |
| 5049 | |
| 5050 | switch (type) { |
| 5051 | case HTX_BLK_UNUSED: |
| 5052 | break; |
| 5053 | |
| 5054 | case HTX_BLK_DATA: |
| 5055 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5056 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5057 | break; |
| 5058 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5059 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5060 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5061 | len = 0; |
| 5062 | break; |
| 5063 | |
| 5064 | default: |
| 5065 | break; |
| 5066 | } |
| 5067 | |
Willy Tarreau | 8424004 | 2022-02-28 16:51:23 +0100 | [diff] [blame] | 5068 | c_rew(req, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5069 | count -= vlen; |
| 5070 | if (len > 0) |
| 5071 | len -= vlen; |
| 5072 | if (sz == vlen) |
| 5073 | blk = htx_remove_blk(htx, blk); |
| 5074 | else { |
| 5075 | htx_cut_data_blk(htx, blk, vlen); |
| 5076 | break; |
| 5077 | } |
| 5078 | } |
| 5079 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5080 | /* The message was fully consumed and no more data are expected |
| 5081 | * (EOM flag set). |
| 5082 | */ |
| 5083 | if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) |
| 5084 | len = 0; |
| 5085 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5086 | htx_to_buf(htx, &req->buf); |
| 5087 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5088 | /* If we are no other data available, yield waiting for new data. */ |
| 5089 | if (len) { |
| 5090 | if (len > 0) { |
| 5091 | lua_pushinteger(L, len); |
| 5092 | lua_replace(L, 2); |
| 5093 | } |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 5094 | cs_cant_get(cs); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5095 | 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] | 5096 | } |
| 5097 | |
| 5098 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5099 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5100 | return 1; |
| 5101 | } |
| 5102 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5103 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5104 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 5105 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5106 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5107 | int len = -1; |
| 5108 | |
| 5109 | /* Check arguments. */ |
| 5110 | if (lua_gettop(L) > 2) |
| 5111 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 5112 | if (lua_gettop(L) >= 2) { |
| 5113 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5114 | lua_pop(L, 1); |
| 5115 | } |
| 5116 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5117 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5118 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5119 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5120 | luaL_buffinit(L, &luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5121 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5122 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5123 | } |
| 5124 | |
| 5125 | /* Append data in the output side of the buffer. This data is immediately |
| 5126 | * sent. The function returns the amount of data written. If the buffer |
| 5127 | * cannot contain the data, the function yields. The function returns -1 |
| 5128 | * if the channel is closed. |
| 5129 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5130 | __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] | 5131 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5132 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 5133 | struct conn_stream *cs = luactx->appctx->owner; |
| 5134 | struct channel *res = cs_ic(cs); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5135 | struct htx *htx = htx_from_buf(&res->buf); |
| 5136 | const char *data; |
| 5137 | size_t len; |
| 5138 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 5139 | int max; |
| 5140 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 5141 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5142 | if (!max) |
| 5143 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5144 | |
| 5145 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5146 | |
| 5147 | /* Get the max amount of data which can write as input in the channel. */ |
| 5148 | if (max > (len - l)) |
| 5149 | max = len - l; |
| 5150 | |
| 5151 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 5152 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5153 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5154 | |
| 5155 | /* update counters. */ |
| 5156 | l += max; |
| 5157 | lua_pop(L, 1); |
| 5158 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5159 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5160 | /* If some data is not send, declares the situation to the |
| 5161 | * applet, and returns a yield. |
| 5162 | */ |
| 5163 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5164 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5165 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 5166 | cs_rx_room_blk(cs); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5167 | 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] | 5168 | } |
| 5169 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5170 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5171 | return 1; |
| 5172 | } |
| 5173 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5174 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5175 | * yield the LUA process, and resume it without checking the |
| 5176 | * input arguments. |
| 5177 | */ |
| 5178 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 5179 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5180 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5181 | |
| 5182 | /* We want to send some data. Headers must be sent. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5183 | if (!(luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5184 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 5185 | WILL_LJMP(lua_error(L)); |
| 5186 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5187 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5188 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5189 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5190 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5191 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5192 | } |
| 5193 | |
| 5194 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 5195 | { |
| 5196 | const char *name; |
| 5197 | int ret; |
| 5198 | |
| 5199 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5200 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5201 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 5202 | |
| 5203 | /* Push in the stack the "response" entry. */ |
| 5204 | ret = lua_getfield(L, 1, "response"); |
| 5205 | if (ret != LUA_TTABLE) { |
| 5206 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 5207 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 5208 | WILL_LJMP(lua_error(L)); |
| 5209 | } |
| 5210 | |
| 5211 | /* check if the header is already registered if it is not |
| 5212 | * the case, register it. |
| 5213 | */ |
| 5214 | ret = lua_getfield(L, -1, name); |
| 5215 | if (ret == LUA_TNIL) { |
| 5216 | |
| 5217 | /* Entry not found. */ |
| 5218 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 5219 | |
| 5220 | /* Insert the new header name in the array in the top of the stack. |
| 5221 | * It left the new array in the top of the stack. |
| 5222 | */ |
| 5223 | lua_newtable(L); |
| 5224 | lua_pushvalue(L, 2); |
| 5225 | lua_pushvalue(L, -2); |
| 5226 | lua_settable(L, -4); |
| 5227 | |
| 5228 | } else if (ret != LUA_TTABLE) { |
| 5229 | |
| 5230 | /* corruption error. */ |
| 5231 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 5232 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 5233 | WILL_LJMP(lua_error(L)); |
| 5234 | } |
| 5235 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5236 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5237 | * the header value as new entry. |
| 5238 | */ |
| 5239 | lua_pushvalue(L, 3); |
| 5240 | ret = lua_rawlen(L, -2); |
| 5241 | lua_rawseti(L, -2, ret + 1); |
| 5242 | lua_pushboolean(L, 1); |
| 5243 | return 1; |
| 5244 | } |
| 5245 | |
| 5246 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 5247 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5248 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5249 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5250 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5251 | |
| 5252 | if (status < 100 || status > 599) { |
| 5253 | lua_pushboolean(L, 0); |
| 5254 | return 1; |
| 5255 | } |
| 5256 | |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5257 | luactx->appctx->ctx.hlua_apphttp.status = status; |
| 5258 | luactx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5259 | lua_pushboolean(L, 1); |
| 5260 | return 1; |
| 5261 | } |
| 5262 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5263 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5264 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5265 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5266 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 5267 | struct conn_stream *cs = luactx->appctx->owner; |
| 5268 | struct channel *res = cs_ic(cs); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5269 | struct htx *htx; |
| 5270 | struct htx_sl *sl; |
| 5271 | struct h1m h1m; |
| 5272 | const char *status, *reason; |
| 5273 | const char *name, *value; |
| 5274 | size_t nlen, vlen; |
| 5275 | unsigned int flags; |
| 5276 | |
| 5277 | /* Send the message at once. */ |
| 5278 | htx = htx_from_buf(&res->buf); |
| 5279 | h1m_init_res(&h1m); |
| 5280 | |
| 5281 | /* Use the same http version than the request. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5282 | status = ultoa_r(luactx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 5283 | reason = luactx->appctx->ctx.hlua_apphttp.reason; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5284 | if (reason == NULL) |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5285 | reason = http_get_reason(luactx->appctx->ctx.hlua_apphttp.status); |
| 5286 | if (luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5287 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5288 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 5289 | } |
| 5290 | else { |
| 5291 | flags = HTX_SL_F_IS_RESP; |
| 5292 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 5293 | } |
| 5294 | if (!sl) { |
| 5295 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5296 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5297 | WILL_LJMP(lua_error(L)); |
| 5298 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5299 | sl->info.res.status = luactx->appctx->ctx.hlua_apphttp.status; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5300 | |
| 5301 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 5302 | lua_pushvalue(L, 0); |
| 5303 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 5304 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5305 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5306 | WILL_LJMP(lua_error(L)); |
| 5307 | } |
| 5308 | |
| 5309 | /* Browse the list of headers. */ |
| 5310 | lua_pushnil(L); |
| 5311 | while(lua_next(L, -2) != 0) { |
| 5312 | /* We expect a string as -2. */ |
| 5313 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 5314 | 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] | 5315 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5316 | lua_typename(L, lua_type(L, -2))); |
| 5317 | WILL_LJMP(lua_error(L)); |
| 5318 | } |
| 5319 | name = lua_tolstring(L, -2, &nlen); |
| 5320 | |
| 5321 | /* We expect an array as -1. */ |
| 5322 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 5323 | 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] | 5324 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5325 | name, |
| 5326 | lua_typename(L, lua_type(L, -1))); |
| 5327 | WILL_LJMP(lua_error(L)); |
| 5328 | } |
| 5329 | |
| 5330 | /* Browse the table who is on the top of the stack. */ |
| 5331 | lua_pushnil(L); |
| 5332 | while(lua_next(L, -2) != 0) { |
| 5333 | int id; |
| 5334 | |
| 5335 | /* We expect a number as -2. */ |
| 5336 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 5337 | 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] | 5338 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5339 | name, |
| 5340 | lua_typename(L, lua_type(L, -2))); |
| 5341 | WILL_LJMP(lua_error(L)); |
| 5342 | } |
| 5343 | id = lua_tointeger(L, -2); |
| 5344 | |
| 5345 | /* We expect a string as -2. */ |
| 5346 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 5347 | 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] | 5348 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5349 | name, id, |
| 5350 | lua_typename(L, lua_type(L, -1))); |
| 5351 | WILL_LJMP(lua_error(L)); |
| 5352 | } |
| 5353 | value = lua_tolstring(L, -1, &vlen); |
| 5354 | |
| 5355 | /* Simple Protocol checks. */ |
Christopher Faulet | 545fbba | 2021-09-28 09:36:25 +0200 | [diff] [blame] | 5356 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) { |
| 5357 | int ret; |
| 5358 | |
| 5359 | ret = h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
| 5360 | if (ret < 0) { |
| 5361 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 5362 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
| 5363 | name); |
| 5364 | WILL_LJMP(lua_error(L)); |
| 5365 | } |
| 5366 | else if (ret == 0) |
| 5367 | goto next; /* Skip it */ |
| 5368 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5369 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 5370 | struct ist v = ist2(value, vlen); |
| 5371 | int ret; |
| 5372 | |
| 5373 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 5374 | if (ret < 0) { |
| 5375 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5376 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5377 | name); |
| 5378 | WILL_LJMP(lua_error(L)); |
| 5379 | } |
| 5380 | else if (ret == 0) |
| 5381 | goto next; /* Skip it */ |
| 5382 | } |
| 5383 | |
| 5384 | /* Add a new header */ |
| 5385 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 5386 | 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] | 5387 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5388 | name); |
| 5389 | WILL_LJMP(lua_error(L)); |
| 5390 | } |
| 5391 | next: |
| 5392 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5393 | lua_pop(L, 1); |
| 5394 | } |
| 5395 | |
| 5396 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5397 | lua_pop(L, 1); |
| 5398 | } |
| 5399 | |
| 5400 | if (h1m.flags & H1_MF_CHNK) |
| 5401 | h1m.flags &= ~H1_MF_CLEN; |
| 5402 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5403 | h1m.flags |= H1_MF_XFER_LEN; |
| 5404 | |
| 5405 | /* Uset HTX start-line flags */ |
| 5406 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5407 | flags |= HTX_SL_F_XFER_ENC; |
| 5408 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5409 | flags |= HTX_SL_F_XFER_LEN; |
| 5410 | if (h1m.flags & H1_MF_CHNK) |
| 5411 | flags |= HTX_SL_F_CHNK; |
| 5412 | else if (h1m.flags & H1_MF_CLEN) |
| 5413 | flags |= HTX_SL_F_CLEN; |
| 5414 | if (h1m.body_len == 0) |
| 5415 | flags |= HTX_SL_F_BODYLESS; |
| 5416 | } |
| 5417 | sl->flags |= flags; |
| 5418 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5419 | /* 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] | 5420 | * and the status code implies the presence of a message body, we must |
| 5421 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5422 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 5423 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5424 | */ |
| 5425 | 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] | 5426 | luactx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 5427 | luactx->appctx->ctx.hlua_apphttp.status != 204 && |
| 5428 | luactx->appctx->ctx.hlua_apphttp.status != 304) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5429 | /* Add a new header */ |
| 5430 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 5431 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 5432 | 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] | 5433 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5434 | WILL_LJMP(lua_error(L)); |
| 5435 | } |
| 5436 | } |
| 5437 | |
| 5438 | /* Finalize headers. */ |
| 5439 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 5440 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5441 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5442 | WILL_LJMP(lua_error(L)); |
| 5443 | } |
| 5444 | |
| 5445 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 5446 | b_reset(&res->buf); |
| 5447 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 5448 | WILL_LJMP(lua_error(L)); |
| 5449 | } |
| 5450 | |
| 5451 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5452 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5453 | |
| 5454 | /* Headers sent, set the flag. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5455 | luactx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5456 | return 0; |
| 5457 | |
| 5458 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5459 | /* We will build the status line and the headers of the HTTP response. |
| 5460 | * We will try send at once if its not possible, we give back the hand |
| 5461 | * waiting for more room. |
| 5462 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5463 | __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] | 5464 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5465 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 5466 | struct conn_stream *cs = luactx->appctx->owner; |
| 5467 | struct channel *res = cs_ic(cs); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5468 | |
| 5469 | if (co_data(res)) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 5470 | cs_rx_room_blk(cs); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5471 | 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] | 5472 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5473 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5474 | } |
| 5475 | |
| 5476 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5477 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5478 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5479 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5480 | } |
| 5481 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5482 | /* |
| 5483 | * |
| 5484 | * |
| 5485 | * Class HTTP |
| 5486 | * |
| 5487 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5488 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5489 | |
| 5490 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5491 | * a class stream, otherwise it throws an error. |
| 5492 | */ |
| 5493 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5494 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5495 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 5496 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5497 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5498 | /* This function creates and push in the stack a HTTP object |
| 5499 | * according with a current TXN. |
| 5500 | */ |
| 5501 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5502 | { |
| 5503 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5504 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5505 | /* Check stack size. */ |
| 5506 | if (!lua_checkstack(L, 3)) |
| 5507 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5508 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5509 | /* Create the object: obj[0] = userdata. |
| 5510 | * Note that the base of the Converters object is the |
| 5511 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5512 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5513 | lua_newtable(L); |
| 5514 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5515 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5516 | |
| 5517 | htxn->s = txn->s; |
| 5518 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 5519 | htxn->dir = txn->dir; |
| 5520 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5521 | |
| 5522 | /* Pop a class stream metatable and affect it to the table. */ |
| 5523 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5524 | lua_setmetatable(L, -2); |
| 5525 | |
| 5526 | return 1; |
| 5527 | } |
| 5528 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5529 | /* This function creates and returns an array containing the status-line |
| 5530 | * elements. This function does not fails. |
| 5531 | */ |
| 5532 | __LJMP static int hlua_http_get_stline(lua_State *L, struct htx_sl *sl) |
| 5533 | { |
| 5534 | /* Create the table. */ |
| 5535 | lua_newtable(L); |
| 5536 | |
| 5537 | if (sl->flags & HTX_SL_F_IS_RESP) { |
| 5538 | lua_pushstring(L, "version"); |
| 5539 | lua_pushlstring(L, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); |
| 5540 | lua_settable(L, -3); |
| 5541 | lua_pushstring(L, "code"); |
| 5542 | lua_pushlstring(L, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); |
| 5543 | lua_settable(L, -3); |
| 5544 | lua_pushstring(L, "reason"); |
| 5545 | lua_pushlstring(L, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); |
| 5546 | lua_settable(L, -3); |
| 5547 | } |
| 5548 | else { |
| 5549 | lua_pushstring(L, "method"); |
| 5550 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 5551 | lua_settable(L, -3); |
| 5552 | lua_pushstring(L, "uri"); |
| 5553 | lua_pushlstring(L, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); |
| 5554 | lua_settable(L, -3); |
| 5555 | lua_pushstring(L, "version"); |
| 5556 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 5557 | lua_settable(L, -3); |
| 5558 | } |
| 5559 | return 1; |
| 5560 | } |
| 5561 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5562 | /* This function creates ans returns an array of HTTP headers. |
| 5563 | * This function does not fails. It is used as wrapper with the |
| 5564 | * 2 following functions. |
| 5565 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5566 | __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] | 5567 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5568 | struct htx *htx; |
| 5569 | int32_t pos; |
| 5570 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5571 | /* Create the table. */ |
| 5572 | lua_newtable(L); |
| 5573 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5574 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5575 | htx = htxbuf(&msg->chn->buf); |
| 5576 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5577 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5578 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5579 | struct ist n, v; |
| 5580 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5581 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5582 | if (type == HTX_BLK_HDR) { |
| 5583 | n = htx_get_blk_name(htx,blk); |
| 5584 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5585 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5586 | else if (type == HTX_BLK_EOH) |
| 5587 | break; |
| 5588 | else |
| 5589 | continue; |
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 | /* Check for existing entry: |
| 5592 | * assume that the table is on the top of the stack, and |
| 5593 | * push the key in the stack, the function lua_gettable() |
| 5594 | * perform the lookup. |
| 5595 | */ |
| 5596 | lua_pushlstring(L, n.ptr, n.len); |
| 5597 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5598 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5599 | switch (lua_type(L, -1)) { |
| 5600 | case LUA_TNIL: |
| 5601 | /* Table not found, create it. */ |
| 5602 | lua_pop(L, 1); /* remove the nil value. */ |
| 5603 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5604 | lua_newtable(L); /* create and push empty table. */ |
| 5605 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5606 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5607 | 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] | 5608 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5609 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5610 | case LUA_TTABLE: |
| 5611 | /* Entry found: push the value in the table. */ |
| 5612 | len = lua_rawlen(L, -1); |
| 5613 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5614 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5615 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5616 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5617 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5618 | default: |
| 5619 | /* Other cases are errors. */ |
| 5620 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5621 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5622 | } |
| 5623 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5624 | return 1; |
| 5625 | } |
| 5626 | |
| 5627 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5628 | { |
| 5629 | struct hlua_txn *htxn; |
| 5630 | |
| 5631 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5632 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5633 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5634 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5635 | WILL_LJMP(lua_error(L)); |
| 5636 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5637 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5638 | } |
| 5639 | |
| 5640 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5641 | { |
| 5642 | struct hlua_txn *htxn; |
| 5643 | |
| 5644 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5645 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5646 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5647 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5648 | WILL_LJMP(lua_error(L)); |
| 5649 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5650 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5651 | } |
| 5652 | |
| 5653 | /* This function replace full header, or just a value in |
| 5654 | * the request or in the response. It is a wrapper fir the |
| 5655 | * 4 following functions. |
| 5656 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5657 | __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] | 5658 | { |
| 5659 | size_t name_len; |
| 5660 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5661 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5662 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5663 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5664 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5665 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5666 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5667 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5668 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5669 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5670 | 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] | 5671 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5672 | return 0; |
| 5673 | } |
| 5674 | |
| 5675 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5676 | { |
| 5677 | struct hlua_txn *htxn; |
| 5678 | |
| 5679 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5680 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5681 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5682 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5683 | WILL_LJMP(lua_error(L)); |
| 5684 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5685 | 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] | 5686 | } |
| 5687 | |
| 5688 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5689 | { |
| 5690 | struct hlua_txn *htxn; |
| 5691 | |
| 5692 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5693 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5694 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5695 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5696 | WILL_LJMP(lua_error(L)); |
| 5697 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5698 | 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] | 5699 | } |
| 5700 | |
| 5701 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5702 | { |
| 5703 | struct hlua_txn *htxn; |
| 5704 | |
| 5705 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5706 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5707 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5708 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5709 | WILL_LJMP(lua_error(L)); |
| 5710 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5711 | 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] | 5712 | } |
| 5713 | |
| 5714 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5715 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5716 | struct hlua_txn *htxn; |
| 5717 | |
| 5718 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5719 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5720 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5721 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5722 | WILL_LJMP(lua_error(L)); |
| 5723 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5724 | 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] | 5725 | } |
| 5726 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5727 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5728 | * It is a wrapper for the 2 following functions. |
| 5729 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5730 | __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] | 5731 | { |
| 5732 | size_t len; |
| 5733 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5734 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5735 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5736 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5737 | ctx.blk = NULL; |
| 5738 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5739 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5740 | return 0; |
| 5741 | } |
| 5742 | |
| 5743 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5744 | { |
| 5745 | struct hlua_txn *htxn; |
| 5746 | |
| 5747 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5748 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5749 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5750 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5751 | WILL_LJMP(lua_error(L)); |
| 5752 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5753 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5754 | } |
| 5755 | |
| 5756 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5757 | { |
| 5758 | struct hlua_txn *htxn; |
| 5759 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5760 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5761 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5762 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5763 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5764 | WILL_LJMP(lua_error(L)); |
| 5765 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5766 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5767 | } |
| 5768 | |
| 5769 | /* This function adds an header. It is a wrapper used by |
| 5770 | * the 2 following functions. |
| 5771 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5772 | __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] | 5773 | { |
| 5774 | size_t name_len; |
| 5775 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5776 | size_t value_len; |
| 5777 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5778 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5779 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5780 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5781 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5782 | return 0; |
| 5783 | } |
| 5784 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5785 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5786 | { |
| 5787 | struct hlua_txn *htxn; |
| 5788 | |
| 5789 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5790 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5791 | |
| 5792 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5793 | WILL_LJMP(lua_error(L)); |
| 5794 | |
| 5795 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 5796 | } |
| 5797 | |
| 5798 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5799 | { |
| 5800 | struct hlua_txn *htxn; |
| 5801 | |
| 5802 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5803 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5804 | |
| 5805 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5806 | WILL_LJMP(lua_error(L)); |
| 5807 | |
| 5808 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 5809 | } |
| 5810 | |
| 5811 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5812 | { |
| 5813 | struct hlua_txn *htxn; |
| 5814 | |
| 5815 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5816 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5817 | |
| 5818 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5819 | WILL_LJMP(lua_error(L)); |
| 5820 | |
| 5821 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5822 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 5823 | } |
| 5824 | |
| 5825 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5826 | { |
| 5827 | struct hlua_txn *htxn; |
| 5828 | |
| 5829 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5830 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5831 | |
| 5832 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5833 | WILL_LJMP(lua_error(L)); |
| 5834 | |
| 5835 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5836 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 5837 | } |
| 5838 | |
| 5839 | /* This function set the method. */ |
| 5840 | static int hlua_http_req_set_meth(lua_State *L) |
| 5841 | { |
| 5842 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5843 | size_t name_len; |
| 5844 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5845 | |
| 5846 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5847 | WILL_LJMP(lua_error(L)); |
| 5848 | |
| 5849 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1); |
| 5850 | return 1; |
| 5851 | } |
| 5852 | |
| 5853 | /* This function set the method. */ |
| 5854 | static int hlua_http_req_set_path(lua_State *L) |
| 5855 | { |
| 5856 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5857 | size_t name_len; |
| 5858 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5859 | |
| 5860 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5861 | WILL_LJMP(lua_error(L)); |
| 5862 | |
| 5863 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1); |
| 5864 | return 1; |
| 5865 | } |
| 5866 | |
| 5867 | /* This function set the query-string. */ |
| 5868 | static int hlua_http_req_set_query(lua_State *L) |
| 5869 | { |
| 5870 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5871 | size_t name_len; |
| 5872 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5873 | |
| 5874 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5875 | WILL_LJMP(lua_error(L)); |
| 5876 | |
| 5877 | /* Check length. */ |
| 5878 | if (name_len > trash.size - 1) { |
| 5879 | lua_pushboolean(L, 0); |
| 5880 | return 1; |
| 5881 | } |
| 5882 | |
| 5883 | /* Add the mark question as prefix. */ |
| 5884 | chunk_reset(&trash); |
| 5885 | trash.area[trash.data++] = '?'; |
| 5886 | memcpy(trash.area + trash.data, name, name_len); |
| 5887 | trash.data += name_len; |
| 5888 | |
| 5889 | lua_pushboolean(L, |
| 5890 | http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1); |
| 5891 | return 1; |
| 5892 | } |
| 5893 | |
| 5894 | /* This function set the uri. */ |
| 5895 | static int hlua_http_req_set_uri(lua_State *L) |
| 5896 | { |
| 5897 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5898 | size_t name_len; |
| 5899 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5900 | |
| 5901 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5902 | WILL_LJMP(lua_error(L)); |
| 5903 | |
| 5904 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1); |
| 5905 | return 1; |
| 5906 | } |
| 5907 | |
| 5908 | /* This function set the response code & optionally reason. */ |
| 5909 | static int hlua_http_res_set_status(lua_State *L) |
| 5910 | { |
| 5911 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5912 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5913 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5914 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
| 5915 | |
| 5916 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5917 | WILL_LJMP(lua_error(L)); |
| 5918 | |
| 5919 | http_res_set_status(code, reason, htxn->s); |
| 5920 | return 0; |
| 5921 | } |
| 5922 | |
| 5923 | /* |
| 5924 | * |
| 5925 | * |
| 5926 | * Class HTTPMessage |
| 5927 | * |
| 5928 | * |
| 5929 | */ |
| 5930 | |
| 5931 | /* Returns a struct http_msg if the stack entry "ud" is a class HTTPMessage, |
| 5932 | * otherwise it throws an error. |
| 5933 | */ |
| 5934 | __LJMP static struct http_msg *hlua_checkhttpmsg(lua_State *L, int ud) |
| 5935 | { |
| 5936 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_msg_ref)); |
| 5937 | } |
| 5938 | |
| 5939 | /* Creates and pushes on the stack a HTTP object according with a current TXN. |
| 5940 | */ |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 5941 | 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] | 5942 | { |
| 5943 | /* Check stack size. */ |
| 5944 | if (!lua_checkstack(L, 3)) |
| 5945 | return 0; |
| 5946 | |
| 5947 | lua_newtable(L); |
| 5948 | lua_pushlightuserdata(L, msg); |
| 5949 | lua_rawseti(L, -2, 0); |
| 5950 | |
| 5951 | /* Create the "channel" field that contains the request channel object. */ |
| 5952 | lua_pushstring(L, "channel"); |
| 5953 | if (!hlua_channel_new(L, msg->chn)) |
| 5954 | return 0; |
| 5955 | lua_rawset(L, -3); |
| 5956 | |
| 5957 | /* Pop a class stream metatable and affect it to the table. */ |
| 5958 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_msg_ref); |
| 5959 | lua_setmetatable(L, -2); |
| 5960 | |
| 5961 | return 1; |
| 5962 | } |
| 5963 | |
| 5964 | /* Helper function returning a filter attached to the HTTP message at the |
| 5965 | * 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] | 5966 | * 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] | 5967 | * filled with output and input length respectively. |
| 5968 | */ |
| 5969 | static struct filter *hlua_http_msg_filter(lua_State *L, int ud, struct http_msg *msg, size_t *offset, size_t *len) |
| 5970 | { |
| 5971 | struct channel *chn = msg->chn; |
| 5972 | struct htx *htx = htxbuf(&chn->buf); |
| 5973 | struct filter *filter = NULL; |
| 5974 | |
| 5975 | *offset = co_data(msg->chn); |
| 5976 | *len = htx->data - co_data(msg->chn); |
| 5977 | |
| 5978 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 5979 | filter = lua_touserdata (L, -1); |
| 5980 | if (msg->msg_state >= HTTP_MSG_DATA) { |
| 5981 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 5982 | |
| 5983 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 5984 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 5985 | } |
| 5986 | } |
| 5987 | |
| 5988 | lua_pop(L, 1); |
| 5989 | return filter; |
| 5990 | } |
| 5991 | |
| 5992 | /* Returns true if the channel attached to the HTTP message is the response |
| 5993 | * channel. |
| 5994 | */ |
| 5995 | __LJMP static int hlua_http_msg_is_resp(lua_State *L) |
| 5996 | { |
| 5997 | struct http_msg *msg; |
| 5998 | |
| 5999 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 6000 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6001 | |
| 6002 | lua_pushboolean(L, !!(msg->chn->flags & CF_ISRESP)); |
| 6003 | return 1; |
| 6004 | } |
| 6005 | |
| 6006 | /* Returns an array containing the elements status-line of the HTTP message. It relies |
| 6007 | * on hlua_http_get_stline(). |
| 6008 | */ |
| 6009 | __LJMP static int hlua_http_msg_get_stline(lua_State *L) |
| 6010 | { |
| 6011 | struct http_msg *msg; |
| 6012 | struct htx *htx; |
| 6013 | struct htx_sl *sl; |
| 6014 | |
| 6015 | MAY_LJMP(check_args(L, 1, "get_stline")); |
| 6016 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6017 | |
| 6018 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6019 | WILL_LJMP(lua_error(L)); |
| 6020 | |
| 6021 | htx = htxbuf(&msg->chn->buf); |
| 6022 | sl = http_get_stline(htx); |
| 6023 | if (!sl) |
| 6024 | return 0; |
| 6025 | return hlua_http_get_stline(L, sl); |
| 6026 | } |
| 6027 | |
| 6028 | /* Returns an array containing all headers of the HTTP message. it relies on |
| 6029 | * hlua_http_get_headers(). |
| 6030 | */ |
| 6031 | __LJMP static int hlua_http_msg_get_headers(lua_State *L) |
| 6032 | { |
| 6033 | struct http_msg *msg; |
| 6034 | |
| 6035 | MAY_LJMP(check_args(L, 1, "get_headers")); |
| 6036 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6037 | |
| 6038 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6039 | WILL_LJMP(lua_error(L)); |
| 6040 | |
| 6041 | return hlua_http_get_headers(L, msg); |
| 6042 | } |
| 6043 | |
| 6044 | /* Deletes all occurrences of an header in the HTTP message matching on its |
| 6045 | * name. It relies on hlua_http_del_hdr(). |
| 6046 | */ |
| 6047 | __LJMP static int hlua_http_msg_del_hdr(lua_State *L) |
| 6048 | { |
| 6049 | struct http_msg *msg; |
| 6050 | |
| 6051 | MAY_LJMP(check_args(L, 2, "del_header")); |
| 6052 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6053 | |
| 6054 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6055 | WILL_LJMP(lua_error(L)); |
| 6056 | |
| 6057 | return hlua_http_del_hdr(L, msg); |
| 6058 | } |
| 6059 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6060 | /* 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] | 6061 | * message given its name against a regex and replaces it if it matches. It |
| 6062 | * relies on hlua_http_rep_hdr(). |
| 6063 | */ |
| 6064 | __LJMP static int hlua_http_msg_rep_hdr(lua_State *L) |
| 6065 | { |
| 6066 | struct http_msg *msg; |
| 6067 | |
| 6068 | MAY_LJMP(check_args(L, 4, "rep_header")); |
| 6069 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6070 | |
| 6071 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6072 | WILL_LJMP(lua_error(L)); |
| 6073 | |
| 6074 | return hlua_http_rep_hdr(L, msg, 1); |
| 6075 | } |
| 6076 | |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 6077 | /* 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] | 6078 | * message given its name against a regex and replaces it if it matches. It |
| 6079 | * relies on hlua_http_rep_hdr(). |
| 6080 | */ |
| 6081 | __LJMP static int hlua_http_msg_rep_val(lua_State *L) |
| 6082 | { |
| 6083 | struct http_msg *msg; |
| 6084 | |
| 6085 | MAY_LJMP(check_args(L, 4, "rep_value")); |
| 6086 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6087 | |
| 6088 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6089 | WILL_LJMP(lua_error(L)); |
| 6090 | |
| 6091 | return hlua_http_rep_hdr(L, msg, 0); |
| 6092 | } |
| 6093 | |
| 6094 | /* Add an header in the HTTP message. It relies on hlua_http_add_hdr() */ |
| 6095 | __LJMP static int hlua_http_msg_add_hdr(lua_State *L) |
| 6096 | { |
| 6097 | struct http_msg *msg; |
| 6098 | |
| 6099 | MAY_LJMP(check_args(L, 3, "add_header")); |
| 6100 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6101 | |
| 6102 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6103 | WILL_LJMP(lua_error(L)); |
| 6104 | |
| 6105 | return hlua_http_add_hdr(L, msg); |
| 6106 | } |
| 6107 | |
| 6108 | /* Add an header in the HTTP message removing existing headers with the same |
| 6109 | * name. It relies on hlua_http_del_hdr() and hlua_http_add_hdr(). |
| 6110 | */ |
| 6111 | __LJMP static int hlua_http_msg_set_hdr(lua_State *L) |
| 6112 | { |
| 6113 | struct http_msg *msg; |
| 6114 | |
| 6115 | MAY_LJMP(check_args(L, 3, "set_header")); |
| 6116 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6117 | |
| 6118 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6119 | WILL_LJMP(lua_error(L)); |
| 6120 | |
| 6121 | hlua_http_del_hdr(L, msg); |
| 6122 | return hlua_http_add_hdr(L, msg); |
| 6123 | } |
| 6124 | |
| 6125 | /* Rewrites the request method. It relies on http_req_replace_stline(). */ |
| 6126 | __LJMP static int hlua_http_msg_set_meth(lua_State *L) |
| 6127 | { |
| 6128 | struct stream *s; |
| 6129 | struct http_msg *msg; |
| 6130 | const char *name; |
| 6131 | size_t name_len; |
| 6132 | |
| 6133 | MAY_LJMP(check_args(L, 2, "set_method")); |
| 6134 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6135 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6136 | |
| 6137 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6138 | WILL_LJMP(lua_error(L)); |
| 6139 | |
| 6140 | s = chn_strm(msg->chn); |
| 6141 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, s->be, s) != -1); |
| 6142 | return 1; |
| 6143 | } |
| 6144 | |
| 6145 | /* Rewrites the request path. It relies on http_req_replace_stline(). */ |
| 6146 | __LJMP static int hlua_http_msg_set_path(lua_State *L) |
| 6147 | { |
| 6148 | struct stream *s; |
| 6149 | struct http_msg *msg; |
| 6150 | const char *name; |
| 6151 | size_t name_len; |
| 6152 | |
| 6153 | MAY_LJMP(check_args(L, 2, "set_path")); |
| 6154 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6155 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6156 | |
| 6157 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6158 | WILL_LJMP(lua_error(L)); |
| 6159 | |
| 6160 | s = chn_strm(msg->chn); |
| 6161 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, s->be, s) != -1); |
| 6162 | return 1; |
| 6163 | } |
| 6164 | |
| 6165 | /* Rewrites the request query-string. It relies on http_req_replace_stline(). */ |
| 6166 | __LJMP static int hlua_http_msg_set_query(lua_State *L) |
| 6167 | { |
| 6168 | struct stream *s; |
| 6169 | struct http_msg *msg; |
| 6170 | const char *name; |
| 6171 | size_t name_len; |
| 6172 | |
| 6173 | MAY_LJMP(check_args(L, 2, "set_query")); |
| 6174 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6175 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6176 | |
| 6177 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6178 | WILL_LJMP(lua_error(L)); |
| 6179 | |
| 6180 | /* Check length. */ |
| 6181 | if (name_len > trash.size - 1) { |
| 6182 | lua_pushboolean(L, 0); |
| 6183 | return 1; |
| 6184 | } |
| 6185 | |
| 6186 | /* Add the mark question as prefix. */ |
| 6187 | chunk_reset(&trash); |
| 6188 | trash.area[trash.data++] = '?'; |
| 6189 | memcpy(trash.area + trash.data, name, name_len); |
| 6190 | trash.data += name_len; |
| 6191 | |
| 6192 | s = chn_strm(msg->chn); |
| 6193 | lua_pushboolean(L, http_req_replace_stline(2, trash.area, trash.data, s->be, s) != -1); |
| 6194 | return 1; |
| 6195 | } |
| 6196 | |
| 6197 | /* Rewrites the request URI. It relies on http_req_replace_stline(). */ |
| 6198 | __LJMP static int hlua_http_msg_set_uri(lua_State *L) |
| 6199 | { |
| 6200 | struct stream *s; |
| 6201 | struct http_msg *msg; |
| 6202 | const char *name; |
| 6203 | size_t name_len; |
| 6204 | |
| 6205 | MAY_LJMP(check_args(L, 2, "set_uri")); |
| 6206 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6207 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6208 | |
| 6209 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6210 | WILL_LJMP(lua_error(L)); |
| 6211 | |
| 6212 | s = chn_strm(msg->chn); |
| 6213 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, s->be, s) != -1); |
| 6214 | return 1; |
| 6215 | } |
| 6216 | |
| 6217 | /* Rewrites the response status code. It relies on http_res_set_status(). */ |
| 6218 | __LJMP static int hlua_http_msg_set_status(lua_State *L) |
| 6219 | { |
| 6220 | struct http_msg *msg; |
| 6221 | unsigned int code; |
| 6222 | const char *reason; |
| 6223 | size_t reason_len; |
| 6224 | |
| 6225 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6226 | code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6227 | reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, &reason_len)); |
| 6228 | |
| 6229 | if (!(msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6230 | WILL_LJMP(lua_error(L)); |
| 6231 | |
| 6232 | lua_pushboolean(L, http_res_set_status(code, ist2(reason, reason_len), chn_strm(msg->chn)) != -1); |
| 6233 | return 1; |
| 6234 | } |
| 6235 | |
| 6236 | /* Returns true if the HTTP message is full. */ |
| 6237 | __LJMP static int hlua_http_msg_is_full(lua_State *L) |
| 6238 | { |
| 6239 | struct http_msg *msg; |
| 6240 | |
| 6241 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 6242 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6243 | lua_pushboolean(L, channel_full(msg->chn, 0)); |
| 6244 | return 1; |
| 6245 | } |
| 6246 | |
| 6247 | /* Returns true if the HTTP message may still receive data. */ |
| 6248 | __LJMP static int hlua_http_msg_may_recv(lua_State *L) |
| 6249 | { |
| 6250 | struct http_msg *msg; |
| 6251 | struct htx *htx; |
| 6252 | |
| 6253 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6254 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6255 | htx = htxbuf(&msg->chn->buf); |
| 6256 | lua_pushboolean(L, (htx_expect_more(htx) && !channel_input_closed(msg->chn) && channel_may_recv(msg->chn))); |
| 6257 | return 1; |
| 6258 | } |
| 6259 | |
| 6260 | /* Returns true if the HTTP message EOM was received */ |
| 6261 | __LJMP static int hlua_http_msg_is_eom(lua_State *L) |
| 6262 | { |
| 6263 | struct http_msg *msg; |
| 6264 | struct htx *htx; |
| 6265 | |
| 6266 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6267 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6268 | htx = htxbuf(&msg->chn->buf); |
| 6269 | lua_pushboolean(L, !htx_expect_more(htx)); |
| 6270 | return 1; |
| 6271 | } |
| 6272 | |
| 6273 | /* Returns the number of bytes available in the input side of the HTTP |
| 6274 | * message. This function never fails. |
| 6275 | */ |
| 6276 | __LJMP static int hlua_http_msg_get_in_len(lua_State *L) |
| 6277 | { |
| 6278 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6279 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6280 | |
| 6281 | MAY_LJMP(check_args(L, 1, "input")); |
| 6282 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6283 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6284 | lua_pushinteger(L, input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6285 | return 1; |
| 6286 | } |
| 6287 | |
| 6288 | /* Returns the number of bytes available in the output side of the HTTP |
| 6289 | * message. This function never fails. |
| 6290 | */ |
| 6291 | __LJMP static int hlua_http_msg_get_out_len(lua_State *L) |
| 6292 | { |
| 6293 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6294 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6295 | |
| 6296 | MAY_LJMP(check_args(L, 1, "output")); |
| 6297 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6298 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6299 | lua_pushinteger(L, output); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6300 | return 1; |
| 6301 | } |
| 6302 | |
| 6303 | /* Copies at most <len> bytes of DATA blocks from the HTTP message <msg> |
| 6304 | * 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] | 6305 | * 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] | 6306 | * block. This function is called during the payload filtering, so the headers |
| 6307 | * are already scheduled for output (from the filter point of view). |
| 6308 | */ |
| 6309 | static int _hlua_http_msg_dup(struct http_msg *msg, lua_State *L, size_t offset, size_t len) |
| 6310 | { |
| 6311 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 6312 | struct htx_blk *blk; |
| 6313 | struct htx_ret htxret; |
| 6314 | luaL_Buffer b; |
| 6315 | int ret = 0; |
| 6316 | |
| 6317 | luaL_buffinit(L, &b); |
| 6318 | htxret = htx_find_offset(htx, offset); |
| 6319 | for (blk = htxret.blk, offset = htxret.ret; blk && len; blk = htx_get_next_blk(htx, blk)) { |
| 6320 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6321 | struct ist v; |
| 6322 | |
| 6323 | switch (type) { |
| 6324 | case HTX_BLK_UNUSED: |
| 6325 | break; |
| 6326 | |
| 6327 | case HTX_BLK_DATA: |
| 6328 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6329 | v = istadv(v, offset); |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6330 | v = isttrim(v, len); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6331 | |
| 6332 | luaL_addlstring(&b, v.ptr, v.len); |
| 6333 | ret += v.len; |
| 6334 | break; |
| 6335 | |
| 6336 | default: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6337 | if (!ret) |
| 6338 | goto no_data; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6339 | goto end; |
| 6340 | } |
| 6341 | offset = 0; |
| 6342 | } |
| 6343 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6344 | end: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6345 | if (!ret && (htx->flags & HTX_FL_EOM)) |
| 6346 | goto no_data; |
| 6347 | luaL_pushresult(&b); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6348 | return ret; |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6349 | |
| 6350 | no_data: |
| 6351 | /* Remove the empty string and push nil on the stack */ |
| 6352 | lua_pop(L, 1); |
| 6353 | lua_pushnil(L); |
| 6354 | return 0; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6355 | } |
| 6356 | |
| 6357 | /* Copies the string <str> to the HTTP message <msg> at the offset |
| 6358 | * <offset>. This function returns -1 if data cannot be copied. Otherwise, it |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6359 | * returns the amount of data written. This function is responsible to update |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6360 | * the filter context. |
| 6361 | */ |
| 6362 | static int _hlua_http_msg_insert(struct http_msg *msg, struct filter *filter, struct ist str, size_t offset) |
| 6363 | { |
| 6364 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6365 | struct htx_ret htxret; |
| 6366 | int /*max, */ret = 0; |
| 6367 | |
| 6368 | /* Nothing to do, just return */ |
| 6369 | if (unlikely(istlen(str) == 0)) |
| 6370 | goto end; |
| 6371 | |
| 6372 | if (istlen(str) > htx_free_data_space(htx)) { |
| 6373 | ret = -1; |
| 6374 | goto end; |
| 6375 | } |
| 6376 | |
| 6377 | htxret = htx_find_offset(htx, offset); |
| 6378 | if (!htxret.blk || htx_get_blk_type(htxret.blk) != HTX_BLK_DATA) { |
| 6379 | if (!htx_add_last_data(htx, str)) |
| 6380 | goto end; |
| 6381 | } |
| 6382 | else { |
| 6383 | struct ist v = htx_get_blk_value(htx, htxret.blk); |
| 6384 | v.ptr += htxret.ret; |
| 6385 | v.len = 0; |
| 6386 | if (!htx_replace_blk_value(htx, htxret.blk, v, str)) |
| 6387 | goto end; |
| 6388 | } |
| 6389 | ret = str.len; |
| 6390 | if (ret) { |
| 6391 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6392 | flt_update_offsets(filter, msg->chn, ret); |
| 6393 | flt_ctx->cur_len[CHN_IDX(msg->chn)] += ret; |
| 6394 | } |
| 6395 | |
| 6396 | end: |
| 6397 | htx_to_buf(htx, &msg->chn->buf); |
| 6398 | return ret; |
| 6399 | } |
| 6400 | |
| 6401 | /* Helper function removing at most <len> bytes of DATA blocks at the absolute |
| 6402 | * position <offset>. It stops on the first non-DATA HTX block. This function is |
| 6403 | * called during the payload filtering, so the headers are already scheduled for |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6404 | * output (from the filter point of view). This function is responsible to |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6405 | * update the filter context. |
| 6406 | */ |
| 6407 | static void _hlua_http_msg_delete(struct http_msg *msg, struct filter *filter, size_t offset, size_t len) |
| 6408 | { |
| 6409 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6410 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6411 | struct htx_blk *blk; |
| 6412 | struct htx_ret htxret; |
| 6413 | size_t ret = 0; |
| 6414 | |
| 6415 | /* Be sure <len> is always the amount of DATA to remove */ |
| 6416 | if (htx->data == offset+len && htx_get_tail_type(htx) == HTX_BLK_DATA) { |
| 6417 | htx_truncate(htx, offset); |
| 6418 | ret = len; |
| 6419 | goto end; |
| 6420 | } |
| 6421 | |
| 6422 | htxret = htx_find_offset(htx, offset); |
| 6423 | blk = htxret.blk; |
| 6424 | if (htxret.ret) { |
| 6425 | struct ist v; |
| 6426 | |
| 6427 | if (htx_get_blk_type(blk) != HTX_BLK_DATA) |
| 6428 | goto end; |
| 6429 | v = htx_get_blk_value(htx, blk); |
| 6430 | v.ptr += htxret.ret; |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6431 | v = isttrim(v, len); |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6432 | blk = htx_replace_blk_value(htx, blk, v, IST_NULL); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6433 | len -= v.len; |
| 6434 | ret += v.len; |
| 6435 | } |
| 6436 | |
| 6437 | |
| 6438 | while (blk && len) { |
| 6439 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6440 | uint32_t sz = htx_get_blksz(blk); |
| 6441 | |
| 6442 | switch (type) { |
| 6443 | case HTX_BLK_UNUSED: |
| 6444 | break; |
| 6445 | |
| 6446 | case HTX_BLK_DATA: |
| 6447 | if (len < sz) { |
| 6448 | htx_cut_data_blk(htx, blk, len); |
| 6449 | ret += len; |
| 6450 | goto end; |
| 6451 | } |
| 6452 | break; |
| 6453 | |
| 6454 | default: |
| 6455 | goto end; |
| 6456 | } |
| 6457 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6458 | /* Remove all the data block */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6459 | len -= sz; |
| 6460 | ret += sz; |
| 6461 | blk = htx_remove_blk(htx, blk); |
| 6462 | } |
| 6463 | |
| 6464 | end: |
| 6465 | flt_update_offsets(filter, msg->chn, -ret); |
| 6466 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6467 | /* WARNING: we don't call htx_to_buf() on purpose, because we don't want |
| 6468 | * to loose the EOM flag if the message is empty. |
| 6469 | */ |
| 6470 | } |
| 6471 | |
| 6472 | /* Copies input data found in an HTTP message. Unlike the channel function used |
| 6473 | * to duplicate raw data, this one can only be called inside a filter, from |
| 6474 | * http_payload callback. So it cannot yield. An exception is returned if it is |
| 6475 | * called from another callback. If nothing was copied, a nil value is pushed on |
| 6476 | * the stack. |
| 6477 | */ |
| 6478 | __LJMP static int hlua_http_msg_get_body(lua_State *L) |
| 6479 | { |
| 6480 | struct http_msg *msg; |
| 6481 | struct filter *filter; |
| 6482 | size_t output, input; |
| 6483 | int offset, len; |
| 6484 | |
| 6485 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 6486 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 6487 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6488 | |
| 6489 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6490 | WILL_LJMP(lua_error(L)); |
| 6491 | |
| 6492 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6493 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6494 | WILL_LJMP(lua_error(L)); |
| 6495 | |
| 6496 | if (!ci_data(msg->chn) && channel_input_closed(msg->chn)) { |
| 6497 | lua_pushnil(L); |
| 6498 | return 1; |
| 6499 | } |
| 6500 | |
| 6501 | offset = output; |
| 6502 | if (lua_gettop(L) > 1) { |
| 6503 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6504 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6505 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6506 | offset += output; |
| 6507 | if (offset < output || offset > input + output) { |
| 6508 | lua_pushfstring(L, "offset out of range."); |
| 6509 | WILL_LJMP(lua_error(L)); |
| 6510 | } |
| 6511 | } |
| 6512 | len = output + input - offset; |
| 6513 | if (lua_gettop(L) == 3) { |
| 6514 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6515 | if (!len) |
| 6516 | goto dup; |
| 6517 | if (len == -1) |
| 6518 | len = global.tune.bufsize; |
| 6519 | if (len < 0) { |
| 6520 | lua_pushfstring(L, "length out of range."); |
| 6521 | WILL_LJMP(lua_error(L)); |
| 6522 | } |
| 6523 | } |
| 6524 | |
| 6525 | dup: |
| 6526 | _hlua_http_msg_dup(msg, L, offset, len); |
| 6527 | return 1; |
| 6528 | } |
| 6529 | |
| 6530 | /* Appends a string to the HTTP message, after all existing DATA blocks but |
| 6531 | * before the trailers, if any. It returns the amount of data written or -1 if |
| 6532 | * nothing was copied. Unlike the channel function used to append data, this one |
| 6533 | * can only be called inside a filter, from http_payload callback. So it cannot |
| 6534 | * yield. An exception is returned if it is called from another callback. |
| 6535 | */ |
| 6536 | __LJMP static int hlua_http_msg_append(lua_State *L) |
| 6537 | { |
| 6538 | struct http_msg *msg; |
| 6539 | struct filter *filter; |
| 6540 | const char *str; |
| 6541 | size_t offset, len, sz; |
| 6542 | int ret; |
| 6543 | |
| 6544 | MAY_LJMP(check_args(L, 2, "append")); |
| 6545 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6546 | |
| 6547 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6548 | WILL_LJMP(lua_error(L)); |
| 6549 | |
| 6550 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6551 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6552 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6553 | WILL_LJMP(lua_error(L)); |
| 6554 | |
| 6555 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset+len); |
| 6556 | lua_pushinteger(L, ret); |
| 6557 | return 1; |
| 6558 | } |
| 6559 | |
| 6560 | /* Prepends a string to the HTTP message, before all existing DATA blocks. It |
| 6561 | * returns the amount of data written or -1 if nothing was copied. Unlike the |
| 6562 | * channel function used to prepend data, this one can only be called inside a |
| 6563 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6564 | * returned if it is called from another callback. |
| 6565 | */ |
| 6566 | __LJMP static int hlua_http_msg_prepend(lua_State *L) |
| 6567 | { |
| 6568 | struct http_msg *msg; |
| 6569 | struct filter *filter; |
| 6570 | const char *str; |
| 6571 | size_t offset, len, sz; |
| 6572 | int ret; |
| 6573 | |
| 6574 | MAY_LJMP(check_args(L, 2, "prepend")); |
| 6575 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6576 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6577 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6578 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6579 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6580 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6581 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6582 | if (!filter || !hlua_filter_from_payload(filter)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6583 | WILL_LJMP(lua_error(L)); |
| 6584 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6585 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6586 | lua_pushinteger(L, ret); |
| 6587 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6588 | } |
| 6589 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6590 | /* Inserts a string to the HTTP message at a given offset. By default the string |
| 6591 | * is appended at the end of DATA blocks. It returns the amount of data written |
| 6592 | * or -1 if nothing was copied. Unlike the channel function used to insert data, |
| 6593 | * this one can only be called inside a filter, from http_payload callback. So |
| 6594 | * it cannot yield. An exception is returned if it is called from another |
| 6595 | * callback. |
| 6596 | */ |
| 6597 | __LJMP static int hlua_http_msg_insert_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6598 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6599 | struct http_msg *msg; |
| 6600 | struct filter *filter; |
| 6601 | const char *str; |
| 6602 | size_t input, output, sz; |
| 6603 | int offset; |
| 6604 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6605 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6606 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 6607 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
| 6608 | MAY_LJMP(check_args(L, 2, "insert")); |
| 6609 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
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 (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6612 | WILL_LJMP(lua_error(L)); |
| 6613 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6614 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6615 | filter = hlua_http_msg_filter(L, 1, msg, &input, &output); |
| 6616 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6617 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6618 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6619 | offset = input + output; |
| 6620 | if (lua_gettop(L) > 2) { |
| 6621 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6622 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6623 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6624 | offset += output; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6625 | if (offset < output || offset > output + input) { |
| 6626 | lua_pushfstring(L, "offset out of range."); |
| 6627 | WILL_LJMP(lua_error(L)); |
| 6628 | } |
| 6629 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6630 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6631 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6632 | lua_pushinteger(L, ret); |
| 6633 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6634 | } |
| 6635 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6636 | /* Removes a given amount of data from the HTTP message at a given offset. By |
| 6637 | * default all DATA blocks are removed. It returns the amount of data |
| 6638 | * removed. Unlike the channel function used to remove data, this one can only |
| 6639 | * be called inside a filter, from http_payload callback. So it cannot yield. An |
| 6640 | * exception is returned if it is called from another callback. |
| 6641 | */ |
| 6642 | __LJMP static int hlua_http_msg_del_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6643 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6644 | struct http_msg *msg; |
| 6645 | struct filter *filter; |
| 6646 | size_t input, output; |
| 6647 | int offset, len; |
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 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 6650 | WILL_LJMP(luaL_error(L, "'insert' expects at most 2 arguments")); |
| 6651 | MAY_LJMP(check_args(L, 2, "insert")); |
| 6652 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
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 (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6655 | WILL_LJMP(lua_error(L)); |
| 6656 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6657 | filter = hlua_http_msg_filter(L, 1, msg, &input, &output); |
| 6658 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6659 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6660 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6661 | offset = input + output; |
| 6662 | if (lua_gettop(L) > 2) { |
| 6663 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6664 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6665 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6666 | offset += output; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6667 | if (offset < output || offset > output + input) { |
| 6668 | lua_pushfstring(L, "offset out of range."); |
| 6669 | WILL_LJMP(lua_error(L)); |
| 6670 | } |
| 6671 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6672 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6673 | len = output + input - offset; |
| 6674 | if (lua_gettop(L) == 4) { |
| 6675 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6676 | if (!len) |
| 6677 | goto end; |
| 6678 | if (len == -1) |
| 6679 | len = output + input - offset; |
| 6680 | if (len < 0 || offset + len > output + input) { |
| 6681 | lua_pushfstring(L, "length out of range."); |
| 6682 | WILL_LJMP(lua_error(L)); |
| 6683 | } |
| 6684 | } |
| 6685 | |
| 6686 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6687 | |
| 6688 | end: |
| 6689 | lua_pushinteger(L, len); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6690 | return 1; |
| 6691 | } |
| 6692 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6693 | /* 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] | 6694 | * all remaining data are removed, accordingly to the filter context. It returns |
| 6695 | * the amount of data written or -1 if nothing was copied. Unlike the channel |
| 6696 | * function used to replace data, this one can only be called inside a filter, |
| 6697 | * from http_payload callback. So it cannot yield. An exception is returned if |
| 6698 | * it is called from another callback. |
| 6699 | */ |
| 6700 | __LJMP static int hlua_http_msg_set_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6701 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6702 | struct http_msg *msg; |
| 6703 | struct filter *filter; |
| 6704 | struct htx *htx; |
| 6705 | const char *str; |
| 6706 | size_t input, output, sz; |
| 6707 | int offset, len; |
| 6708 | int ret; |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 6709 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6710 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 6711 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 6712 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6713 | |
| 6714 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6715 | WILL_LJMP(lua_error(L)); |
| 6716 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6717 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6718 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6719 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6720 | WILL_LJMP(lua_error(L)); |
| 6721 | |
| 6722 | offset = output; |
| 6723 | if (lua_gettop(L) > 2) { |
| 6724 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6725 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6726 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6727 | offset += output; |
| 6728 | if (offset < output || offset > input + output) { |
| 6729 | lua_pushfstring(L, "offset out of range."); |
| 6730 | WILL_LJMP(lua_error(L)); |
| 6731 | } |
| 6732 | } |
| 6733 | |
| 6734 | len = output + input - offset; |
| 6735 | if (lua_gettop(L) == 4) { |
| 6736 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6737 | if (!len) |
| 6738 | goto set; |
| 6739 | if (len == -1) |
| 6740 | len = output + input - offset; |
| 6741 | if (len < 0 || offset + len > output + input) { |
| 6742 | lua_pushfstring(L, "length out of range."); |
| 6743 | WILL_LJMP(lua_error(L)); |
| 6744 | } |
| 6745 | } |
| 6746 | |
| 6747 | set: |
| 6748 | /* Be sure we can copied the string once input data will be removed. */ |
| 6749 | htx = htx_from_buf(&msg->chn->buf); |
| 6750 | if (sz > htx_free_data_space(htx) + len) |
| 6751 | lua_pushinteger(L, -1); |
| 6752 | else { |
| 6753 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6754 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6755 | lua_pushinteger(L, ret); |
| 6756 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6757 | return 1; |
| 6758 | } |
| 6759 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6760 | /* Prepends data into an HTTP message and forward it, from the filter point of |
| 6761 | * view. It returns the amount of data written or -1 if nothing was sent. Unlike |
| 6762 | * the channel function used to send data, this one can only be called inside a |
| 6763 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6764 | * returned if it is called from another callback. |
| 6765 | */ |
| 6766 | __LJMP static int hlua_http_msg_send(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6767 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6768 | struct http_msg *msg; |
| 6769 | struct filter *filter; |
| 6770 | struct htx *htx; |
| 6771 | const char *str; |
| 6772 | size_t offset, len, sz; |
| 6773 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6774 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6775 | MAY_LJMP(check_args(L, 2, "send")); |
| 6776 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6777 | |
| 6778 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6779 | WILL_LJMP(lua_error(L)); |
| 6780 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6781 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6782 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6783 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6784 | WILL_LJMP(lua_error(L)); |
| 6785 | |
| 6786 | /* Return an error if the channel's output is closed */ |
| 6787 | if (unlikely(channel_output_closed(msg->chn))) { |
| 6788 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6789 | return 1; |
| 6790 | } |
| 6791 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6792 | htx = htx_from_buf(&msg->chn->buf); |
| 6793 | if (sz > htx_free_data_space(htx)) { |
| 6794 | lua_pushinteger(L, -1); |
| 6795 | return 1; |
| 6796 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6797 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6798 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6799 | if (ret > 0) { |
| 6800 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6801 | |
| 6802 | FLT_OFF(filter, msg->chn) += ret; |
| 6803 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6804 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 6805 | } |
| 6806 | |
| 6807 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6808 | return 1; |
| 6809 | } |
| 6810 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6811 | /* Forwards a given amount of bytes. It return -1 if the channel's output is |
| 6812 | * closed. Otherwise, it returns the number of bytes forwarded. Unlike the |
| 6813 | * channel function used to forward data, this one can only be called inside a |
| 6814 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6815 | * returned if it is called from another callback. All other functions deal with |
| 6816 | * DATA block, this one not. |
| 6817 | */ |
| 6818 | __LJMP static int hlua_http_msg_forward(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6819 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6820 | struct http_msg *msg; |
| 6821 | struct filter *filter; |
| 6822 | size_t offset, len; |
| 6823 | int fwd, ret = 0; |
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 | MAY_LJMP(check_args(L, 2, "forward")); |
| 6826 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6827 | |
| 6828 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6829 | WILL_LJMP(lua_error(L)); |
| 6830 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6831 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6832 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6833 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6834 | WILL_LJMP(lua_error(L)); |
| 6835 | |
| 6836 | /* Nothing to do, just return */ |
| 6837 | if (!fwd) |
| 6838 | goto end; |
| 6839 | |
| 6840 | /* Return an error if the channel's output is closed */ |
| 6841 | if (unlikely(channel_output_closed(msg->chn))) { |
| 6842 | ret = -1; |
| 6843 | goto end; |
| 6844 | } |
| 6845 | |
| 6846 | ret = fwd; |
| 6847 | if (ret > len) |
| 6848 | ret = len; |
| 6849 | |
| 6850 | if (ret) { |
| 6851 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6852 | |
| 6853 | FLT_OFF(filter, msg->chn) += ret; |
| 6854 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 6855 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6856 | } |
| 6857 | |
| 6858 | end: |
| 6859 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6860 | return 1; |
| 6861 | } |
| 6862 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6863 | /* Set EOM flag on the HTX message. |
| 6864 | * |
| 6865 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 6866 | * really know how to do without this feature. |
| 6867 | */ |
| 6868 | __LJMP static int hlua_http_msg_set_eom(lua_State *L) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6869 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6870 | struct http_msg *msg; |
| 6871 | struct htx *htx; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6872 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6873 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 6874 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6875 | htx = htxbuf(&msg->chn->buf); |
| 6876 | htx->flags |= HTX_FL_EOM; |
| 6877 | return 0; |
| 6878 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6879 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6880 | /* Unset EOM flag on the HTX message. |
| 6881 | * |
| 6882 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 6883 | * really know how to do without this feature. |
| 6884 | */ |
| 6885 | __LJMP static int hlua_http_msg_unset_eom(lua_State *L) |
| 6886 | { |
| 6887 | struct http_msg *msg; |
| 6888 | struct htx *htx; |
| 6889 | |
| 6890 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 6891 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6892 | htx = htxbuf(&msg->chn->buf); |
| 6893 | htx->flags &= ~HTX_FL_EOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6894 | return 0; |
| 6895 | } |
| 6896 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6897 | /* |
| 6898 | * |
| 6899 | * |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 6900 | * Class HTTPClient |
| 6901 | * |
| 6902 | * |
| 6903 | */ |
| 6904 | __LJMP static struct hlua_httpclient *hlua_checkhttpclient(lua_State *L, int ud) |
| 6905 | { |
| 6906 | return MAY_LJMP(hlua_checkudata(L, ud, class_httpclient_ref)); |
| 6907 | } |
| 6908 | |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 6909 | |
| 6910 | /* stops the httpclient and ask it to kill itself */ |
| 6911 | __LJMP static int hlua_httpclient_gc(lua_State *L) |
| 6912 | { |
| 6913 | struct hlua_httpclient *hlua_hc; |
| 6914 | |
| 6915 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 6916 | |
| 6917 | hlua_hc = MAY_LJMP(hlua_checkhttpclient(L, 1)); |
| 6918 | |
| 6919 | httpclient_stop_and_destroy(hlua_hc->hc); |
| 6920 | |
| 6921 | hlua_hc->hc = NULL; |
| 6922 | |
| 6923 | |
| 6924 | return 0; |
| 6925 | } |
| 6926 | |
| 6927 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 6928 | __LJMP static int hlua_httpclient_new(lua_State *L) |
| 6929 | { |
| 6930 | struct hlua_httpclient *hlua_hc; |
| 6931 | struct hlua *hlua; |
| 6932 | |
| 6933 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6934 | hlua = hlua_gethlua(L); |
| 6935 | if (!hlua) |
| 6936 | return 0; |
| 6937 | |
| 6938 | /* Check stack size. */ |
| 6939 | if (!lua_checkstack(L, 3)) { |
| 6940 | hlua_pusherror(L, "httpclient: full stack"); |
| 6941 | goto err; |
| 6942 | } |
| 6943 | /* Create the object: obj[0] = userdata. */ |
| 6944 | lua_newtable(L); |
| 6945 | hlua_hc = MAY_LJMP(lua_newuserdata(L, sizeof(*hlua_hc))); |
| 6946 | lua_rawseti(L, -2, 0); |
| 6947 | memset(hlua_hc, 0, sizeof(*hlua_hc)); |
| 6948 | |
| 6949 | hlua_hc->hc = httpclient_new(hlua, 0, IST_NULL); |
| 6950 | if (!hlua_hc->hc) |
| 6951 | goto err; |
| 6952 | |
| 6953 | /* Pop a class stream metatable and affect it to the userdata. */ |
| 6954 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_httpclient_ref); |
| 6955 | lua_setmetatable(L, -2); |
| 6956 | |
| 6957 | return 1; |
| 6958 | |
| 6959 | err: |
| 6960 | WILL_LJMP(lua_error(L)); |
| 6961 | return 0; |
| 6962 | } |
| 6963 | |
| 6964 | |
| 6965 | /* |
| 6966 | * Callback of the httpclient, this callback wakes the lua task up, once the |
| 6967 | * httpclient receives some data |
| 6968 | * |
| 6969 | */ |
| 6970 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 6971 | static void hlua_httpclient_cb(struct httpclient *hc) |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 6972 | { |
| 6973 | struct hlua *hlua = hc->caller; |
| 6974 | |
| 6975 | if (!hlua || !hlua->task) |
| 6976 | return; |
| 6977 | |
| 6978 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 6979 | } |
| 6980 | |
| 6981 | /* |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 6982 | * Fill the lua stack with headers from the httpclient response |
| 6983 | * This works the same way as the hlua_http_get_headers() function |
| 6984 | */ |
| 6985 | __LJMP static int hlua_httpclient_get_headers(lua_State *L, struct hlua_httpclient *hlua_hc) |
| 6986 | { |
| 6987 | struct http_hdr *hdr; |
| 6988 | |
| 6989 | lua_newtable(L); |
| 6990 | |
William Lallemand | ef574b2 | 2021-10-05 16:19:31 +0200 | [diff] [blame] | 6991 | for (hdr = hlua_hc->hc->res.hdrs; hdr && isttest(hdr->n); hdr++) { |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 6992 | struct ist n, v; |
| 6993 | int len; |
| 6994 | |
| 6995 | n = hdr->n; |
| 6996 | v = hdr->v; |
| 6997 | |
| 6998 | /* Check for existing entry: |
| 6999 | * assume that the table is on the top of the stack, and |
| 7000 | * push the key in the stack, the function lua_gettable() |
| 7001 | * perform the lookup. |
| 7002 | */ |
| 7003 | |
| 7004 | lua_pushlstring(L, n.ptr, n.len); |
| 7005 | lua_gettable(L, -2); |
| 7006 | |
| 7007 | switch (lua_type(L, -1)) { |
| 7008 | case LUA_TNIL: |
| 7009 | /* Table not found, create it. */ |
| 7010 | lua_pop(L, 1); /* remove the nil value. */ |
| 7011 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 7012 | lua_newtable(L); /* create and push empty table. */ |
| 7013 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7014 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 7015 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 7016 | break; |
| 7017 | |
| 7018 | case LUA_TTABLE: |
| 7019 | /* Entry found: push the value in the table. */ |
| 7020 | len = lua_rawlen(L, -1); |
| 7021 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7022 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 7023 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 7024 | break; |
| 7025 | |
| 7026 | default: |
| 7027 | /* Other cases are errors. */ |
| 7028 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 7029 | WILL_LJMP(lua_error(L)); |
| 7030 | } |
| 7031 | } |
| 7032 | return 1; |
| 7033 | } |
| 7034 | |
| 7035 | /* |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7036 | * Allocate and return an array of http_hdr ist extracted from the <headers> lua table |
| 7037 | * |
| 7038 | * Caller must free the result |
| 7039 | */ |
| 7040 | struct http_hdr *hlua_httpclient_table_to_hdrs(lua_State *L) |
| 7041 | { |
| 7042 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
| 7043 | struct http_hdr *result = NULL; |
| 7044 | uint32_t hdr_num = 0; |
| 7045 | |
| 7046 | lua_pushnil(L); |
| 7047 | while (lua_next(L, -2) != 0) { |
| 7048 | struct ist name, value; |
| 7049 | const char *n, *v; |
| 7050 | size_t nlen, vlen; |
| 7051 | |
| 7052 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 7053 | /* Skip element if the key is not a string or if the value is not a table */ |
| 7054 | goto next_hdr; |
| 7055 | } |
| 7056 | |
| 7057 | n = lua_tolstring(L, -2, &nlen); |
| 7058 | name = ist2(n, nlen); |
| 7059 | |
| 7060 | /* Loop on header's values */ |
| 7061 | lua_pushnil(L); |
| 7062 | while (lua_next(L, -2)) { |
| 7063 | if (!lua_isstring(L, -1)) { |
| 7064 | /* Skip the value if it is not a string */ |
| 7065 | goto next_value; |
| 7066 | } |
| 7067 | |
| 7068 | v = lua_tolstring(L, -1, &vlen); |
| 7069 | value = ist2(v, vlen); |
| 7070 | name = ist2(n, nlen); |
| 7071 | |
| 7072 | hdrs[hdr_num].n = istdup(name); |
| 7073 | hdrs[hdr_num].v = istdup(value); |
| 7074 | |
| 7075 | hdr_num++; |
| 7076 | |
| 7077 | next_value: |
| 7078 | lua_pop(L, 1); |
| 7079 | } |
| 7080 | |
| 7081 | next_hdr: |
| 7082 | lua_pop(L, 1); |
| 7083 | |
| 7084 | } |
| 7085 | |
| 7086 | if (hdr_num) { |
| 7087 | /* alloc and copy the headers in the httpclient struct */ |
Tim Duesterhus | 16cc16d | 2021-11-06 15:14:45 +0100 | [diff] [blame] | 7088 | result = calloc((hdr_num + 1), sizeof(*result)); |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7089 | if (!result) |
| 7090 | goto skip_headers; |
| 7091 | memcpy(result, hdrs, sizeof(struct http_hdr) * (hdr_num + 1)); |
| 7092 | |
| 7093 | result[hdr_num].n = IST_NULL; |
| 7094 | result[hdr_num].v = IST_NULL; |
| 7095 | } |
| 7096 | |
| 7097 | skip_headers: |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7098 | |
| 7099 | return result; |
| 7100 | } |
| 7101 | |
| 7102 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7103 | /* |
| 7104 | * For each yield, checks if there is some data in the httpclient and push them |
| 7105 | * in the lua buffer, once the httpclient finished its job, push the result on |
| 7106 | * the stack |
| 7107 | */ |
| 7108 | __LJMP static int hlua_httpclient_rcv_yield(lua_State *L, int status, lua_KContext ctx) |
| 7109 | { |
| 7110 | struct buffer *tr; |
| 7111 | int res; |
| 7112 | struct hlua *hlua = hlua_gethlua(L); |
| 7113 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7114 | |
| 7115 | |
| 7116 | tr = get_trash_chunk(); |
| 7117 | |
| 7118 | res = httpclient_res_xfer(hlua_hc->hc, tr); |
| 7119 | luaL_addlstring(&hlua_hc->b, b_orig(tr), res); |
| 7120 | |
| 7121 | if (!httpclient_data(hlua_hc->hc) && httpclient_ended(hlua_hc->hc)) { |
| 7122 | |
| 7123 | luaL_pushresult(&hlua_hc->b); |
| 7124 | lua_settable(L, -3); |
| 7125 | |
| 7126 | lua_pushstring(L, "status"); |
| 7127 | lua_pushinteger(L, hlua_hc->hc->res.status); |
| 7128 | lua_settable(L, -3); |
| 7129 | |
| 7130 | |
| 7131 | lua_pushstring(L, "reason"); |
| 7132 | lua_pushlstring(L, hlua_hc->hc->res.reason.ptr, hlua_hc->hc->res.reason.len); |
| 7133 | lua_settable(L, -3); |
| 7134 | |
| 7135 | lua_pushstring(L, "headers"); |
| 7136 | hlua_httpclient_get_headers(L, hlua_hc); |
| 7137 | lua_settable(L, -3); |
| 7138 | |
| 7139 | return 1; |
| 7140 | } |
| 7141 | |
| 7142 | if (httpclient_data(hlua_hc->hc)) |
| 7143 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 7144 | |
| 7145 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7146 | |
| 7147 | return 0; |
| 7148 | } |
| 7149 | |
| 7150 | /* |
| 7151 | * Call this when trying to stream a body during a request |
| 7152 | */ |
| 7153 | __LJMP static int hlua_httpclient_snd_yield(lua_State *L, int status, lua_KContext ctx) |
| 7154 | { |
| 7155 | struct hlua *hlua; |
| 7156 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7157 | const char *body_str = NULL; |
| 7158 | int ret; |
| 7159 | int end = 0; |
| 7160 | size_t buf_len; |
| 7161 | size_t to_send = 0; |
| 7162 | |
| 7163 | hlua = hlua_gethlua(L); |
| 7164 | |
| 7165 | if (!hlua || !hlua->task) |
| 7166 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7167 | "'frontend', 'backend' or 'task'")); |
| 7168 | |
| 7169 | ret = lua_getfield(L, -1, "body"); |
| 7170 | if (ret != LUA_TSTRING) |
| 7171 | goto rcv; |
| 7172 | |
| 7173 | body_str = lua_tolstring(L, -1, &buf_len); |
| 7174 | lua_pop(L, 1); |
| 7175 | |
Christopher Faulet | fc59129 | 2022-01-12 14:46:03 +0100 | [diff] [blame] | 7176 | to_send = buf_len - hlua_hc->sent; |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7177 | |
| 7178 | if ((hlua_hc->sent + to_send) >= buf_len) |
| 7179 | end = 1; |
| 7180 | |
| 7181 | /* the end flag is always set since we are using the whole remaining size */ |
| 7182 | hlua_hc->sent += httpclient_req_xfer(hlua_hc->hc, ist2(body_str + hlua_hc->sent, to_send), end); |
| 7183 | |
| 7184 | if (buf_len > hlua_hc->sent) { |
| 7185 | /* still need to process the buffer */ |
| 7186 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_snd_yield, TICK_ETERNITY, 0)); |
| 7187 | } else { |
| 7188 | goto rcv; |
| 7189 | /* we sent the whole request buffer we can recv */ |
| 7190 | } |
| 7191 | return 0; |
| 7192 | |
| 7193 | rcv: |
| 7194 | |
| 7195 | /* we return a "res" object */ |
| 7196 | lua_newtable(L); |
| 7197 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7198 | lua_pushstring(L, "body"); |
William Lallemand | d1187eb | 2021-11-02 10:40:06 +0100 | [diff] [blame] | 7199 | luaL_buffinit(L, &hlua_hc->b); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7200 | |
William Lallemand | 933fe39 | 2021-11-04 09:45:58 +0100 | [diff] [blame] | 7201 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7202 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7203 | |
| 7204 | return 1; |
| 7205 | } |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7206 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7207 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7208 | * Send an HTTP request and wait for a response |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7209 | */ |
| 7210 | |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7211 | __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] | 7212 | { |
| 7213 | struct hlua_httpclient *hlua_hc; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7214 | struct http_hdr *hdrs = NULL; |
| 7215 | struct http_hdr *hdrs_i = NULL; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7216 | struct hlua *hlua; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7217 | const char *url_str = NULL; |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7218 | const char *body_str = NULL; |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7219 | size_t buf_len; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7220 | int ret; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7221 | |
| 7222 | hlua = hlua_gethlua(L); |
| 7223 | |
| 7224 | if (!hlua || !hlua->task) |
| 7225 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7226 | "'frontend', 'backend' or 'task'")); |
| 7227 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7228 | if (lua_gettop(L) != 2 || lua_type(L, -1) != LUA_TTABLE) |
| 7229 | WILL_LJMP(luaL_error(L, "'get' needs a table as argument")); |
| 7230 | |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 7231 | hlua_hc = hlua_checkhttpclient(L, 1); |
| 7232 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7233 | lua_pushnil(L); /* first key */ |
| 7234 | while (lua_next(L, 2)) { |
| 7235 | if (strcmp(lua_tostring(L, -2), "dst") == 0) { |
| 7236 | if (httpclient_set_dst(hlua_hc->hc, lua_tostring(L, -1)) < 0) |
| 7237 | WILL_LJMP(luaL_error(L, "Can't use the 'dst' argument")); |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 7238 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7239 | } else if (strcmp(lua_tostring(L, -2), "url") == 0) { |
| 7240 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7241 | WILL_LJMP(luaL_error(L, "invalid parameter in 'url', must be a string")); |
| 7242 | url_str = lua_tostring(L, -1); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7243 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7244 | } else if (strcmp(lua_tostring(L, -2), "timeout") == 0) { |
| 7245 | if (lua_type(L, -1) != LUA_TNUMBER) |
| 7246 | WILL_LJMP(luaL_error(L, "invalid parameter in 'timeout', must be a number")); |
| 7247 | httpclient_set_timeout(hlua_hc->hc, lua_tointeger(L, -1)); |
William Lallemand | b4a4ef6 | 2022-02-23 14:18:16 +0100 | [diff] [blame] | 7248 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7249 | } else if (strcmp(lua_tostring(L, -2), "headers") == 0) { |
| 7250 | if (lua_type(L, -1) != LUA_TTABLE) |
| 7251 | WILL_LJMP(luaL_error(L, "invalid parameter in 'headers', must be a table")); |
| 7252 | hdrs = hlua_httpclient_table_to_hdrs(L); |
William Lallemand | 79416cb | 2021-09-24 14:51:44 +0200 | [diff] [blame] | 7253 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7254 | } else if (strcmp(lua_tostring(L, -2), "body") == 0) { |
| 7255 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7256 | WILL_LJMP(luaL_error(L, "invalid parameter in 'body', must be a string")); |
| 7257 | body_str = lua_tolstring(L, -1, &buf_len); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7258 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7259 | } else { |
| 7260 | WILL_LJMP(luaL_error(L, "'%s' invalid parameter name", lua_tostring(L, -2))); |
| 7261 | } |
| 7262 | /* removes 'value'; keeps 'key' for next iteration */ |
| 7263 | lua_pop(L, 1); |
| 7264 | } |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7265 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7266 | if (!url_str) { |
| 7267 | WILL_LJMP(luaL_error(L, "'get' need a 'url' argument")); |
| 7268 | return 0; |
| 7269 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7270 | |
William Lallemand | 10a3736 | 2022-03-02 16:18:26 +0100 | [diff] [blame] | 7271 | hlua_hc->sent = 0; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7272 | |
| 7273 | hlua_hc->hc->req.url = istdup(ist(url_str)); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7274 | hlua_hc->hc->req.meth = meth; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7275 | |
| 7276 | /* update the httpclient callbacks */ |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7277 | hlua_hc->hc->ops.res_stline = hlua_httpclient_cb; |
| 7278 | hlua_hc->hc->ops.res_headers = hlua_httpclient_cb; |
| 7279 | hlua_hc->hc->ops.res_payload = hlua_httpclient_cb; |
William Lallemand | 8f170c7 | 2022-03-15 10:52:07 +0100 | [diff] [blame] | 7280 | hlua_hc->hc->ops.res_end = hlua_httpclient_cb; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7281 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7282 | /* a body is available, it will use the request callback */ |
| 7283 | if (body_str) { |
| 7284 | hlua_hc->hc->ops.req_payload = hlua_httpclient_cb; |
| 7285 | } |
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 | 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] | 7288 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7289 | /* free the temporary headers array */ |
| 7290 | hdrs_i = hdrs; |
| 7291 | while (hdrs_i && isttest(hdrs_i->n)) { |
| 7292 | istfree(&hdrs_i->n); |
| 7293 | istfree(&hdrs_i->v); |
| 7294 | hdrs_i++; |
| 7295 | } |
| 7296 | ha_free(&hdrs); |
| 7297 | |
| 7298 | |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7299 | if (ret != ERR_NONE) { |
| 7300 | WILL_LJMP(luaL_error(L, "Can't generate the HTTP request")); |
| 7301 | return 0; |
| 7302 | } |
| 7303 | |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7304 | httpclient_start(hlua_hc->hc); |
| 7305 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7306 | 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] | 7307 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7308 | return 0; |
| 7309 | } |
| 7310 | |
| 7311 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7312 | * Sends an HTTP HEAD request and wait for a response |
| 7313 | * |
| 7314 | * httpclient:head(url, headers, payload) |
| 7315 | */ |
| 7316 | __LJMP static int hlua_httpclient_head(lua_State *L) |
| 7317 | { |
| 7318 | return hlua_httpclient_send(L, HTTP_METH_HEAD); |
| 7319 | } |
| 7320 | |
| 7321 | /* |
| 7322 | * Send an HTTP GET request and wait for a response |
| 7323 | * |
| 7324 | * httpclient:get(url, headers, payload) |
| 7325 | */ |
| 7326 | __LJMP static int hlua_httpclient_get(lua_State *L) |
| 7327 | { |
| 7328 | return hlua_httpclient_send(L, HTTP_METH_GET); |
| 7329 | |
| 7330 | } |
| 7331 | |
| 7332 | /* |
| 7333 | * Sends an HTTP PUT request and wait for a response |
| 7334 | * |
| 7335 | * httpclient:put(url, headers, payload) |
| 7336 | */ |
| 7337 | __LJMP static int hlua_httpclient_put(lua_State *L) |
| 7338 | { |
| 7339 | return hlua_httpclient_send(L, HTTP_METH_PUT); |
| 7340 | } |
| 7341 | |
| 7342 | /* |
| 7343 | * Send an HTTP POST request and wait for a response |
| 7344 | * |
| 7345 | * httpclient:post(url, headers, payload) |
| 7346 | */ |
| 7347 | __LJMP static int hlua_httpclient_post(lua_State *L) |
| 7348 | { |
| 7349 | return hlua_httpclient_send(L, HTTP_METH_POST); |
| 7350 | } |
| 7351 | |
| 7352 | |
| 7353 | /* |
| 7354 | * Sends an HTTP DELETE request and wait for a response |
| 7355 | * |
| 7356 | * httpclient:delete(url, headers, payload) |
| 7357 | */ |
| 7358 | __LJMP static int hlua_httpclient_delete(lua_State *L) |
| 7359 | { |
| 7360 | return hlua_httpclient_send(L, HTTP_METH_DELETE); |
| 7361 | } |
| 7362 | |
| 7363 | /* |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7364 | * |
| 7365 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7366 | * Class TXN |
| 7367 | * |
| 7368 | * |
| 7369 | */ |
| 7370 | |
| 7371 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7372 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7373 | */ |
| 7374 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 7375 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7376 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7377 | } |
| 7378 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7379 | __LJMP static int hlua_set_var(lua_State *L) |
| 7380 | { |
| 7381 | struct hlua_txn *htxn; |
| 7382 | const char *name; |
| 7383 | size_t len; |
| 7384 | struct sample smp; |
| 7385 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 7386 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7387 | 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] | 7388 | |
| 7389 | /* It is useles to retrieve the stream, but this function |
| 7390 | * runs only in a stream context. |
| 7391 | */ |
| 7392 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7393 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7394 | |
| 7395 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 7396 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7397 | hlua_lua2smp(L, 3, &smp); |
| 7398 | |
| 7399 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7400 | 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] | 7401 | |
| 7402 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 7403 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 7404 | else |
| 7405 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 7406 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 7407 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7408 | } |
| 7409 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7410 | __LJMP static int hlua_unset_var(lua_State *L) |
| 7411 | { |
| 7412 | struct hlua_txn *htxn; |
| 7413 | const char *name; |
| 7414 | size_t len; |
| 7415 | struct sample smp; |
| 7416 | |
| 7417 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 7418 | |
| 7419 | /* It is useles to retrieve the stream, but this function |
| 7420 | * runs only in a stream context. |
| 7421 | */ |
| 7422 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7423 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7424 | |
| 7425 | /* Unset the variable. */ |
| 7426 | 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] | 7427 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 7428 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7429 | } |
| 7430 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7431 | __LJMP static int hlua_get_var(lua_State *L) |
| 7432 | { |
| 7433 | struct hlua_txn *htxn; |
| 7434 | const char *name; |
| 7435 | size_t len; |
| 7436 | struct sample smp; |
| 7437 | |
| 7438 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 7439 | |
| 7440 | /* It is useles to retrieve the stream, but this function |
| 7441 | * runs only in a stream context. |
| 7442 | */ |
| 7443 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7444 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7445 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7446 | 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] | 7447 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7448 | lua_pushnil(L); |
| 7449 | return 1; |
| 7450 | } |
| 7451 | |
| 7452 | return hlua_smp2lua(L, &smp); |
| 7453 | } |
| 7454 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7455 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7456 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7457 | struct hlua *hlua; |
| 7458 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7459 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 7460 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7461 | /* It is useles to retrieve the stream, but this function |
| 7462 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7463 | */ |
| 7464 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7465 | |
| 7466 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7467 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7468 | if (!hlua) |
| 7469 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7470 | |
| 7471 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 7472 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7473 | |
| 7474 | /* Get and store new value. */ |
| 7475 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 7476 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 7477 | |
| 7478 | return 0; |
| 7479 | } |
| 7480 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7481 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7482 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7483 | struct hlua *hlua; |
| 7484 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7485 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 7486 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7487 | /* It is useles to retrieve the stream, but this function |
| 7488 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7489 | */ |
| 7490 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7491 | |
| 7492 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7493 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7494 | if (!hlua) { |
| 7495 | lua_pushnil(L); |
| 7496 | return 1; |
| 7497 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7498 | |
| 7499 | /* Push configuration index in the stack. */ |
| 7500 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 7501 | |
| 7502 | return 1; |
| 7503 | } |
| 7504 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7505 | /* Create stack entry containing a class TXN. This function |
| 7506 | * return 0 if the stack does not contains free slots, |
| 7507 | * otherwise it returns 1. |
| 7508 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7509 | 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] | 7510 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7511 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7512 | |
| 7513 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7514 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7515 | return 0; |
| 7516 | |
| 7517 | /* NOTE: The allocation never fails. The failure |
| 7518 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7519 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7520 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7521 | /* Create the object: obj[0] = userdata. */ |
| 7522 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7523 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7524 | lua_rawseti(L, -2, 0); |
| 7525 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7526 | htxn->s = s; |
| 7527 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 7528 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7529 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7530 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7531 | /* Create the "f" field that contains a list of fetches. */ |
| 7532 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7533 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7534 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7535 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7536 | |
| 7537 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 7538 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7539 | 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] | 7540 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7541 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7542 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7543 | /* Create the "c" field that contains a list of converters. */ |
| 7544 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7545 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +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 | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7548 | |
| 7549 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 7550 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 7551 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +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 | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7554 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7555 | /* Create the "req" field that contains the request channel object. */ |
| 7556 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7557 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +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 | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7560 | |
| 7561 | /* Create the "res" field that contains the response channel object. */ |
| 7562 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7563 | if (!hlua_channel_new(L, &s->res)) |
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 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7567 | /* Creates the HTTP object is the current proxy allows http. */ |
| 7568 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 7569 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7570 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7571 | return 0; |
| 7572 | } |
| 7573 | else |
| 7574 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7575 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7576 | |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 7577 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) { |
| 7578 | /* HTTPMessage object are created when a lua TXN is created from |
| 7579 | * a filter context only |
| 7580 | */ |
| 7581 | |
| 7582 | /* Creates the HTTP-Request object is the current proxy allows http. */ |
| 7583 | lua_pushstring(L, "http_req"); |
| 7584 | if (p->mode == PR_MODE_HTTP) { |
| 7585 | if (!hlua_http_msg_new(L, &s->txn->req)) |
| 7586 | return 0; |
| 7587 | } |
| 7588 | else |
| 7589 | lua_pushnil(L); |
| 7590 | lua_rawset(L, -3); |
| 7591 | |
| 7592 | /* Creates the HTTP-Response object is the current proxy allows http. */ |
| 7593 | lua_pushstring(L, "http_res"); |
| 7594 | if (p->mode == PR_MODE_HTTP) { |
| 7595 | if (!hlua_http_msg_new(L, &s->txn->rsp)) |
| 7596 | return 0; |
| 7597 | } |
| 7598 | else |
| 7599 | lua_pushnil(L); |
| 7600 | lua_rawset(L, -3); |
| 7601 | } |
| 7602 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7603 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 7604 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 7605 | lua_setmetatable(L, -2); |
| 7606 | |
| 7607 | return 1; |
| 7608 | } |
| 7609 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7610 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 7611 | { |
| 7612 | const char *msg; |
| 7613 | struct hlua_txn *htxn; |
| 7614 | |
| 7615 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 7616 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7617 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7618 | |
| 7619 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 7620 | return 0; |
| 7621 | } |
| 7622 | |
| 7623 | __LJMP static int hlua_txn_log(lua_State *L) |
| 7624 | { |
| 7625 | int level; |
| 7626 | const char *msg; |
| 7627 | struct hlua_txn *htxn; |
| 7628 | |
| 7629 | MAY_LJMP(check_args(L, 3, "log")); |
| 7630 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7631 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7632 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 7633 | |
| 7634 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 7635 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 7636 | |
| 7637 | hlua_sendlog(htxn->s->be, level, msg); |
| 7638 | return 0; |
| 7639 | } |
| 7640 | |
| 7641 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 7642 | { |
| 7643 | const char *msg; |
| 7644 | struct hlua_txn *htxn; |
| 7645 | |
| 7646 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 7647 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7648 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7649 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 7650 | return 0; |
| 7651 | } |
| 7652 | |
| 7653 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 7654 | { |
| 7655 | const char *msg; |
| 7656 | struct hlua_txn *htxn; |
| 7657 | |
| 7658 | MAY_LJMP(check_args(L, 2, "Info")); |
| 7659 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7660 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7661 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 7662 | return 0; |
| 7663 | } |
| 7664 | |
| 7665 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 7666 | { |
| 7667 | const char *msg; |
| 7668 | struct hlua_txn *htxn; |
| 7669 | |
| 7670 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 7671 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7672 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7673 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 7674 | return 0; |
| 7675 | } |
| 7676 | |
| 7677 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 7678 | { |
| 7679 | const char *msg; |
| 7680 | struct hlua_txn *htxn; |
| 7681 | |
| 7682 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 7683 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7684 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7685 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 7686 | return 0; |
| 7687 | } |
| 7688 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7689 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 7690 | { |
| 7691 | struct hlua_txn *htxn; |
| 7692 | int ll; |
| 7693 | |
| 7694 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 7695 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7696 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7697 | |
| 7698 | if (ll < 0 || ll > 7) |
| 7699 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 7700 | |
| 7701 | htxn->s->logs.level = ll; |
| 7702 | return 0; |
| 7703 | } |
| 7704 | |
| 7705 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 7706 | { |
| 7707 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7708 | int tos; |
| 7709 | |
| 7710 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 7711 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7712 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7713 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 7714 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7715 | return 0; |
| 7716 | } |
| 7717 | |
| 7718 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 7719 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7720 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7721 | int mark; |
| 7722 | |
| 7723 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 7724 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7725 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7726 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 7727 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7728 | return 0; |
| 7729 | } |
| 7730 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 7731 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 7732 | { |
| 7733 | struct hlua_txn *htxn; |
| 7734 | |
| 7735 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 7736 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7737 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 7738 | return 0; |
| 7739 | } |
| 7740 | |
| 7741 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 7742 | { |
| 7743 | struct hlua_txn *htxn; |
| 7744 | |
| 7745 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 7746 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7747 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 7748 | return 0; |
| 7749 | } |
| 7750 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7751 | /* 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] | 7752 | * 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] | 7753 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 7754 | * error. The Reply must be on top of the stack. |
| 7755 | */ |
| 7756 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 7757 | { |
| 7758 | struct htx *htx; |
| 7759 | struct htx_sl *sl; |
| 7760 | struct h1m h1m; |
| 7761 | const char *status, *reason, *body; |
| 7762 | size_t status_len, reason_len, body_len; |
| 7763 | int ret, code, flags; |
| 7764 | |
| 7765 | code = 200; |
| 7766 | status = "200"; |
| 7767 | status_len = 3; |
| 7768 | ret = lua_getfield(L, -1, "status"); |
| 7769 | if (ret == LUA_TNUMBER) { |
| 7770 | code = lua_tointeger(L, -1); |
| 7771 | status = lua_tolstring(L, -1, &status_len); |
| 7772 | } |
| 7773 | lua_pop(L, 1); |
| 7774 | |
| 7775 | reason = http_get_reason(code); |
| 7776 | reason_len = strlen(reason); |
| 7777 | ret = lua_getfield(L, -1, "reason"); |
| 7778 | if (ret == LUA_TSTRING) |
| 7779 | reason = lua_tolstring(L, -1, &reason_len); |
| 7780 | lua_pop(L, 1); |
| 7781 | |
| 7782 | body = NULL; |
| 7783 | body_len = 0; |
| 7784 | ret = lua_getfield(L, -1, "body"); |
| 7785 | if (ret == LUA_TSTRING) |
| 7786 | body = lua_tolstring(L, -1, &body_len); |
| 7787 | lua_pop(L, 1); |
| 7788 | |
| 7789 | /* Prepare the response before inserting the headers */ |
| 7790 | h1m_init_res(&h1m); |
| 7791 | htx = htx_from_buf(&s->res.buf); |
| 7792 | channel_htx_truncate(&s->res, htx); |
| 7793 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 7794 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 7795 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 7796 | ist2(status, status_len), ist2(reason, reason_len)); |
| 7797 | } |
| 7798 | else { |
| 7799 | flags = HTX_SL_F_IS_RESP; |
| 7800 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 7801 | ist2(status, status_len), ist2(reason, reason_len)); |
| 7802 | } |
| 7803 | if (!sl) |
| 7804 | goto fail; |
| 7805 | sl->info.res.status = code; |
| 7806 | |
| 7807 | /* Push in the stack the "headers" entry. */ |
| 7808 | ret = lua_getfield(L, -1, "headers"); |
| 7809 | if (ret != LUA_TTABLE) |
| 7810 | goto skip_headers; |
| 7811 | |
| 7812 | lua_pushnil(L); |
| 7813 | while (lua_next(L, -2) != 0) { |
| 7814 | struct ist name, value; |
| 7815 | const char *n, *v; |
| 7816 | size_t nlen, vlen; |
| 7817 | |
| 7818 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 7819 | /* Skip element if the key is not a string or if the value is not a table */ |
| 7820 | goto next_hdr; |
| 7821 | } |
| 7822 | |
| 7823 | n = lua_tolstring(L, -2, &nlen); |
| 7824 | name = ist2(n, nlen); |
| 7825 | if (isteqi(name, ist("content-length"))) { |
| 7826 | /* Always skip content-length header. It will be added |
| 7827 | * later with the correct len |
| 7828 | */ |
| 7829 | goto next_hdr; |
| 7830 | } |
| 7831 | |
| 7832 | /* Loop on header's values */ |
| 7833 | lua_pushnil(L); |
| 7834 | while (lua_next(L, -2)) { |
| 7835 | if (!lua_isstring(L, -1)) { |
| 7836 | /* Skip the value if it is not a string */ |
| 7837 | goto next_value; |
| 7838 | } |
| 7839 | |
| 7840 | v = lua_tolstring(L, -1, &vlen); |
| 7841 | value = ist2(v, vlen); |
| 7842 | |
| 7843 | if (isteqi(name, ist("transfer-encoding"))) |
| 7844 | h1_parse_xfer_enc_header(&h1m, value); |
| 7845 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 7846 | goto fail; |
| 7847 | |
| 7848 | next_value: |
| 7849 | lua_pop(L, 1); |
| 7850 | } |
| 7851 | |
| 7852 | next_hdr: |
| 7853 | lua_pop(L, 1); |
| 7854 | } |
| 7855 | skip_headers: |
| 7856 | lua_pop(L, 1); |
| 7857 | |
| 7858 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 7859 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 7860 | const char *clen = ultoa(body_len); |
| 7861 | |
| 7862 | h1m.flags |= H1_MF_CLEN; |
| 7863 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 7864 | goto fail; |
| 7865 | } |
| 7866 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 7867 | h1m.flags |= H1_MF_XFER_LEN; |
| 7868 | |
| 7869 | /* Update HTX start-line flags */ |
| 7870 | if (h1m.flags & H1_MF_XFER_ENC) |
| 7871 | flags |= HTX_SL_F_XFER_ENC; |
| 7872 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 7873 | flags |= HTX_SL_F_XFER_LEN; |
| 7874 | if (h1m.flags & H1_MF_CHNK) |
| 7875 | flags |= HTX_SL_F_CHNK; |
| 7876 | else if (h1m.flags & H1_MF_CLEN) |
| 7877 | flags |= HTX_SL_F_CLEN; |
| 7878 | if (h1m.body_len == 0) |
| 7879 | flags |= HTX_SL_F_BODYLESS; |
| 7880 | } |
| 7881 | sl->flags |= flags; |
| 7882 | |
| 7883 | |
| 7884 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7885 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7886 | goto fail; |
| 7887 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7888 | htx->flags |= HTX_FL_EOM; |
| 7889 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7890 | /* Now, forward the response and terminate the transaction */ |
| 7891 | s->txn->status = code; |
| 7892 | htx_to_buf(htx, &s->res.buf); |
| 7893 | if (!http_forward_proxy_resp(s, 1)) |
| 7894 | goto fail; |
| 7895 | |
| 7896 | return 1; |
| 7897 | |
| 7898 | fail: |
| 7899 | channel_htx_truncate(&s->res, htx); |
| 7900 | return 0; |
| 7901 | } |
| 7902 | |
| 7903 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 7904 | * processing is just aborted. Nothing is returned to the client and all |
| 7905 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 7906 | * is forwarded to the client before terminating the transaction. On success, |
| 7907 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 7908 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 7909 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7910 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7911 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7912 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 7913 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7914 | struct stream *s; |
| 7915 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7916 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 7917 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7918 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7919 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 7920 | * just end the execution of the current lua code. */ |
| 7921 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7922 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7923 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7924 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 7925 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7926 | struct channel *req = &s->req; |
| 7927 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 7928 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7929 | channel_auto_read(req); |
| 7930 | channel_abort(req); |
| 7931 | channel_auto_close(req); |
| 7932 | channel_erase(req); |
| 7933 | |
| 7934 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 7935 | channel_auto_read(res); |
| 7936 | channel_auto_close(res); |
| 7937 | channel_shutr_now(res); |
| 7938 | |
| 7939 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 7940 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 7941 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 7942 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7943 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 7944 | /* No reply or invalid reply */ |
| 7945 | s->txn->status = 0; |
| 7946 | http_reply_and_close(s, 0, NULL); |
| 7947 | } |
| 7948 | else { |
| 7949 | /* Remove extra args to have the reply on top of the stack */ |
| 7950 | if (lua_gettop(L) > 2) |
| 7951 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7952 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7953 | if (!hlua_txn_forward_reply(L, s)) { |
| 7954 | if (!(s->flags & SF_ERR_MASK)) |
| 7955 | s->flags |= SF_ERR_PRXCOND; |
| 7956 | lua_pushinteger(L, ACT_RET_ERR); |
| 7957 | WILL_LJMP(hlua_done(L)); |
| 7958 | return 0; /* Never reached */ |
| 7959 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 7960 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7961 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7962 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 7963 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 7964 | /* let's log the request time */ |
| 7965 | s->logs.tv_request = now; |
| 7966 | 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] | 7967 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7968 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 7969 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7970 | done: |
| 7971 | if (!(s->flags & SF_ERR_MASK)) |
| 7972 | s->flags |= SF_ERR_LOCAL; |
| 7973 | if (!(s->flags & SF_FINST_MASK)) |
| 7974 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 7975 | |
Christopher Faulet | e48d1dc | 2021-08-13 14:11:17 +0200 | [diff] [blame] | 7976 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) |
| 7977 | lua_pushinteger(L, -1); |
| 7978 | else |
| 7979 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7980 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7981 | return 0; |
| 7982 | } |
| 7983 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7984 | /* |
| 7985 | * |
| 7986 | * |
| 7987 | * Class REPLY |
| 7988 | * |
| 7989 | * |
| 7990 | */ |
| 7991 | |
| 7992 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 7993 | * free slots, the function fails and returns 0; |
| 7994 | */ |
| 7995 | static int hlua_txn_reply_new(lua_State *L) |
| 7996 | { |
| 7997 | struct hlua_txn *htxn; |
| 7998 | const char *reason, *body = NULL; |
| 7999 | int ret, status; |
| 8000 | |
| 8001 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8002 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8003 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 8004 | WILL_LJMP(lua_error(L)); |
| 8005 | } |
| 8006 | |
| 8007 | /* Default value */ |
| 8008 | status = 200; |
| 8009 | reason = http_get_reason(status); |
| 8010 | |
| 8011 | if (lua_istable(L, 2)) { |
| 8012 | /* load status and reason from the table argument at index 2 */ |
| 8013 | ret = lua_getfield(L, 2, "status"); |
| 8014 | if (ret == LUA_TNIL) |
| 8015 | goto reason; |
| 8016 | else if (ret != LUA_TNUMBER) { |
| 8017 | /* invalid status: ignore the reason */ |
| 8018 | goto body; |
| 8019 | } |
| 8020 | status = lua_tointeger(L, -1); |
| 8021 | |
| 8022 | reason: |
| 8023 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 8024 | ret = lua_getfield(L, 2, "reason"); |
| 8025 | if (ret == LUA_TSTRING) |
| 8026 | reason = lua_tostring(L, -1); |
| 8027 | |
| 8028 | body: |
| 8029 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 8030 | ret = lua_getfield(L, 2, "body"); |
| 8031 | if (ret == LUA_TSTRING) |
| 8032 | body = lua_tostring(L, -1); |
| 8033 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 8034 | } |
| 8035 | |
| 8036 | /* Create the Reply table */ |
| 8037 | lua_newtable(L); |
| 8038 | |
| 8039 | /* Add status element */ |
| 8040 | lua_pushstring(L, "status"); |
| 8041 | lua_pushinteger(L, status); |
| 8042 | lua_settable(L, -3); |
| 8043 | |
| 8044 | /* Add reason element */ |
| 8045 | reason = http_get_reason(status); |
| 8046 | lua_pushstring(L, "reason"); |
| 8047 | lua_pushstring(L, reason); |
| 8048 | lua_settable(L, -3); |
| 8049 | |
| 8050 | /* Add body element, nil if undefined */ |
| 8051 | lua_pushstring(L, "body"); |
| 8052 | if (body) |
| 8053 | lua_pushstring(L, body); |
| 8054 | else |
| 8055 | lua_pushnil(L); |
| 8056 | lua_settable(L, -3); |
| 8057 | |
| 8058 | /* Add headers element */ |
| 8059 | lua_pushstring(L, "headers"); |
| 8060 | lua_newtable(L); |
| 8061 | |
| 8062 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8063 | if (lua_istable(L, 2)) { |
| 8064 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 8065 | ret = lua_getfield(L, 2, "headers"); |
| 8066 | if (ret == LUA_TTABLE) { |
| 8067 | /* stack: [ ... <headers:table>, <table> ] */ |
| 8068 | lua_pushnil(L); |
| 8069 | while (lua_next(L, -2) != 0) { |
| 8070 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 8071 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 8072 | /* invalid value type, skip it */ |
| 8073 | lua_pop(L, 1); |
| 8074 | continue; |
| 8075 | } |
| 8076 | |
| 8077 | |
| 8078 | /* Duplicate the key and swap it with the value. */ |
| 8079 | lua_pushvalue(L, -2); |
| 8080 | lua_insert(L, -2); |
| 8081 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 8082 | |
| 8083 | lua_newtable(L); |
| 8084 | lua_insert(L, -2); |
| 8085 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 8086 | |
| 8087 | if (lua_isstring(L, -1)) { |
| 8088 | /* push the value in the inner table */ |
| 8089 | lua_rawseti(L, -2, 1); |
| 8090 | } |
| 8091 | else { /* table */ |
| 8092 | lua_pushnil(L); |
| 8093 | while (lua_next(L, -2) != 0) { |
| 8094 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 8095 | if (!lua_isstring(L, -1)) { |
| 8096 | /* invalid value type, skip it*/ |
| 8097 | lua_pop(L, 1); |
| 8098 | continue; |
| 8099 | } |
| 8100 | /* push the value in the inner table */ |
| 8101 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 8102 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 8103 | } |
| 8104 | lua_pop(L, 1); |
| 8105 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 8106 | } |
| 8107 | |
| 8108 | /* push (k,v) on the stack in the headers table: |
| 8109 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 8110 | */ |
| 8111 | lua_settable(L, -5); |
| 8112 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 8113 | } |
| 8114 | } |
| 8115 | lua_pop(L, 1); |
| 8116 | } |
| 8117 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8118 | lua_settable(L, -3); |
| 8119 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 8120 | |
| 8121 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 8122 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 8123 | lua_setmetatable(L, -2); |
| 8124 | return 1; |
| 8125 | } |
| 8126 | |
| 8127 | /* Set the reply status code, and optionally the reason. If no reason is |
| 8128 | * provided, the default one corresponding to the status code is used. |
| 8129 | */ |
| 8130 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 8131 | { |
| 8132 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 8133 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 8134 | |
| 8135 | /* First argument (self) must be a table */ |
| 8136 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8137 | |
| 8138 | if (status < 100 || status > 599) { |
| 8139 | lua_pushboolean(L, 0); |
| 8140 | return 1; |
| 8141 | } |
| 8142 | if (!reason) |
| 8143 | reason = http_get_reason(status); |
| 8144 | |
| 8145 | lua_pushinteger(L, status); |
| 8146 | lua_setfield(L, 1, "status"); |
| 8147 | |
| 8148 | lua_pushstring(L, reason); |
| 8149 | lua_setfield(L, 1, "reason"); |
| 8150 | |
| 8151 | lua_pushboolean(L, 1); |
| 8152 | return 1; |
| 8153 | } |
| 8154 | |
| 8155 | /* Add a header into the reply object. Each header name is associated to an |
| 8156 | * array of values in the "headers" table. If the header name is not found, a |
| 8157 | * new entry is created. |
| 8158 | */ |
| 8159 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 8160 | { |
| 8161 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8162 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 8163 | int ret; |
| 8164 | |
| 8165 | /* First argument (self) must be a table */ |
| 8166 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8167 | |
| 8168 | /* Push in the stack the "headers" entry. */ |
| 8169 | ret = lua_getfield(L, 1, "headers"); |
| 8170 | if (ret != LUA_TTABLE) { |
| 8171 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 8172 | WILL_LJMP(lua_error(L)); |
| 8173 | } |
| 8174 | |
| 8175 | /* check if the header is already registered. If not, register it. */ |
| 8176 | ret = lua_getfield(L, -1, name); |
| 8177 | if (ret == LUA_TNIL) { |
| 8178 | /* Entry not found. */ |
| 8179 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 8180 | |
| 8181 | /* Insert the new header name in the array in the top of the stack. |
| 8182 | * It left the new array in the top of the stack. |
| 8183 | */ |
| 8184 | lua_newtable(L); |
| 8185 | lua_pushstring(L, name); |
| 8186 | lua_pushvalue(L, -2); |
| 8187 | lua_settable(L, -4); |
| 8188 | } |
| 8189 | else if (ret != LUA_TTABLE) { |
| 8190 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 8191 | WILL_LJMP(lua_error(L)); |
| 8192 | } |
| 8193 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8194 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8195 | * the header value as new entry. |
| 8196 | */ |
| 8197 | lua_pushstring(L, value); |
| 8198 | ret = lua_rawlen(L, -2); |
| 8199 | lua_rawseti(L, -2, ret + 1); |
| 8200 | |
| 8201 | lua_pushboolean(L, 1); |
| 8202 | return 1; |
| 8203 | } |
| 8204 | |
| 8205 | /* Remove all occurrences of a given header name. */ |
| 8206 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 8207 | { |
| 8208 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8209 | int ret; |
| 8210 | |
| 8211 | /* First argument (self) must be a table */ |
| 8212 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8213 | |
| 8214 | /* Push in the stack the "headers" entry. */ |
| 8215 | ret = lua_getfield(L, 1, "headers"); |
| 8216 | if (ret != LUA_TTABLE) { |
| 8217 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 8218 | WILL_LJMP(lua_error(L)); |
| 8219 | } |
| 8220 | |
| 8221 | lua_pushstring(L, name); |
| 8222 | lua_pushnil(L); |
| 8223 | lua_settable(L, -3); |
| 8224 | |
| 8225 | lua_pushboolean(L, 1); |
| 8226 | return 1; |
| 8227 | } |
| 8228 | |
| 8229 | /* Set the reply's body. Overwrite any existing entry. */ |
| 8230 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 8231 | { |
| 8232 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8233 | |
| 8234 | /* First argument (self) must be a table */ |
| 8235 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8236 | |
| 8237 | lua_pushstring(L, payload); |
| 8238 | lua_setfield(L, 1, "body"); |
| 8239 | |
| 8240 | lua_pushboolean(L, 1); |
| 8241 | return 1; |
| 8242 | } |
| 8243 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8244 | __LJMP static int hlua_log(lua_State *L) |
| 8245 | { |
| 8246 | int level; |
| 8247 | const char *msg; |
| 8248 | |
| 8249 | MAY_LJMP(check_args(L, 2, "log")); |
| 8250 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 8251 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8252 | |
| 8253 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 8254 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 8255 | |
| 8256 | hlua_sendlog(NULL, level, msg); |
| 8257 | return 0; |
| 8258 | } |
| 8259 | |
| 8260 | __LJMP static int hlua_log_debug(lua_State *L) |
| 8261 | { |
| 8262 | const char *msg; |
| 8263 | |
| 8264 | MAY_LJMP(check_args(L, 1, "debug")); |
| 8265 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8266 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 8267 | return 0; |
| 8268 | } |
| 8269 | |
| 8270 | __LJMP static int hlua_log_info(lua_State *L) |
| 8271 | { |
| 8272 | const char *msg; |
| 8273 | |
| 8274 | MAY_LJMP(check_args(L, 1, "info")); |
| 8275 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8276 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 8277 | return 0; |
| 8278 | } |
| 8279 | |
| 8280 | __LJMP static int hlua_log_warning(lua_State *L) |
| 8281 | { |
| 8282 | const char *msg; |
| 8283 | |
| 8284 | MAY_LJMP(check_args(L, 1, "warning")); |
| 8285 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8286 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 8287 | return 0; |
| 8288 | } |
| 8289 | |
| 8290 | __LJMP static int hlua_log_alert(lua_State *L) |
| 8291 | { |
| 8292 | const char *msg; |
| 8293 | |
| 8294 | MAY_LJMP(check_args(L, 1, "alert")); |
| 8295 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8296 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8297 | return 0; |
| 8298 | } |
| 8299 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8300 | __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] | 8301 | { |
| 8302 | int wakeup_ms = lua_tointeger(L, -1); |
Willy Tarreau | 12c0270 | 2021-09-30 16:12:31 +0200 | [diff] [blame] | 8303 | if (!tick_is_expired(wakeup_ms, now_ms)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8304 | 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] | 8305 | return 0; |
| 8306 | } |
| 8307 | |
| 8308 | __LJMP static int hlua_sleep(lua_State *L) |
| 8309 | { |
| 8310 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8311 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8312 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8313 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8314 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8315 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8316 | wakeup_ms = tick_add(now_ms, delay); |
| 8317 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8318 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8319 | 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] | 8320 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8321 | } |
| 8322 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8323 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8324 | { |
| 8325 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8326 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8327 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8328 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8329 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8330 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8331 | wakeup_ms = tick_add(now_ms, delay); |
| 8332 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8333 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8334 | 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] | 8335 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8336 | } |
| 8337 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8338 | /* This functionis an LUA binding. it permits to give back |
| 8339 | * the hand at the HAProxy scheduler. It is used when the |
| 8340 | * LUA processing consumes a lot of time. |
| 8341 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8342 | __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] | 8343 | { |
| 8344 | return 0; |
| 8345 | } |
| 8346 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8347 | __LJMP static int hlua_yield(lua_State *L) |
| 8348 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8349 | 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] | 8350 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8351 | } |
| 8352 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8353 | /* This function change the nice of the currently executed |
| 8354 | * task. It is used set low or high priority at the current |
| 8355 | * task. |
| 8356 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8357 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8358 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8359 | struct hlua *hlua; |
| 8360 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8361 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8362 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8363 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8364 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8365 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 8366 | hlua = hlua_gethlua(L); |
| 8367 | |
| 8368 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8369 | if (!hlua || !hlua->task) |
| 8370 | return 0; |
| 8371 | |
| 8372 | if (nice < -1024) |
| 8373 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8374 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8375 | nice = 1024; |
| 8376 | |
| 8377 | hlua->task->nice = nice; |
| 8378 | return 0; |
| 8379 | } |
| 8380 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8381 | /* 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] | 8382 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 8383 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 8384 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8385 | * |
| 8386 | * Task wrapper are longjmp safe because the only one Lua code |
| 8387 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8388 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 8389 | 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] | 8390 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 8391 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8392 | enum hlua_exec status; |
| 8393 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 8394 | if (task->thread_mask == MAX_THREADS_MASK) |
| 8395 | task_set_affinity(task, tid_bit); |
| 8396 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8397 | /* If it is the first call to the task, we must initialize the |
| 8398 | * execution timeouts. |
| 8399 | */ |
| 8400 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 8401 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8402 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8403 | /* Execute the Lua code. */ |
| 8404 | status = hlua_ctx_resume(hlua, 1); |
| 8405 | |
| 8406 | switch (status) { |
| 8407 | /* finished or yield */ |
| 8408 | case HLUA_E_OK: |
| 8409 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8410 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 8411 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8412 | break; |
| 8413 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 8414 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 8415 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 8416 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8417 | break; |
| 8418 | |
| 8419 | /* finished with error. */ |
| 8420 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8421 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8422 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8423 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8424 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8425 | break; |
| 8426 | |
| 8427 | case HLUA_E_ERR: |
| 8428 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8429 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8430 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8431 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8432 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8433 | break; |
| 8434 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8435 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8436 | } |
| 8437 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8438 | /* This function is an LUA binding that register LUA function to be |
| 8439 | * executed after the HAProxy configuration parsing and before the |
| 8440 | * HAProxy scheduler starts. This function expect only one LUA |
| 8441 | * argument that is a function. This function returns nothing, but |
| 8442 | * throws if an error is encountered. |
| 8443 | */ |
| 8444 | __LJMP static int hlua_register_init(lua_State *L) |
| 8445 | { |
| 8446 | struct hlua_init_function *init; |
| 8447 | int ref; |
| 8448 | |
| 8449 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 8450 | |
| 8451 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8452 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8453 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8454 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8455 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8456 | |
| 8457 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 8458 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8459 | return 0; |
| 8460 | } |
| 8461 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8462 | /* This functio is an LUA binding. It permits to register a task |
| 8463 | * executed in parallel of the main HAroxy activity. The task is |
| 8464 | * created and it is set in the HAProxy scheduler. It can be called |
| 8465 | * from the "init" section, "post init" or during the runtime. |
| 8466 | * |
| 8467 | * Lua prototype: |
| 8468 | * |
| 8469 | * <none> core.register_task(<function>) |
| 8470 | */ |
| 8471 | static int hlua_register_task(lua_State *L) |
| 8472 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8473 | struct hlua *hlua = NULL; |
| 8474 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8475 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8476 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8477 | |
| 8478 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 8479 | |
| 8480 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8481 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8482 | /* Get the reference state. If the reference is NULL, L is the master |
| 8483 | * state, otherwise hlua->T is. |
| 8484 | */ |
| 8485 | hlua = hlua_gethlua(L); |
| 8486 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8487 | /* we are in runtime processing */ |
| 8488 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8489 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8490 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8491 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8492 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8493 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8494 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8495 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8496 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8497 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8498 | /* We are in the common lua state, execute the task anywhere, |
| 8499 | * otherwise, inherit the current thread identifier |
| 8500 | */ |
| 8501 | if (state_id == 0) |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8502 | task = task_new_anywhere(); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8503 | else |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8504 | task = task_new_here(); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8505 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8506 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8507 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8508 | task->context = hlua; |
| 8509 | task->process = hlua_process_task; |
| 8510 | |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8511 | if (!hlua_ctx_init(hlua, state_id, task, 1)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8512 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8513 | |
| 8514 | /* Restore the function in the stack. */ |
| 8515 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 8516 | hlua->nargs = 0; |
| 8517 | |
| 8518 | /* Schedule task. */ |
Willy Tarreau | e3957f8 | 2021-09-30 16:17:37 +0200 | [diff] [blame] | 8519 | task_wakeup(task, TASK_WOKEN_INIT); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8520 | |
| 8521 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8522 | |
| 8523 | alloc_error: |
| 8524 | task_destroy(task); |
| 8525 | hlua_ctx_destroy(hlua); |
| 8526 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8527 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8528 | } |
| 8529 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8530 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 8531 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 8532 | * resume converters. |
| 8533 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8534 | 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] | 8535 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8536 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8537 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8538 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8539 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8540 | if (!stream) |
| 8541 | return 0; |
| 8542 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8543 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8544 | * Lua context can be not initialized. This behavior |
| 8545 | * permits to save performances because a systematic |
| 8546 | * Lua initialization cause 5% performances loss. |
| 8547 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8548 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8549 | struct hlua *hlua; |
| 8550 | |
| 8551 | hlua = pool_alloc(pool_head_hlua); |
| 8552 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8553 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8554 | return 0; |
| 8555 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8556 | HLUA_INIT(hlua); |
| 8557 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8558 | 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] | 8559 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8560 | return 0; |
| 8561 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8562 | } |
| 8563 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8564 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8565 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8566 | |
| 8567 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8568 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8569 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8570 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8571 | else |
| 8572 | error = "critical error"; |
| 8573 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8574 | return 0; |
| 8575 | } |
| 8576 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8577 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8578 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8579 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8580 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8581 | return 0; |
| 8582 | } |
| 8583 | |
| 8584 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8585 | 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] | 8586 | |
| 8587 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8588 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8589 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8590 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8591 | return 0; |
| 8592 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8593 | hlua_smp2lua(stream->hlua->T, smp); |
| 8594 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8595 | |
| 8596 | /* push keywords in the stack. */ |
| 8597 | if (arg_p) { |
| 8598 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8599 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8600 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8601 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8602 | return 0; |
| 8603 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8604 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8605 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8606 | } |
| 8607 | } |
| 8608 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8609 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8610 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8611 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8612 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8613 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8614 | } |
| 8615 | |
| 8616 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8617 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8618 | /* finished. */ |
| 8619 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8620 | /* If the stack is empty, the function fails. */ |
| 8621 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8622 | return 0; |
| 8623 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8624 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8625 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8626 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8627 | return 1; |
| 8628 | |
| 8629 | /* yield. */ |
| 8630 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8631 | 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] | 8632 | return 0; |
| 8633 | |
| 8634 | /* finished with error. */ |
| 8635 | case HLUA_E_ERRMSG: |
| 8636 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8637 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8638 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8639 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8640 | return 0; |
| 8641 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8642 | case HLUA_E_ETMOUT: |
| 8643 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 8644 | return 0; |
| 8645 | |
| 8646 | case HLUA_E_NOMEM: |
| 8647 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 8648 | return 0; |
| 8649 | |
| 8650 | case HLUA_E_YIELD: |
| 8651 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 8652 | return 0; |
| 8653 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8654 | case HLUA_E_ERR: |
| 8655 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8656 | 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] | 8657 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8658 | |
| 8659 | default: |
| 8660 | return 0; |
| 8661 | } |
| 8662 | } |
| 8663 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8664 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 8665 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8666 | * resume sample-fetches. This function will be called by the sample |
| 8667 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8668 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8669 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 8670 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8671 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8672 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8673 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8674 | const char *error; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 8675 | unsigned int hflags = HLUA_TXN_NOTERM | HLUA_TXN_SMP_CTX; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8676 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8677 | if (!stream) |
| 8678 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8679 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8680 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8681 | * Lua context can be not initialized. This behavior |
| 8682 | * permits to save performances because a systematic |
| 8683 | * Lua initialization cause 5% performances loss. |
| 8684 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8685 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8686 | struct hlua *hlua; |
| 8687 | |
| 8688 | hlua = pool_alloc(pool_head_hlua); |
| 8689 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8690 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8691 | return 0; |
| 8692 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8693 | hlua->T = NULL; |
| 8694 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8695 | 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] | 8696 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8697 | return 0; |
| 8698 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8699 | } |
| 8700 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8701 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8702 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8703 | |
| 8704 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8705 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8706 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8707 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8708 | else |
| 8709 | error = "critical error"; |
| 8710 | 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] | 8711 | return 0; |
| 8712 | } |
| 8713 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8714 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8715 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8716 | 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] | 8717 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8718 | return 0; |
| 8719 | } |
| 8720 | |
| 8721 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8722 | 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] | 8723 | |
| 8724 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 8725 | 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] | 8726 | 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] | 8727 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8728 | return 0; |
| 8729 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8730 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8731 | |
| 8732 | /* push keywords in the stack. */ |
| 8733 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 8734 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8735 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8736 | 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] | 8737 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8738 | return 0; |
| 8739 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8740 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8741 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8742 | } |
| 8743 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8744 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8745 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8746 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8747 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8748 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8749 | } |
| 8750 | |
| 8751 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8752 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8753 | /* finished. */ |
| 8754 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8755 | /* If the stack is empty, the function fails. */ |
| 8756 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8757 | return 0; |
| 8758 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8759 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8760 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8761 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8762 | |
| 8763 | /* Set the end of execution flag. */ |
| 8764 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 8765 | return 1; |
| 8766 | |
| 8767 | /* yield. */ |
| 8768 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8769 | 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] | 8770 | return 0; |
| 8771 | |
| 8772 | /* finished with error. */ |
| 8773 | case HLUA_E_ERRMSG: |
| 8774 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8775 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8776 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8777 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8778 | return 0; |
| 8779 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8780 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8781 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 8782 | return 0; |
| 8783 | |
| 8784 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8785 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 8786 | return 0; |
| 8787 | |
| 8788 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8789 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 8790 | return 0; |
| 8791 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8792 | case HLUA_E_ERR: |
| 8793 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8794 | 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] | 8795 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8796 | |
| 8797 | default: |
| 8798 | return 0; |
| 8799 | } |
| 8800 | } |
| 8801 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8802 | /* This function is an LUA binding used for registering |
| 8803 | * "sample-conv" functions. It expects a converter name used |
| 8804 | * in the haproxy configuration file, and an LUA function. |
| 8805 | */ |
| 8806 | __LJMP static int hlua_register_converters(lua_State *L) |
| 8807 | { |
| 8808 | struct sample_conv_kw_list *sck; |
| 8809 | const char *name; |
| 8810 | int ref; |
| 8811 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8812 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8813 | struct sample_conv *sc; |
| 8814 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8815 | |
| 8816 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 8817 | |
| 8818 | /* First argument : converter name. */ |
| 8819 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8820 | |
| 8821 | /* Second argument : lua function. */ |
| 8822 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 8823 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8824 | /* Check if the converter is already registered */ |
| 8825 | trash = get_trash_chunk(); |
| 8826 | chunk_printf(trash, "lua.%s", name); |
| 8827 | sc = find_sample_conv(trash->area, trash->data); |
| 8828 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8829 | fcn = sc->private; |
| 8830 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8831 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 8832 | "This will become a hard error in version 2.5.\n", name); |
| 8833 | } |
| 8834 | fcn->function_ref[hlua_state_id] = ref; |
| 8835 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8836 | } |
| 8837 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8838 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8839 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8840 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8841 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8842 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8843 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8844 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8845 | |
| 8846 | /* Fill fcn. */ |
| 8847 | fcn->name = strdup(name); |
| 8848 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8849 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8850 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8851 | |
| 8852 | /* List head */ |
| 8853 | sck->list.n = sck->list.p = NULL; |
| 8854 | |
| 8855 | /* converter keyword. */ |
| 8856 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8857 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8858 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8859 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8860 | |
| 8861 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 8862 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 8863 | 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] | 8864 | sck->kw[0].val_args = NULL; |
| 8865 | sck->kw[0].in_type = SMP_T_STR; |
| 8866 | sck->kw[0].out_type = SMP_T_STR; |
| 8867 | sck->kw[0].private = fcn; |
| 8868 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8869 | /* Register this new converter */ |
| 8870 | sample_register_convs(sck); |
| 8871 | |
| 8872 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8873 | |
| 8874 | alloc_error: |
| 8875 | release_hlua_function(fcn); |
| 8876 | ha_free(&sck); |
| 8877 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8878 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8879 | } |
| 8880 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8881 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8882 | * "sample-fetch" functions. It expects a converter name used |
| 8883 | * in the haproxy configuration file, and an LUA function. |
| 8884 | */ |
| 8885 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 8886 | { |
| 8887 | const char *name; |
| 8888 | int ref; |
| 8889 | int len; |
| 8890 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8891 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8892 | struct sample_fetch *sf; |
| 8893 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8894 | |
| 8895 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 8896 | |
| 8897 | /* First argument : sample-fetch name. */ |
| 8898 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8899 | |
| 8900 | /* Second argument : lua function. */ |
| 8901 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 8902 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8903 | /* Check if the sample-fetch is already registered */ |
| 8904 | trash = get_trash_chunk(); |
| 8905 | chunk_printf(trash, "lua.%s", name); |
| 8906 | sf = find_sample_fetch(trash->area, trash->data); |
| 8907 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8908 | fcn = sf->private; |
| 8909 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8910 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 8911 | "This will become a hard error in version 2.5.\n", name); |
| 8912 | } |
| 8913 | fcn->function_ref[hlua_state_id] = ref; |
| 8914 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8915 | } |
| 8916 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8917 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8918 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8919 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8920 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8921 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8922 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8923 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8924 | |
| 8925 | /* Fill fcn. */ |
| 8926 | fcn->name = strdup(name); |
| 8927 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8928 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8929 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8930 | |
| 8931 | /* List head */ |
| 8932 | sfk->list.n = sfk->list.p = NULL; |
| 8933 | |
| 8934 | /* sample-fetch keyword. */ |
| 8935 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8936 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8937 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8938 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8939 | |
| 8940 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 8941 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 8942 | 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] | 8943 | sfk->kw[0].val_args = NULL; |
| 8944 | sfk->kw[0].out_type = SMP_T_STR; |
| 8945 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 8946 | sfk->kw[0].val = 0; |
| 8947 | sfk->kw[0].private = fcn; |
| 8948 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8949 | /* Register this new fetch. */ |
| 8950 | sample_register_fetches(sfk); |
| 8951 | |
| 8952 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8953 | |
| 8954 | alloc_error: |
| 8955 | release_hlua_function(fcn); |
| 8956 | ha_free(&sfk); |
| 8957 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8958 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8959 | } |
| 8960 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 8961 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8962 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 8963 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8964 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8965 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8966 | unsigned int delay; |
| 8967 | unsigned int wakeup_ms; |
| 8968 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8969 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 8970 | hlua = hlua_gethlua(L); |
| 8971 | if (!hlua) { |
| 8972 | return 0; |
| 8973 | } |
| 8974 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8975 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 8976 | |
| 8977 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 8978 | wakeup_ms = tick_add(now_ms, delay); |
| 8979 | hlua->wake_time = wakeup_ms; |
| 8980 | return 0; |
| 8981 | } |
| 8982 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8983 | /* This function is a wrapper to execute each LUA function declared as an action |
| 8984 | * wrapper during the initialisation period. This function may return any |
| 8985 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 8986 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 8987 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8988 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 8989 | 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] | 8990 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8991 | { |
| 8992 | char **arg; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 8993 | unsigned int hflags = HLUA_TXN_ACT_CTX; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8994 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8995 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 8996 | |
| 8997 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8998 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 8999 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 9000 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 9001 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9002 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9003 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9004 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9005 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9006 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9007 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9008 | * Lua context can be not initialized. This behavior |
| 9009 | * permits to save performances because a systematic |
| 9010 | * Lua initialization cause 5% performances loss. |
| 9011 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9012 | if (!s->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9013 | struct hlua *hlua; |
| 9014 | |
| 9015 | hlua = pool_alloc(pool_head_hlua); |
| 9016 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9017 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9018 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9019 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9020 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9021 | HLUA_INIT(hlua); |
| 9022 | s->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9023 | 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] | 9024 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9025 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9026 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9027 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9028 | } |
| 9029 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9030 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9031 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9032 | |
| 9033 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9034 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9035 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 9036 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9037 | else |
| 9038 | error = "critical error"; |
| 9039 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9040 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9041 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9042 | } |
| 9043 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9044 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9045 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9046 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9047 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9048 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9049 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9050 | } |
| 9051 | |
| 9052 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9053 | 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] | 9054 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9055 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 9056 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9057 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9058 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9059 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9060 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9061 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9062 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9063 | |
| 9064 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9065 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9066 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9067 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9068 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9069 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9070 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9071 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9072 | lua_pushstring(s->hlua->T, *arg); |
| 9073 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9074 | } |
| 9075 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9076 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9077 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9078 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9079 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9080 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9081 | } |
| 9082 | |
| 9083 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 9084 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9085 | /* finished. */ |
| 9086 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9087 | /* Catch the return value */ |
| 9088 | if (lua_gettop(s->hlua->T) > 0) |
| 9089 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9090 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9091 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 9092 | if (act_ret == ACT_RET_YIELD) { |
| 9093 | if (flags & ACT_OPT_FINAL) |
| 9094 | goto err_yield; |
| 9095 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9096 | if (dir == SMP_OPT_DIR_REQ) |
| 9097 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 9098 | s->hlua->wake_time); |
| 9099 | else |
| 9100 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 9101 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9102 | } |
| 9103 | goto end; |
| 9104 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9105 | /* yield. */ |
| 9106 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 9107 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9108 | if (dir == SMP_OPT_DIR_REQ) |
| 9109 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 9110 | s->hlua->wake_time); |
| 9111 | else |
| 9112 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 9113 | s->hlua->wake_time); |
| 9114 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9115 | /* Some actions can be wake up when a "write" event |
| 9116 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9117 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9118 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 9119 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9120 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9121 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9122 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9123 | act_ret = ACT_RET_YIELD; |
| 9124 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9125 | |
| 9126 | /* finished with error. */ |
| 9127 | case HLUA_E_ERRMSG: |
| 9128 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9129 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9130 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9131 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9132 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9133 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9134 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9135 | 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] | 9136 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9137 | |
| 9138 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9139 | 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] | 9140 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9141 | |
| 9142 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 9143 | err_yield: |
| 9144 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9145 | 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] | 9146 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9147 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9148 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9149 | case HLUA_E_ERR: |
| 9150 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9151 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9152 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9153 | |
| 9154 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9155 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9156 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9157 | |
| 9158 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 9159 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9160 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9161 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9162 | } |
| 9163 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 9164 | 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] | 9165 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 9166 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9167 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9168 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 9169 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 9170 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9171 | } |
| 9172 | |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9173 | static int hlua_applet_tcp_init(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9174 | { |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 9175 | struct conn_stream *cs = ctx->owner; |
| 9176 | struct stream *strm = __cs_strm(cs); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9177 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9178 | struct task *task; |
| 9179 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9180 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9181 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9182 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9183 | if (!hlua) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9184 | SEND_ERR(strm->be, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9185 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9186 | return 0; |
| 9187 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9188 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9189 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9190 | ctx->ctx.hlua_apptcp.flags = 0; |
| 9191 | |
| 9192 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 9193 | task = task_new_here(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9194 | if (!task) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9195 | SEND_ERR(strm->be, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9196 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9197 | return 0; |
| 9198 | } |
| 9199 | task->nice = 0; |
| 9200 | task->context = ctx; |
| 9201 | task->process = hlua_applet_wakeup; |
| 9202 | ctx->ctx.hlua_apptcp.task = task; |
| 9203 | |
| 9204 | /* In the execution wrappers linked with a stream, the |
| 9205 | * Lua context can be not initialized. This behavior |
| 9206 | * permits to save performances because a systematic |
| 9207 | * Lua initialization cause 5% performances loss. |
| 9208 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9209 | 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] | 9210 | 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] | 9211 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9212 | return 0; |
| 9213 | } |
| 9214 | |
| 9215 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 9216 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9217 | |
| 9218 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9219 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9220 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9221 | error = lua_tostring(hlua->T, -1); |
| 9222 | else |
| 9223 | error = "critical error"; |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9224 | SEND_ERR(strm->be, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9225 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9226 | return 0; |
| 9227 | } |
| 9228 | |
| 9229 | /* Check stack available size. */ |
| 9230 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9231 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9232 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9233 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9234 | return 0; |
| 9235 | } |
| 9236 | |
| 9237 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9238 | 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] | 9239 | |
| 9240 | /* Create and and push object stream in the stack. */ |
| 9241 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9242 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9243 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9244 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9245 | return 0; |
| 9246 | } |
| 9247 | hlua->nargs = 1; |
| 9248 | |
| 9249 | /* push keywords in the stack. */ |
| 9250 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 9251 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9252 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9253 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9254 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9255 | return 0; |
| 9256 | } |
| 9257 | lua_pushstring(hlua->T, *arg); |
| 9258 | hlua->nargs++; |
| 9259 | } |
| 9260 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9261 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9262 | |
| 9263 | /* Wakeup the applet ASAP. */ |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 9264 | cs_cant_get(cs); |
| 9265 | cs_rx_endp_more(cs); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9266 | |
| 9267 | return 1; |
| 9268 | } |
| 9269 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 9270 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9271 | { |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 9272 | struct conn_stream *cs = ctx->owner; |
| 9273 | struct stream *strm = __cs_strm(cs); |
| 9274 | struct channel *res = cs_ic(cs); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9275 | struct act_rule *rule = ctx->rule; |
| 9276 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9277 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9278 | |
| 9279 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 9280 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 9281 | /* eat the whole request */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 9282 | co_skip(cs_oc(cs), co_data(cs_oc(cs))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9283 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 9284 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9285 | |
| 9286 | /* If the stream is disconnect or closed, ldo nothing. */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 9287 | if (unlikely(cs->state == CS_ST_DIS || cs->state == CS_ST_CLO)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9288 | return; |
| 9289 | |
| 9290 | /* Execute the function. */ |
| 9291 | switch (hlua_ctx_resume(hlua, 1)) { |
| 9292 | /* finished. */ |
| 9293 | case HLUA_E_OK: |
| 9294 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 9295 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9296 | /* eat the whole request */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 9297 | co_skip(cs_oc(cs), co_data(cs_oc(cs))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9298 | res->flags |= CF_READ_NULL; |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame] | 9299 | cs_shutr(cs); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9300 | return; |
| 9301 | |
| 9302 | /* yield. */ |
| 9303 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 9304 | if (hlua->wake_time != TICK_ETERNITY) |
| 9305 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9306 | return; |
| 9307 | |
| 9308 | /* finished with error. */ |
| 9309 | case HLUA_E_ERRMSG: |
| 9310 | /* Display log. */ |
| 9311 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9312 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9313 | lua_pop(hlua->T, 1); |
| 9314 | goto error; |
| 9315 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9316 | case HLUA_E_ETMOUT: |
| 9317 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9318 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9319 | goto error; |
| 9320 | |
| 9321 | case HLUA_E_NOMEM: |
| 9322 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9323 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9324 | goto error; |
| 9325 | |
| 9326 | case HLUA_E_YIELD: /* unexpected */ |
| 9327 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9328 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9329 | goto error; |
| 9330 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9331 | case HLUA_E_ERR: |
| 9332 | /* Display log. */ |
| 9333 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9334 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9335 | goto error; |
| 9336 | |
| 9337 | default: |
| 9338 | goto error; |
| 9339 | } |
| 9340 | |
| 9341 | error: |
| 9342 | |
| 9343 | /* For all other cases, just close the stream. */ |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame] | 9344 | cs_shutw(cs); |
| 9345 | cs_shutr(cs); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9346 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 9347 | } |
| 9348 | |
| 9349 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 9350 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 9351 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9352 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9353 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9354 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9355 | } |
| 9356 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9357 | /* The function returns 1 if the initialisation is complete, 0 if |
| 9358 | * an errors occurs and -1 if more data are required for initializing |
| 9359 | * the applet. |
| 9360 | */ |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9361 | static int hlua_applet_http_init(struct appctx *ctx) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9362 | { |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 9363 | struct conn_stream *cs = ctx->owner; |
| 9364 | struct stream *strm = __cs_strm(cs); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9365 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9366 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9367 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9368 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9369 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9370 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9371 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9372 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9373 | if (!hlua) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9374 | SEND_ERR(strm->be, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9375 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9376 | return 0; |
| 9377 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9378 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9379 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9380 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 9381 | ctx->ctx.hlua_apphttp.flags = 0; |
| 9382 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 9383 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 9384 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 9385 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9386 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 9387 | task = task_new_here(); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9388 | if (!task) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9389 | SEND_ERR(strm->be, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9390 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9391 | return 0; |
| 9392 | } |
| 9393 | task->nice = 0; |
| 9394 | task->context = ctx; |
| 9395 | task->process = hlua_applet_wakeup; |
| 9396 | ctx->ctx.hlua_apphttp.task = task; |
| 9397 | |
| 9398 | /* In the execution wrappers linked with a stream, the |
| 9399 | * Lua context can be not initialized. This behavior |
| 9400 | * permits to save performances because a systematic |
| 9401 | * Lua initialization cause 5% performances loss. |
| 9402 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9403 | 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] | 9404 | 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] | 9405 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9406 | return 0; |
| 9407 | } |
| 9408 | |
| 9409 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 9410 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9411 | |
| 9412 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9413 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9414 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9415 | error = lua_tostring(hlua->T, -1); |
| 9416 | else |
| 9417 | error = "critical error"; |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9418 | SEND_ERR(strm->be, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9419 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9420 | return 0; |
| 9421 | } |
| 9422 | |
| 9423 | /* Check stack available size. */ |
| 9424 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9425 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9426 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9427 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9428 | return 0; |
| 9429 | } |
| 9430 | |
| 9431 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9432 | 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] | 9433 | |
| 9434 | /* Create and and push object stream in the stack. */ |
| 9435 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9436 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9437 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9438 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9439 | return 0; |
| 9440 | } |
| 9441 | hlua->nargs = 1; |
| 9442 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9443 | /* push keywords in the stack. */ |
| 9444 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 9445 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9446 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9447 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9448 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9449 | return 0; |
| 9450 | } |
| 9451 | lua_pushstring(hlua->T, *arg); |
| 9452 | hlua->nargs++; |
| 9453 | } |
| 9454 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9455 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9456 | |
| 9457 | /* Wakeup the applet when data is ready for read. */ |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 9458 | cs_cant_get(cs); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9459 | |
| 9460 | return 1; |
| 9461 | } |
| 9462 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 9463 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9464 | { |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 9465 | struct conn_stream *cs = ctx->owner; |
| 9466 | struct stream *strm = __cs_strm(cs); |
| 9467 | struct channel *req = cs_oc(cs); |
| 9468 | struct channel *res = cs_ic(cs); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9469 | struct act_rule *rule = ctx->rule; |
| 9470 | struct proxy *px = strm->be; |
| 9471 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 9472 | struct htx *req_htx, *res_htx; |
| 9473 | |
| 9474 | res_htx = htx_from_buf(&res->buf); |
| 9475 | |
| 9476 | /* If the stream is disconnect or closed, ldo nothing. */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 9477 | if (unlikely(cs->state == CS_ST_DIS || cs->state == CS_ST_CLO)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9478 | goto out; |
| 9479 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9480 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9481 | if (!b_size(&res->buf)) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 9482 | cs_rx_room_blk(cs); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9483 | goto out; |
| 9484 | } |
| 9485 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9486 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9487 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 9488 | |
| 9489 | /* Set the currently running flag. */ |
| 9490 | if (!HLUA_IS_RUNNING(hlua) && |
| 9491 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 9492 | if (!co_data(req)) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 9493 | cs_cant_get(cs); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9494 | goto out; |
| 9495 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9496 | } |
| 9497 | |
| 9498 | /* Executes The applet if it is not done. */ |
| 9499 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 9500 | |
| 9501 | /* Execute the function. */ |
| 9502 | switch (hlua_ctx_resume(hlua, 1)) { |
| 9503 | /* finished. */ |
| 9504 | case HLUA_E_OK: |
| 9505 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 9506 | break; |
| 9507 | |
| 9508 | /* yield. */ |
| 9509 | case HLUA_E_AGAIN: |
| 9510 | if (hlua->wake_time != TICK_ETERNITY) |
| 9511 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9512 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9513 | |
| 9514 | /* finished with error. */ |
| 9515 | case HLUA_E_ERRMSG: |
| 9516 | /* Display log. */ |
| 9517 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9518 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9519 | lua_pop(hlua->T, 1); |
| 9520 | goto error; |
| 9521 | |
| 9522 | case HLUA_E_ETMOUT: |
| 9523 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9524 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9525 | goto error; |
| 9526 | |
| 9527 | case HLUA_E_NOMEM: |
| 9528 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9529 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9530 | goto error; |
| 9531 | |
| 9532 | case HLUA_E_YIELD: /* unexpected */ |
| 9533 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9534 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9535 | goto error; |
| 9536 | |
| 9537 | case HLUA_E_ERR: |
| 9538 | /* Display log. */ |
| 9539 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\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 | default: |
| 9544 | goto error; |
| 9545 | } |
| 9546 | } |
| 9547 | |
| 9548 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9549 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 9550 | goto done; |
| 9551 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9552 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 9553 | goto error; |
| 9554 | |
Christopher Faulet | f89af9c | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 9555 | /* no more data are expected. If the response buffer is empty |
| 9556 | * for a chunked message, be sure to add something (EOT block in |
| 9557 | * this case) to have something to send. It is important to be |
| 9558 | * sure the EOM flags will be handled by the endpoint. |
| 9559 | */ |
| 9560 | if (htx_is_empty(res_htx) && (strm->txn->rsp.flags & (HTTP_MSGF_XFER_LEN|HTTP_MSGF_CNT_LEN)) == HTTP_MSGF_XFER_LEN) { |
| 9561 | if (!htx_add_endof(res_htx, HTX_BLK_EOT)) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 9562 | cs_rx_room_blk(cs); |
Christopher Faulet | f89af9c | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 9563 | goto out; |
| 9564 | } |
| 9565 | channel_add_input(res, 1); |
| 9566 | } |
| 9567 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 9568 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 9569 | cs->endp->flags |= CS_EP_EOI; |
Christopher Faulet | d8d2708 | 2022-03-07 15:50:54 +0100 | [diff] [blame] | 9570 | res->flags |= CF_EOI; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9571 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 9572 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9573 | } |
| 9574 | |
| 9575 | done: |
| 9576 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9577 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9578 | res->flags |= CF_READ_NULL; |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame] | 9579 | cs_shutr(cs); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9580 | } |
| 9581 | |
| 9582 | /* eat the whole request */ |
| 9583 | if (co_data(req)) { |
| 9584 | req_htx = htx_from_buf(&req->buf); |
| 9585 | co_htx_skip(req, req_htx, co_data(req)); |
| 9586 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9587 | } |
| 9588 | } |
| 9589 | |
| 9590 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9591 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9592 | return; |
| 9593 | |
| 9594 | error: |
| 9595 | |
| 9596 | /* If we are in HTTP mode, and we are not send any |
| 9597 | * data, return a 500 server error in best effort: |
| 9598 | * if there is no room available in the buffer, |
| 9599 | * just close the connection. |
| 9600 | */ |
| 9601 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 9602 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9603 | |
| 9604 | channel_erase(res); |
| 9605 | res->buf.data = b_data(err); |
| 9606 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 9607 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 9608 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9609 | } |
| 9610 | if (!(strm->flags & SF_ERR_MASK)) |
| 9611 | strm->flags |= SF_ERR_RESOURCE; |
| 9612 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 9613 | goto done; |
| 9614 | } |
| 9615 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9616 | static void hlua_applet_http_release(struct appctx *ctx) |
| 9617 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 9618 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9619 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9620 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9621 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9622 | } |
| 9623 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9624 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9625 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9626 | * |
| 9627 | * This function can fail with an abort() due to an Lua critical error. |
| 9628 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 9629 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9630 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9631 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 9632 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9633 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9634 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9635 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9636 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9637 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9638 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9639 | if (!rule->arg.hlua_rule) { |
| 9640 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9641 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9642 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9643 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9644 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 9645 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 9646 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9647 | if (!rule->arg.hlua_rule->args) { |
| 9648 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9649 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9650 | } |
| 9651 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9652 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9653 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9654 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9655 | /* Expect some arguments */ |
| 9656 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9657 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9658 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9659 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9660 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9661 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9662 | if (!rule->arg.hlua_rule->args[i]) { |
| 9663 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9664 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9665 | } |
| 9666 | (*cur_arg)++; |
| 9667 | } |
| 9668 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9669 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 9670 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9671 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9672 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9673 | |
| 9674 | error: |
| 9675 | if (rule->arg.hlua_rule) { |
| 9676 | if (rule->arg.hlua_rule->args) { |
| 9677 | for (i = 0; i < fcn->nargs; i++) |
| 9678 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 9679 | ha_free(&rule->arg.hlua_rule->args); |
| 9680 | } |
| 9681 | ha_free(&rule->arg.hlua_rule); |
| 9682 | } |
| 9683 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9684 | } |
| 9685 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9686 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 9687 | struct act_rule *rule, char **err) |
| 9688 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9689 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9690 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9691 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9692 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9693 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9694 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9695 | * the call of this analyzer. |
| 9696 | */ |
| 9697 | if (rule->from != ACT_F_HTTP_REQ) { |
| 9698 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 9699 | return ACT_RET_PRS_ERR; |
| 9700 | } |
| 9701 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9702 | /* Memory for the rule. */ |
| 9703 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 9704 | if (!rule->arg.hlua_rule) { |
| 9705 | memprintf(err, "out of memory error"); |
| 9706 | return ACT_RET_PRS_ERR; |
| 9707 | } |
| 9708 | |
| 9709 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9710 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9711 | |
| 9712 | /* TODO: later accept arguments. */ |
| 9713 | rule->arg.hlua_rule->args = NULL; |
| 9714 | |
| 9715 | /* Add applet pointer in the rule. */ |
| 9716 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 9717 | rule->applet.name = fcn->name; |
| 9718 | rule->applet.init = hlua_applet_http_init; |
| 9719 | rule->applet.fct = hlua_applet_http_fct; |
| 9720 | rule->applet.release = hlua_applet_http_release; |
| 9721 | rule->applet.timeout = hlua_timeout_applet; |
| 9722 | |
| 9723 | return ACT_RET_PRS_OK; |
| 9724 | } |
| 9725 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9726 | /* This function is an LUA binding used for registering |
| 9727 | * "sample-conv" functions. It expects a converter name used |
| 9728 | * in the haproxy configuration file, and an LUA function. |
| 9729 | */ |
| 9730 | __LJMP static int hlua_register_action(lua_State *L) |
| 9731 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9732 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9733 | const char *name; |
| 9734 | int ref; |
| 9735 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9736 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9737 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9738 | struct buffer *trash; |
| 9739 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9740 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9741 | /* Initialise the number of expected arguments at 0. */ |
| 9742 | nargs = 0; |
| 9743 | |
| 9744 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 9745 | 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] | 9746 | |
| 9747 | /* First argument : converter name. */ |
| 9748 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9749 | |
| 9750 | /* Second argument : environment. */ |
| 9751 | if (lua_type(L, 2) != LUA_TTABLE) |
| 9752 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9753 | |
| 9754 | /* Third argument : lua function. */ |
| 9755 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 9756 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9757 | /* 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] | 9758 | if (lua_gettop(L) >= 4) |
| 9759 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 9760 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9761 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9762 | lua_pushnil(L); |
| 9763 | while (lua_next(L, 2) != 0) { |
| 9764 | if (lua_type(L, -1) != LUA_TSTRING) |
| 9765 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9766 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9767 | /* Check if action exists */ |
| 9768 | trash = get_trash_chunk(); |
| 9769 | chunk_printf(trash, "lua.%s", name); |
| 9770 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 9771 | akw = tcp_req_cont_action(trash->area); |
| 9772 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 9773 | akw = tcp_res_cont_action(trash->area); |
| 9774 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 9775 | akw = action_http_req_custom(trash->area); |
| 9776 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 9777 | akw = action_http_res_custom(trash->area); |
| 9778 | } else { |
| 9779 | akw = NULL; |
| 9780 | } |
| 9781 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9782 | fcn = akw->private; |
| 9783 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9784 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 9785 | "This will become a hard error in version 2.5.\n", name); |
| 9786 | } |
| 9787 | fcn->function_ref[hlua_state_id] = ref; |
| 9788 | |
| 9789 | /* pop the environment string. */ |
| 9790 | lua_pop(L, 1); |
| 9791 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9792 | } |
| 9793 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9794 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 9795 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9796 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9797 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9798 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9799 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9800 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9801 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9802 | |
| 9803 | /* Fill fcn. */ |
| 9804 | fcn->name = strdup(name); |
| 9805 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9806 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9807 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9808 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 9809 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9810 | fcn->nargs = nargs; |
| 9811 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9812 | /* List head */ |
| 9813 | akl->list.n = akl->list.p = NULL; |
| 9814 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9815 | /* action keyword. */ |
| 9816 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9817 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9818 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9819 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9820 | |
| 9821 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 9822 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 9823 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9824 | akl->kw[0].private = fcn; |
| 9825 | akl->kw[0].parse = action_register_lua; |
| 9826 | |
| 9827 | /* select the action registering point. */ |
| 9828 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 9829 | tcp_req_cont_keywords_register(akl); |
| 9830 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 9831 | tcp_res_cont_keywords_register(akl); |
| 9832 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 9833 | http_req_keywords_register(akl); |
| 9834 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 9835 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9836 | else { |
| 9837 | release_hlua_function(fcn); |
| 9838 | if (akl) |
| 9839 | ha_free((char **)&(akl->kw[0].kw)); |
| 9840 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 9841 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9842 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 9843 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9844 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9845 | |
| 9846 | /* pop the environment string. */ |
| 9847 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9848 | |
| 9849 | /* reset for next loop */ |
| 9850 | akl = NULL; |
| 9851 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9852 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9853 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9854 | |
| 9855 | alloc_error: |
| 9856 | release_hlua_function(fcn); |
| 9857 | ha_free(&akl); |
| 9858 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9859 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9860 | } |
| 9861 | |
| 9862 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 9863 | struct act_rule *rule, char **err) |
| 9864 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9865 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9866 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 9867 | if (px->mode == PR_MODE_HTTP) { |
| 9868 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 9869 | return ACT_RET_PRS_ERR; |
| 9870 | } |
| 9871 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9872 | /* Memory for the rule. */ |
| 9873 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 9874 | if (!rule->arg.hlua_rule) { |
| 9875 | memprintf(err, "out of memory error"); |
| 9876 | return ACT_RET_PRS_ERR; |
| 9877 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9878 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9879 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9880 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9881 | |
| 9882 | /* TODO: later accept arguments. */ |
| 9883 | rule->arg.hlua_rule->args = NULL; |
| 9884 | |
| 9885 | /* Add applet pointer in the rule. */ |
| 9886 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 9887 | rule->applet.name = fcn->name; |
| 9888 | rule->applet.init = hlua_applet_tcp_init; |
| 9889 | rule->applet.fct = hlua_applet_tcp_fct; |
| 9890 | rule->applet.release = hlua_applet_tcp_release; |
| 9891 | rule->applet.timeout = hlua_timeout_applet; |
| 9892 | |
| 9893 | return 0; |
| 9894 | } |
| 9895 | |
| 9896 | /* This function is an LUA binding used for registering |
| 9897 | * "sample-conv" functions. It expects a converter name used |
| 9898 | * in the haproxy configuration file, and an LUA function. |
| 9899 | */ |
| 9900 | __LJMP static int hlua_register_service(lua_State *L) |
| 9901 | { |
| 9902 | struct action_kw_list *akl; |
| 9903 | const char *name; |
| 9904 | const char *env; |
| 9905 | int ref; |
| 9906 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9907 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9908 | struct buffer *trash; |
| 9909 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9910 | |
| 9911 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 9912 | |
| 9913 | /* First argument : converter name. */ |
| 9914 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9915 | |
| 9916 | /* Second argument : environment. */ |
| 9917 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 9918 | |
| 9919 | /* Third argument : lua function. */ |
| 9920 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 9921 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9922 | /* Check for service already registered */ |
| 9923 | trash = get_trash_chunk(); |
| 9924 | chunk_printf(trash, "lua.%s", name); |
| 9925 | akw = service_find(trash->area); |
| 9926 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9927 | fcn = akw->private; |
| 9928 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9929 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 9930 | "This will become a hard error in version 2.5.\n", name); |
| 9931 | } |
| 9932 | fcn->function_ref[hlua_state_id] = ref; |
| 9933 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9934 | } |
| 9935 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9936 | /* Allocate and fill the sample fetch keyword struct. */ |
| 9937 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 9938 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9939 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9940 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9941 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9942 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9943 | |
| 9944 | /* Fill fcn. */ |
| 9945 | len = strlen("<lua.>") + strlen(name) + 1; |
| 9946 | fcn->name = calloc(1, len); |
| 9947 | if (!fcn->name) |
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 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9950 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9951 | |
| 9952 | /* List head */ |
| 9953 | akl->list.n = akl->list.p = NULL; |
| 9954 | |
| 9955 | /* converter keyword. */ |
| 9956 | len = strlen("lua.") + strlen(name) + 1; |
| 9957 | akl->kw[0].kw = calloc(1, len); |
| 9958 | if (!akl->kw[0].kw) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9959 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9960 | |
| 9961 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 9962 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 9963 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9964 | if (strcmp(env, "tcp") == 0) |
| 9965 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9966 | else if (strcmp(env, "http") == 0) |
| 9967 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9968 | else { |
| 9969 | release_hlua_function(fcn); |
| 9970 | if (akl) |
| 9971 | ha_free((char **)&(akl->kw[0].kw)); |
| 9972 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 9973 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 9974 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9975 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9976 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 9977 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9978 | akl->kw[0].private = fcn; |
| 9979 | |
| 9980 | /* End of array. */ |
| 9981 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 9982 | |
| 9983 | /* Register this new converter */ |
| 9984 | service_keywords_register(akl); |
| 9985 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9986 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9987 | |
| 9988 | alloc_error: |
| 9989 | release_hlua_function(fcn); |
| 9990 | ha_free(&akl); |
| 9991 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9992 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9993 | } |
| 9994 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9995 | /* This function initialises Lua cli handler. It copies the |
| 9996 | * arguments in the Lua stack and create channel IO objects. |
| 9997 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 9998 | 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] | 9999 | { |
| 10000 | struct hlua *hlua; |
| 10001 | struct hlua_function *fcn; |
| 10002 | int i; |
| 10003 | const char *error; |
| 10004 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10005 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10006 | appctx->ctx.hlua_cli.fcn = private; |
| 10007 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10008 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10009 | if (!hlua) { |
| 10010 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10011 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10012 | } |
| 10013 | HLUA_INIT(hlua); |
| 10014 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10015 | |
| 10016 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 10017 | * 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] | 10018 | * applet_http. It is absolutely compatible. |
| 10019 | */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 10020 | appctx->ctx.hlua_cli.task = task_new_here(); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10021 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 10022 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10023 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10024 | } |
| 10025 | appctx->ctx.hlua_cli.task->nice = 0; |
| 10026 | appctx->ctx.hlua_cli.task->context = appctx; |
| 10027 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 10028 | |
| 10029 | /* Initialises the Lua context */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10030 | if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(fcn), appctx->ctx.hlua_cli.task, 0)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10031 | 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] | 10032 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10033 | } |
| 10034 | |
| 10035 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10036 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10037 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 10038 | error = lua_tostring(hlua->T, -1); |
| 10039 | else |
| 10040 | error = "critical error"; |
| 10041 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 10042 | goto error; |
| 10043 | } |
| 10044 | |
| 10045 | /* Check stack available size. */ |
| 10046 | if (!lua_checkstack(hlua->T, 2)) { |
| 10047 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10048 | goto error; |
| 10049 | } |
| 10050 | |
| 10051 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10052 | 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] | 10053 | |
| 10054 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 10055 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10056 | */ |
| 10057 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 10058 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10059 | goto error; |
| 10060 | } |
| 10061 | hlua->nargs = 1; |
| 10062 | |
| 10063 | /* push keywords in the stack. */ |
| 10064 | for (i = 0; *args[i]; i++) { |
| 10065 | /* Check stack available size. */ |
| 10066 | if (!lua_checkstack(hlua->T, 1)) { |
| 10067 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10068 | goto error; |
| 10069 | } |
| 10070 | lua_pushstring(hlua->T, args[i]); |
| 10071 | hlua->nargs++; |
| 10072 | } |
| 10073 | |
| 10074 | /* We must initialize the execution timeouts. */ |
| 10075 | hlua->max_time = hlua_timeout_session; |
| 10076 | |
| 10077 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10078 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10079 | |
| 10080 | /* It's ok */ |
| 10081 | return 0; |
| 10082 | |
| 10083 | /* It's not ok. */ |
| 10084 | error: |
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 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10087 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10088 | return 1; |
| 10089 | } |
| 10090 | |
| 10091 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 10092 | { |
| 10093 | struct hlua *hlua; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 10094 | struct conn_stream *cs; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10095 | struct hlua_function *fcn; |
| 10096 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10097 | hlua = appctx->ctx.hlua_cli.hlua; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 10098 | cs = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 10099 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10100 | |
| 10101 | /* If the stream is disconnect or closed, ldo nothing. */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 10102 | 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] | 10103 | return 1; |
| 10104 | |
| 10105 | /* Execute the function. */ |
| 10106 | switch (hlua_ctx_resume(hlua, 1)) { |
| 10107 | |
| 10108 | /* finished. */ |
| 10109 | case HLUA_E_OK: |
| 10110 | return 1; |
| 10111 | |
| 10112 | /* yield. */ |
| 10113 | case HLUA_E_AGAIN: |
| 10114 | /* We want write. */ |
| 10115 | if (HLUA_IS_WAKERESWR(hlua)) |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 10116 | cs_rx_room_blk(cs); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10117 | /* Set the timeout. */ |
| 10118 | if (hlua->wake_time != TICK_ETERNITY) |
| 10119 | task_schedule(hlua->task, hlua->wake_time); |
| 10120 | return 0; |
| 10121 | |
| 10122 | /* finished with error. */ |
| 10123 | case HLUA_E_ERRMSG: |
| 10124 | /* Display log. */ |
| 10125 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 10126 | fcn->name, lua_tostring(hlua->T, -1)); |
| 10127 | lua_pop(hlua->T, 1); |
| 10128 | return 1; |
| 10129 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10130 | case HLUA_E_ETMOUT: |
| 10131 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 10132 | fcn->name); |
| 10133 | return 1; |
| 10134 | |
| 10135 | case HLUA_E_NOMEM: |
| 10136 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 10137 | fcn->name); |
| 10138 | return 1; |
| 10139 | |
| 10140 | case HLUA_E_YIELD: /* unexpected */ |
| 10141 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 10142 | fcn->name); |
| 10143 | return 1; |
| 10144 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10145 | case HLUA_E_ERR: |
| 10146 | /* Display log. */ |
| 10147 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 10148 | fcn->name); |
| 10149 | return 1; |
| 10150 | |
| 10151 | default: |
| 10152 | return 1; |
| 10153 | } |
| 10154 | |
| 10155 | return 1; |
| 10156 | } |
| 10157 | |
| 10158 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 10159 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10160 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10161 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10162 | } |
| 10163 | |
| 10164 | /* This function is an LUA binding used for registering |
| 10165 | * new keywords in the cli. It expects a list of keywords |
| 10166 | * which are the "path". It is limited to 5 keywords. A |
| 10167 | * description of the command, a function to be executed |
| 10168 | * for the parsing and a function for io handlers. |
| 10169 | */ |
| 10170 | __LJMP static int hlua_register_cli(lua_State *L) |
| 10171 | { |
| 10172 | struct cli_kw_list *cli_kws; |
| 10173 | const char *message; |
| 10174 | int ref_io; |
| 10175 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10176 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10177 | int index; |
| 10178 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10179 | struct buffer *trash; |
| 10180 | const char *kw[5]; |
| 10181 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10182 | const char *errmsg; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10183 | |
| 10184 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 10185 | |
| 10186 | /* First argument : an array of maximum 5 keywords. */ |
| 10187 | if (!lua_istable(L, 1)) |
| 10188 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 10189 | |
| 10190 | /* Second argument : string with contextual message. */ |
| 10191 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 10192 | |
| 10193 | /* Third and fourth argument : lua function. */ |
| 10194 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 10195 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10196 | /* Check for CLI service already registered */ |
| 10197 | trash = get_trash_chunk(); |
| 10198 | index = 0; |
| 10199 | lua_pushnil(L); |
| 10200 | memset(kw, 0, sizeof(kw)); |
| 10201 | while (lua_next(L, 1) != 0) { |
| 10202 | if (index >= CLI_PREFIX_KW_NB) |
| 10203 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 10204 | if (lua_type(L, -1) != LUA_TSTRING) |
| 10205 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 10206 | kw[index] = lua_tostring(L, -1); |
| 10207 | if (index == 0) |
| 10208 | chunk_printf(trash, "%s", kw[index]); |
| 10209 | else |
| 10210 | chunk_appendf(trash, " %s", kw[index]); |
| 10211 | index++; |
| 10212 | lua_pop(L, 1); |
| 10213 | } |
| 10214 | cli_kw = cli_find_kw_exact((char **)kw); |
| 10215 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10216 | fcn = cli_kw->private; |
| 10217 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 10218 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 10219 | "This will become a hard error in version 2.5.\n", trash->area); |
| 10220 | } |
| 10221 | fcn->function_ref[hlua_state_id] = ref_io; |
| 10222 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10223 | } |
| 10224 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10225 | /* Allocate and fill the sample fetch keyword struct. */ |
| 10226 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10227 | if (!cli_kws) { |
| 10228 | errmsg = "Lua out of memory error."; |
| 10229 | goto error; |
| 10230 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10231 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10232 | if (!fcn) { |
| 10233 | errmsg = "Lua out of memory error."; |
| 10234 | goto error; |
| 10235 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10236 | |
| 10237 | /* Fill path. */ |
| 10238 | index = 0; |
| 10239 | lua_pushnil(L); |
| 10240 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10241 | if (index >= 5) { |
| 10242 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 10243 | goto error; |
| 10244 | } |
| 10245 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 10246 | errmsg = "1st argument must be a table filled with strings"; |
| 10247 | goto error; |
| 10248 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10249 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10250 | if (!cli_kws->kw[0].str_kw[index]) { |
| 10251 | errmsg = "Lua out of memory error."; |
| 10252 | goto error; |
| 10253 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10254 | index++; |
| 10255 | lua_pop(L, 1); |
| 10256 | } |
| 10257 | |
| 10258 | /* Copy help message. */ |
| 10259 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10260 | if (!cli_kws->kw[0].usage) { |
| 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 | |
| 10265 | /* Fill fcn io handler. */ |
| 10266 | len = strlen("<lua.cli>") + 1; |
| 10267 | for (i = 0; i < index; i++) |
| 10268 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 10269 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10270 | if (!fcn->name) { |
| 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 | strncat((char *)fcn->name, "<lua.cli", len); |
| 10275 | for (i = 0; i < index; i++) { |
| 10276 | strncat((char *)fcn->name, ".", len); |
| 10277 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 10278 | } |
| 10279 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10280 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10281 | |
| 10282 | /* Fill last entries. */ |
| 10283 | cli_kws->kw[0].private = fcn; |
| 10284 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 10285 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 10286 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 10287 | |
| 10288 | /* Register this new converter */ |
| 10289 | cli_register_kw(cli_kws); |
| 10290 | |
| 10291 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10292 | |
| 10293 | error: |
| 10294 | release_hlua_function(fcn); |
| 10295 | if (cli_kws) { |
| 10296 | for (i = 0; i < index; i++) |
| 10297 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 10298 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 10299 | } |
| 10300 | ha_free(&cli_kws); |
| 10301 | WILL_LJMP(luaL_error(L, errmsg)); |
| 10302 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10303 | } |
| 10304 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10305 | static int hlua_filter_init_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 10306 | { |
| 10307 | struct hlua_flt_config *conf = fconf->conf; |
| 10308 | lua_State *L; |
| 10309 | int error, pos, state_id, flt_ref; |
| 10310 | |
| 10311 | state_id = reg_flt_to_stack_id(conf->reg); |
| 10312 | L = hlua_states[state_id]; |
| 10313 | pos = lua_gettop(L); |
| 10314 | |
| 10315 | /* The filter parsing function */ |
| 10316 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->fun_ref[state_id]); |
| 10317 | |
| 10318 | /* Push the filter class on the stack and resolve all callbacks */ |
| 10319 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->flt_ref[state_id]); |
| 10320 | |
| 10321 | /* Duplicate the filter class so each filter will have its own copy */ |
| 10322 | lua_newtable(L); |
| 10323 | lua_pushnil(L); |
| 10324 | |
| 10325 | while (lua_next(L, pos+2)) { |
| 10326 | lua_pushvalue(L, -2); |
| 10327 | lua_insert(L, -2); |
| 10328 | lua_settable(L, -4); |
| 10329 | } |
| 10330 | flt_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
| 10331 | |
| 10332 | /* Remove the original lua filter class from the stack */ |
| 10333 | lua_pop(L, 1); |
| 10334 | |
| 10335 | /* Push the copy on the stack */ |
| 10336 | lua_rawgeti(L, LUA_REGISTRYINDEX, flt_ref); |
| 10337 | |
| 10338 | /* extra args are pushed in a table */ |
| 10339 | lua_newtable(L); |
| 10340 | for (pos = 0; conf->args[pos]; pos++) { |
| 10341 | /* Check stack available size. */ |
| 10342 | if (!lua_checkstack(L, 1)) { |
| 10343 | ha_alert("Lua filter '%s' : Lua error : full stack.", conf->reg->name); |
| 10344 | goto error; |
| 10345 | } |
| 10346 | lua_pushstring(L, conf->args[pos]); |
| 10347 | lua_rawseti(L, -2, lua_rawlen(L, -2) + 1); |
| 10348 | } |
| 10349 | |
| 10350 | error = lua_pcall(L, 2, LUA_MULTRET, 0); |
| 10351 | switch (error) { |
| 10352 | case LUA_OK: |
| 10353 | /* replace the filter ref */ |
| 10354 | conf->ref[state_id] = flt_ref; |
| 10355 | break; |
| 10356 | case LUA_ERRRUN: |
| 10357 | ha_alert("Lua filter '%s' : runtime error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10358 | goto error; |
| 10359 | case LUA_ERRMEM: |
| 10360 | ha_alert("Lua filter '%s' : out of memory error", conf->reg->name); |
| 10361 | goto error; |
| 10362 | case LUA_ERRERR: |
| 10363 | ha_alert("Lua filter '%s' : message handler error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10364 | goto error; |
| 10365 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
| 10366 | case LUA_ERRGCMM: |
| 10367 | ha_alert("Lua filter '%s' : garbage collector error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10368 | goto error; |
| 10369 | #endif |
| 10370 | default: |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 10371 | 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] | 10372 | goto error; |
| 10373 | } |
| 10374 | |
| 10375 | lua_settop(L, 0); |
| 10376 | return 0; |
| 10377 | |
| 10378 | error: |
| 10379 | lua_settop(L, 0); |
| 10380 | return -1; |
| 10381 | } |
| 10382 | |
| 10383 | static void hlua_filter_deinit_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 10384 | { |
| 10385 | struct hlua_flt_config *conf = fconf->conf; |
| 10386 | lua_State *L; |
| 10387 | int state_id; |
| 10388 | |
| 10389 | if (!conf) |
| 10390 | return; |
| 10391 | |
| 10392 | state_id = reg_flt_to_stack_id(conf->reg); |
| 10393 | L = hlua_states[state_id]; |
| 10394 | luaL_unref(L, LUA_REGISTRYINDEX, conf->ref[state_id]); |
| 10395 | } |
| 10396 | |
| 10397 | static int hlua_filter_init(struct proxy *px, struct flt_conf *fconf) |
| 10398 | { |
| 10399 | struct hlua_flt_config *conf = fconf->conf; |
| 10400 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 10401 | |
| 10402 | /* Rely on per-thread init for global scripts */ |
| 10403 | if (!state_id) |
| 10404 | return hlua_filter_init_per_thread(px, fconf); |
| 10405 | return 0; |
| 10406 | } |
| 10407 | |
| 10408 | static void hlua_filter_deinit(struct proxy *px, struct flt_conf *fconf) |
| 10409 | { |
| 10410 | |
| 10411 | if (fconf->conf) { |
| 10412 | struct hlua_flt_config *conf = fconf->conf; |
| 10413 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 10414 | int pos; |
| 10415 | |
| 10416 | /* Rely on per-thread deinit for global scripts */ |
| 10417 | if (!state_id) |
| 10418 | hlua_filter_deinit_per_thread(px, fconf); |
| 10419 | |
| 10420 | for (pos = 0; conf->args[pos]; pos++) |
| 10421 | free(conf->args[pos]); |
| 10422 | free(conf->args); |
| 10423 | } |
| 10424 | ha_free(&fconf->conf); |
| 10425 | ha_free((char **)&fconf->id); |
| 10426 | ha_free(&fconf->ops); |
| 10427 | } |
| 10428 | |
| 10429 | static int hlua_filter_new(struct stream *s, struct filter *filter) |
| 10430 | { |
| 10431 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 10432 | struct hlua_flt_ctx *flt_ctx = NULL; |
| 10433 | int ret = 1; |
| 10434 | |
| 10435 | /* In the execution wrappers linked with a stream, the |
| 10436 | * Lua context can be not initialized. This behavior |
| 10437 | * permits to save performances because a systematic |
| 10438 | * Lua initialization cause 5% performances loss. |
| 10439 | */ |
| 10440 | if (!s->hlua) { |
| 10441 | struct hlua *hlua; |
| 10442 | |
| 10443 | hlua = pool_alloc(pool_head_hlua); |
| 10444 | if (!hlua) { |
| 10445 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 10446 | conf->reg->name); |
| 10447 | ret = 0; |
| 10448 | goto end; |
| 10449 | } |
| 10450 | HLUA_INIT(hlua); |
| 10451 | s->hlua = hlua; |
| 10452 | if (!hlua_ctx_init(s->hlua, reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 10453 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 10454 | conf->reg->name); |
| 10455 | ret = 0; |
| 10456 | goto end; |
| 10457 | } |
| 10458 | } |
| 10459 | |
| 10460 | flt_ctx = pool_zalloc(pool_head_hlua_flt_ctx); |
| 10461 | if (!flt_ctx) { |
| 10462 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10463 | conf->reg->name); |
| 10464 | ret = 0; |
| 10465 | goto end; |
| 10466 | } |
| 10467 | flt_ctx->hlua[0] = pool_alloc(pool_head_hlua); |
| 10468 | flt_ctx->hlua[1] = pool_alloc(pool_head_hlua); |
| 10469 | if (!flt_ctx->hlua[0] || !flt_ctx->hlua[1]) { |
| 10470 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10471 | conf->reg->name); |
| 10472 | ret = 0; |
| 10473 | goto end; |
| 10474 | } |
| 10475 | HLUA_INIT(flt_ctx->hlua[0]); |
| 10476 | HLUA_INIT(flt_ctx->hlua[1]); |
| 10477 | if (!hlua_ctx_init(flt_ctx->hlua[0], reg_flt_to_stack_id(conf->reg), s->task, 0) || |
| 10478 | !hlua_ctx_init(flt_ctx->hlua[1], reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 10479 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10480 | conf->reg->name); |
| 10481 | ret = 0; |
| 10482 | goto end; |
| 10483 | } |
| 10484 | |
| 10485 | if (!HLUA_IS_RUNNING(s->hlua)) { |
| 10486 | /* The following Lua calls can fail. */ |
| 10487 | if (!SET_SAFE_LJMP(s->hlua)) { |
| 10488 | const char *error; |
| 10489 | |
| 10490 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 10491 | error = lua_tostring(s->hlua->T, -1); |
| 10492 | else |
| 10493 | error = "critical error"; |
| 10494 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 10495 | ret = 0; |
| 10496 | goto end; |
| 10497 | } |
| 10498 | |
| 10499 | /* Check stack size. */ |
| 10500 | if (!lua_checkstack(s->hlua->T, 1)) { |
| 10501 | 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] | 10502 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10503 | ret = 0; |
| 10504 | goto end; |
| 10505 | } |
| 10506 | |
| 10507 | lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, conf->ref[s->hlua->state_id]); |
| 10508 | if (lua_getfield(s->hlua->T, -1, "new") != LUA_TFUNCTION) { |
| 10509 | SEND_ERR(s->be, "Lua filter '%s': 'new' field is not a function.\n", |
| 10510 | conf->reg->name); |
| 10511 | RESET_SAFE_LJMP(s->hlua); |
| 10512 | ret = 0; |
| 10513 | goto end; |
| 10514 | } |
| 10515 | lua_insert(s->hlua->T, -2); |
| 10516 | |
| 10517 | /* Push the copy on the stack */ |
| 10518 | s->hlua->nargs = 1; |
| 10519 | |
| 10520 | /* We must initialize the execution timeouts. */ |
| 10521 | s->hlua->max_time = hlua_timeout_session; |
| 10522 | |
| 10523 | /* At this point the execution is safe. */ |
| 10524 | RESET_SAFE_LJMP(s->hlua); |
| 10525 | } |
| 10526 | |
| 10527 | switch (hlua_ctx_resume(s->hlua, 0)) { |
| 10528 | case HLUA_E_OK: |
| 10529 | /* Nothing returned or not a table, ignore the filter for current stream */ |
| 10530 | if (!lua_gettop(s->hlua->T) || !lua_istable(s->hlua->T, 1)) { |
| 10531 | ret = 0; |
| 10532 | goto end; |
| 10533 | } |
| 10534 | |
| 10535 | /* Attached the filter pointer to the ctx */ |
| 10536 | lua_pushstring(s->hlua->T, "__filter"); |
| 10537 | lua_pushlightuserdata(s->hlua->T, filter); |
| 10538 | lua_settable(s->hlua->T, -3); |
| 10539 | |
| 10540 | /* Save a ref on the filter ctx */ |
| 10541 | lua_pushvalue(s->hlua->T, 1); |
| 10542 | flt_ctx->ref = luaL_ref(s->hlua->T, LUA_REGISTRYINDEX); |
| 10543 | filter->ctx = flt_ctx; |
| 10544 | break; |
| 10545 | case HLUA_E_ERRMSG: |
| 10546 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(s->hlua->T, -1)); |
| 10547 | ret = -1; |
| 10548 | goto end; |
| 10549 | case HLUA_E_ETMOUT: |
| 10550 | SEND_ERR(s->be, "Lua filter '%s' : 'new' execution timeout.\n", conf->reg->name); |
| 10551 | ret = 0; |
| 10552 | goto end; |
| 10553 | case HLUA_E_NOMEM: |
| 10554 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10555 | ret = 0; |
| 10556 | goto end; |
| 10557 | case HLUA_E_AGAIN: |
| 10558 | case HLUA_E_YIELD: |
| 10559 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10560 | " are not allowed from 'new' function.\n", conf->reg->name); |
| 10561 | ret = 0; |
| 10562 | goto end; |
| 10563 | case HLUA_E_ERR: |
| 10564 | SEND_ERR(s->be, "Lua filter '%s': 'new' returns an unknown error.\n", conf->reg->name); |
| 10565 | ret = 0; |
| 10566 | goto end; |
| 10567 | default: |
| 10568 | ret = 0; |
| 10569 | goto end; |
| 10570 | } |
| 10571 | |
| 10572 | end: |
| 10573 | if (s->hlua) |
| 10574 | lua_settop(s->hlua->T, 0); |
| 10575 | if (ret <= 0) { |
| 10576 | if (flt_ctx) { |
| 10577 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10578 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10579 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10580 | } |
| 10581 | } |
| 10582 | return ret; |
| 10583 | } |
| 10584 | |
| 10585 | static void hlua_filter_delete(struct stream *s, struct filter *filter) |
| 10586 | { |
| 10587 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10588 | |
| 10589 | luaL_unref(s->hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 10590 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10591 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10592 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10593 | filter->ctx = NULL; |
| 10594 | } |
| 10595 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 10596 | static int hlua_filter_from_payload(struct filter *filter) |
| 10597 | { |
| 10598 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10599 | |
| 10600 | return (flt_ctx && !!(flt_ctx->flags & HLUA_FLT_CTX_FL_PAYLOAD)); |
| 10601 | } |
| 10602 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10603 | static int hlua_filter_callback(struct stream *s, struct filter *filter, const char *fun, |
| 10604 | int dir, unsigned int flags) |
| 10605 | { |
| 10606 | struct hlua *flt_hlua; |
| 10607 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 10608 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10609 | unsigned int hflags = HLUA_TXN_FLT_CTX; |
| 10610 | int ret = 1; |
| 10611 | |
| 10612 | flt_hlua = flt_ctx->hlua[(dir == SMP_OPT_DIR_REQ ? 0 : 1)]; |
| 10613 | if (!flt_hlua) |
| 10614 | goto end; |
| 10615 | |
| 10616 | if (!HLUA_IS_RUNNING(flt_hlua)) { |
| 10617 | int extra_idx = lua_gettop(flt_hlua->T); |
| 10618 | |
| 10619 | /* The following Lua calls can fail. */ |
| 10620 | if (!SET_SAFE_LJMP(flt_hlua)) { |
| 10621 | const char *error; |
| 10622 | |
| 10623 | if (lua_type(flt_hlua->T, -1) == LUA_TSTRING) |
| 10624 | error = lua_tostring(flt_hlua->T, -1); |
| 10625 | else |
| 10626 | error = "critical error"; |
| 10627 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 10628 | goto end; |
| 10629 | } |
| 10630 | |
| 10631 | /* Check stack size. */ |
| 10632 | if (!lua_checkstack(flt_hlua->T, 3)) { |
| 10633 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10634 | RESET_SAFE_LJMP(flt_hlua); |
| 10635 | goto end; |
| 10636 | } |
| 10637 | |
| 10638 | lua_rawgeti(flt_hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 10639 | if (lua_getfield(flt_hlua->T, -1, fun) != LUA_TFUNCTION) { |
| 10640 | RESET_SAFE_LJMP(flt_hlua); |
| 10641 | goto end; |
| 10642 | } |
| 10643 | lua_insert(flt_hlua->T, -2); |
| 10644 | |
| 10645 | if (!hlua_txn_new(flt_hlua->T, s, s->be, dir, hflags)) { |
| 10646 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10647 | RESET_SAFE_LJMP(flt_hlua); |
| 10648 | goto end; |
| 10649 | } |
| 10650 | flt_hlua->nargs = 2; |
| 10651 | |
| 10652 | if (flags & HLUA_FLT_CB_ARG_CHN) { |
| 10653 | if (dir == SMP_OPT_DIR_REQ) |
| 10654 | lua_getfield(flt_hlua->T, -1, "req"); |
| 10655 | else |
| 10656 | lua_getfield(flt_hlua->T, -1, "res"); |
| 10657 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10658 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10659 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10660 | lua_settable(flt_hlua->T, -3); |
| 10661 | } |
| 10662 | flt_hlua->nargs++; |
| 10663 | } |
| 10664 | else if (flags & HLUA_FLT_CB_ARG_HTTP_MSG) { |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10665 | if (dir == SMP_OPT_DIR_REQ) |
| 10666 | lua_getfield(flt_hlua->T, -1, "http_req"); |
| 10667 | else |
| 10668 | lua_getfield(flt_hlua->T, -1, "http_res"); |
| 10669 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10670 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10671 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10672 | lua_settable(flt_hlua->T, -3); |
| 10673 | } |
| 10674 | flt_hlua->nargs++; |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10675 | } |
| 10676 | |
| 10677 | /* Check stack size. */ |
| 10678 | if (!lua_checkstack(flt_hlua->T, 1)) { |
| 10679 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10680 | RESET_SAFE_LJMP(flt_hlua); |
| 10681 | goto end; |
| 10682 | } |
| 10683 | |
| 10684 | while (extra_idx--) { |
| 10685 | lua_pushvalue(flt_hlua->T, 1); |
| 10686 | lua_remove(flt_hlua->T, 1); |
| 10687 | flt_hlua->nargs++; |
| 10688 | } |
| 10689 | |
| 10690 | /* We must initialize the execution timeouts. */ |
| 10691 | flt_hlua->max_time = hlua_timeout_session; |
| 10692 | |
| 10693 | /* At this point the execution is safe. */ |
| 10694 | RESET_SAFE_LJMP(flt_hlua); |
| 10695 | } |
| 10696 | |
| 10697 | switch (hlua_ctx_resume(flt_hlua, !(flags & HLUA_FLT_CB_FINAL))) { |
| 10698 | case HLUA_E_OK: |
| 10699 | /* Catch the return value if it required */ |
| 10700 | if ((flags & HLUA_FLT_CB_RETVAL) && lua_gettop(flt_hlua->T) > 0) { |
| 10701 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10702 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10703 | } |
| 10704 | |
| 10705 | /* Set timeout in the required channel. */ |
| 10706 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10707 | if (dir == SMP_OPT_DIR_REQ) |
| 10708 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10709 | else |
| 10710 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10711 | } |
| 10712 | break; |
| 10713 | case HLUA_E_AGAIN: |
| 10714 | /* Set timeout in the required channel. */ |
| 10715 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10716 | if (dir == SMP_OPT_DIR_REQ) |
| 10717 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10718 | else |
| 10719 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10720 | } |
| 10721 | /* Some actions can be wake up when a "write" event |
| 10722 | * is detected on a response channel. This is useful |
| 10723 | * only for actions targeted on the requests. |
| 10724 | */ |
| 10725 | if (HLUA_IS_WAKERESWR(flt_hlua)) |
| 10726 | s->res.flags |= CF_WAKE_WRITE; |
| 10727 | if (HLUA_IS_WAKEREQWR(flt_hlua)) |
| 10728 | s->req.flags |= CF_WAKE_WRITE; |
| 10729 | ret = 0; |
| 10730 | goto end; |
| 10731 | case HLUA_E_ERRMSG: |
| 10732 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(flt_hlua->T, -1)); |
| 10733 | ret = -1; |
| 10734 | goto end; |
| 10735 | case HLUA_E_ETMOUT: |
| 10736 | SEND_ERR(s->be, "Lua filter '%s' : '%s' callback execution timeout.\n", conf->reg->name, fun); |
| 10737 | goto end; |
| 10738 | case HLUA_E_NOMEM: |
| 10739 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10740 | goto end; |
| 10741 | case HLUA_E_YIELD: |
| 10742 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10743 | " are not allowed from '%s' callback.\n", conf->reg->name, fun); |
| 10744 | goto end; |
| 10745 | case HLUA_E_ERR: |
| 10746 | SEND_ERR(s->be, "Lua filter '%s': '%s' returns an unknown error.\n", conf->reg->name, fun); |
| 10747 | goto end; |
| 10748 | default: |
| 10749 | goto end; |
| 10750 | } |
| 10751 | |
| 10752 | |
| 10753 | end: |
| 10754 | return ret; |
| 10755 | } |
| 10756 | |
| 10757 | static int hlua_filter_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 10758 | { |
| 10759 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10760 | |
| 10761 | flt_ctx->flags = 0; |
| 10762 | return hlua_filter_callback(s, filter, "start_analyze", |
| 10763 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10764 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 10765 | } |
| 10766 | |
| 10767 | static int hlua_filter_end_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 &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10772 | return hlua_filter_callback(s, filter, "end_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 | |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10777 | static int hlua_filter_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 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, "http_headers", |
| 10783 | (!(msg->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_HTTP_MSG)); |
| 10785 | } |
| 10786 | |
| 10787 | static int hlua_filter_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg, |
| 10788 | unsigned int offset, unsigned int len) |
| 10789 | { |
| 10790 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10791 | struct hlua *flt_hlua; |
| 10792 | int dir = (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 10793 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 10794 | int ret; |
| 10795 | |
| 10796 | flt_hlua = flt_ctx->hlua[idx]; |
| 10797 | flt_ctx->cur_off[idx] = offset; |
| 10798 | flt_ctx->cur_len[idx] = len; |
| 10799 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 10800 | ret = hlua_filter_callback(s, filter, "http_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10801 | if (ret != -1) { |
| 10802 | ret = flt_ctx->cur_len[idx]; |
| 10803 | if (lua_gettop(flt_hlua->T) > 0) { |
| 10804 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10805 | if (ret > flt_ctx->cur_len[idx]) |
| 10806 | ret = flt_ctx->cur_len[idx]; |
| 10807 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10808 | } |
| 10809 | } |
| 10810 | return ret; |
| 10811 | } |
| 10812 | |
| 10813 | static int hlua_filter_http_end(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 10814 | { |
| 10815 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10816 | |
| 10817 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10818 | return hlua_filter_callback(s, filter, "http_end", |
| 10819 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10820 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10821 | } |
| 10822 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10823 | static int hlua_filter_tcp_payload(struct stream *s, struct filter *filter, struct channel *chn, |
| 10824 | unsigned int offset, unsigned int len) |
| 10825 | { |
| 10826 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10827 | struct hlua *flt_hlua; |
| 10828 | int dir = (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 10829 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 10830 | int ret; |
| 10831 | |
| 10832 | flt_hlua = flt_ctx->hlua[idx]; |
| 10833 | flt_ctx->cur_off[idx] = offset; |
| 10834 | flt_ctx->cur_len[idx] = len; |
| 10835 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 10836 | ret = hlua_filter_callback(s, filter, "tcp_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_CHN)); |
| 10837 | if (ret != -1) { |
| 10838 | ret = flt_ctx->cur_len[idx]; |
| 10839 | if (lua_gettop(flt_hlua->T) > 0) { |
| 10840 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10841 | if (ret > flt_ctx->cur_len[idx]) |
| 10842 | ret = flt_ctx->cur_len[idx]; |
| 10843 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10844 | } |
| 10845 | } |
| 10846 | return ret; |
| 10847 | } |
| 10848 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10849 | static int hlua_filter_parse_fct(char **args, int *cur_arg, struct proxy *px, |
| 10850 | struct flt_conf *fconf, char **err, void *private) |
| 10851 | { |
| 10852 | struct hlua_reg_filter *reg_flt = private; |
| 10853 | lua_State *L; |
| 10854 | struct hlua_flt_config *conf = NULL; |
| 10855 | const char *flt_id = NULL; |
| 10856 | int state_id, pos, flt_flags = 0; |
| 10857 | struct flt_ops *hlua_flt_ops = NULL; |
| 10858 | |
| 10859 | state_id = reg_flt_to_stack_id(reg_flt); |
| 10860 | L = hlua_states[state_id]; |
| 10861 | |
| 10862 | /* Initialize the filter ops with default callbacks */ |
| 10863 | hlua_flt_ops = calloc(1, sizeof(*hlua_flt_ops)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10864 | if (!hlua_flt_ops) |
| 10865 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10866 | hlua_flt_ops->init = hlua_filter_init; |
| 10867 | hlua_flt_ops->deinit = hlua_filter_deinit; |
| 10868 | if (state_id) { |
| 10869 | /* Set per-thread callback if script is loaded per-thread */ |
| 10870 | hlua_flt_ops->init_per_thread = hlua_filter_init_per_thread; |
| 10871 | hlua_flt_ops->deinit_per_thread = hlua_filter_deinit_per_thread; |
| 10872 | } |
| 10873 | hlua_flt_ops->attach = hlua_filter_new; |
| 10874 | hlua_flt_ops->detach = hlua_filter_delete; |
| 10875 | |
| 10876 | /* Push the filter class on the stack and resolve all callbacks */ |
| 10877 | lua_rawgeti(L, LUA_REGISTRYINDEX, reg_flt->flt_ref[state_id]); |
| 10878 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10879 | if (lua_getfield(L, -1, "start_analyze") == LUA_TFUNCTION) |
| 10880 | hlua_flt_ops->channel_start_analyze = hlua_filter_start_analyze; |
| 10881 | lua_pop(L, 1); |
| 10882 | if (lua_getfield(L, -1, "end_analyze") == LUA_TFUNCTION) |
| 10883 | hlua_flt_ops->channel_end_analyze = hlua_filter_end_analyze; |
| 10884 | lua_pop(L, 1); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10885 | if (lua_getfield(L, -1, "http_headers") == LUA_TFUNCTION) |
| 10886 | hlua_flt_ops->http_headers = hlua_filter_http_headers; |
| 10887 | lua_pop(L, 1); |
| 10888 | if (lua_getfield(L, -1, "http_payload") == LUA_TFUNCTION) |
| 10889 | hlua_flt_ops->http_payload = hlua_filter_http_payload; |
| 10890 | lua_pop(L, 1); |
| 10891 | if (lua_getfield(L, -1, "http_end") == LUA_TFUNCTION) |
| 10892 | hlua_flt_ops->http_end = hlua_filter_http_end; |
| 10893 | lua_pop(L, 1); |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10894 | if (lua_getfield(L, -1, "tcp_payload") == LUA_TFUNCTION) |
| 10895 | hlua_flt_ops->tcp_payload = hlua_filter_tcp_payload; |
| 10896 | lua_pop(L, 1); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10897 | |
| 10898 | /* Get id and flags of the filter class */ |
| 10899 | if (lua_getfield(L, -1, "id") == LUA_TSTRING) |
| 10900 | flt_id = lua_tostring(L, -1); |
| 10901 | lua_pop(L, 1); |
| 10902 | if (lua_getfield(L, -1, "flags") == LUA_TNUMBER) |
| 10903 | flt_flags = lua_tointeger(L, -1); |
| 10904 | lua_pop(L, 1); |
| 10905 | |
| 10906 | /* Create the filter config */ |
| 10907 | conf = calloc(1, sizeof(*conf)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10908 | if (!conf) |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10909 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10910 | conf->reg = reg_flt; |
| 10911 | |
| 10912 | /* duplicate args */ |
| 10913 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++); |
| 10914 | conf->args = calloc(pos + 1, sizeof(*conf->args)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10915 | if (!conf->args) |
| 10916 | goto error; |
| 10917 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++) { |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10918 | conf->args[pos] = strdup(args[*cur_arg + 1 + pos]); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10919 | if (!conf->args[pos]) |
| 10920 | goto error; |
| 10921 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10922 | conf->args[pos] = NULL; |
| 10923 | *cur_arg += pos + 1; |
| 10924 | |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10925 | if (flt_id) { |
| 10926 | fconf->id = strdup(flt_id); |
| 10927 | if (!fconf->id) |
| 10928 | goto error; |
| 10929 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10930 | fconf->flags = flt_flags; |
| 10931 | fconf->conf = conf; |
| 10932 | fconf->ops = hlua_flt_ops; |
| 10933 | |
| 10934 | lua_settop(L, 0); |
| 10935 | return 0; |
| 10936 | |
| 10937 | error: |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10938 | 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] | 10939 | free(hlua_flt_ops); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10940 | if (conf && conf->args) { |
| 10941 | for (pos = 0; conf->args[pos]; pos++) |
| 10942 | free(conf->args[pos]); |
| 10943 | free(conf->args); |
| 10944 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10945 | free(conf); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10946 | free((char *)fconf->id); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10947 | lua_settop(L, 0); |
| 10948 | return -1; |
| 10949 | } |
| 10950 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10951 | __LJMP static int hlua_register_data_filter(lua_State *L) |
| 10952 | { |
| 10953 | struct filter *filter; |
| 10954 | struct channel *chn; |
| 10955 | |
| 10956 | MAY_LJMP(check_args(L, 2, "register_data_filter")); |
| 10957 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 10958 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 10959 | |
| 10960 | lua_getfield(L, 1, "__filter"); |
| 10961 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 10962 | filter = lua_touserdata (L, -1); |
| 10963 | lua_pop(L, 1); |
| 10964 | |
| 10965 | register_data_filter(chn_strm(chn), chn, filter); |
| 10966 | return 1; |
| 10967 | } |
| 10968 | |
| 10969 | __LJMP static int hlua_unregister_data_filter(lua_State *L) |
| 10970 | { |
| 10971 | struct filter *filter; |
| 10972 | struct channel *chn; |
| 10973 | |
| 10974 | MAY_LJMP(check_args(L, 2, "unregister_data_filter")); |
| 10975 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 10976 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 10977 | |
| 10978 | lua_getfield(L, 1, "__filter"); |
| 10979 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 10980 | filter = lua_touserdata (L, -1); |
| 10981 | lua_pop(L, 1); |
| 10982 | |
| 10983 | unregister_data_filter(chn_strm(chn), chn, filter); |
| 10984 | return 1; |
| 10985 | } |
| 10986 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10987 | /* 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] | 10988 | * 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] | 10989 | * parse configuration arguments. |
| 10990 | */ |
| 10991 | __LJMP static int hlua_register_filter(lua_State *L) |
| 10992 | { |
| 10993 | struct buffer *trash; |
| 10994 | struct flt_kw_list *fkl; |
| 10995 | struct flt_kw *fkw; |
| 10996 | const char *name; |
| 10997 | struct hlua_reg_filter *reg_flt= NULL; |
| 10998 | int flt_ref, fun_ref; |
| 10999 | int len; |
| 11000 | |
| 11001 | MAY_LJMP(check_args(L, 3, "register_filter")); |
| 11002 | |
| 11003 | /* First argument : filter name. */ |
| 11004 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 11005 | |
| 11006 | /* Second argument : The filter class */ |
| 11007 | flt_ref = MAY_LJMP(hlua_checktable(L, 2)); |
| 11008 | |
| 11009 | /* Third argument : lua function. */ |
| 11010 | fun_ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 11011 | |
| 11012 | trash = get_trash_chunk(); |
| 11013 | chunk_printf(trash, "lua.%s", name); |
| 11014 | fkw = flt_find_kw(trash->area); |
| 11015 | if (fkw != NULL) { |
| 11016 | reg_flt = fkw->private; |
| 11017 | if (reg_flt->flt_ref[hlua_state_id] != -1 || reg_flt->fun_ref[hlua_state_id] != -1) { |
| 11018 | ha_warning("Trying to register filter 'lua.%s' more than once. " |
| 11019 | "This will become a hard error in version 2.5.\n", name); |
| 11020 | } |
| 11021 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 11022 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 11023 | return 0; |
| 11024 | } |
| 11025 | |
| 11026 | fkl = calloc(1, sizeof(*fkl) + sizeof(struct flt_kw) * 2); |
| 11027 | if (!fkl) |
| 11028 | goto alloc_error; |
| 11029 | fkl->scope = "HLUA"; |
| 11030 | |
| 11031 | reg_flt = new_hlua_reg_filter(name); |
| 11032 | if (!reg_flt) |
| 11033 | goto alloc_error; |
| 11034 | |
| 11035 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 11036 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 11037 | |
| 11038 | /* The filter keyword */ |
| 11039 | len = strlen("lua.") + strlen(name) + 1; |
| 11040 | fkl->kw[0].kw = calloc(1, len); |
| 11041 | if (!fkl->kw[0].kw) |
| 11042 | goto alloc_error; |
| 11043 | |
| 11044 | snprintf((char *)fkl->kw[0].kw, len, "lua.%s", name); |
| 11045 | |
| 11046 | fkl->kw[0].parse = hlua_filter_parse_fct; |
| 11047 | fkl->kw[0].private = reg_flt; |
| 11048 | memset(&fkl->kw[1], 0, sizeof(*fkl->kw)); |
| 11049 | |
| 11050 | /* Register this new filter */ |
| 11051 | flt_register_keywords(fkl); |
| 11052 | |
| 11053 | return 0; |
| 11054 | |
| 11055 | alloc_error: |
| 11056 | release_hlua_reg_filter(reg_flt); |
| 11057 | ha_free(&fkl); |
| 11058 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 11059 | return 0; /* Never reached */ |
| 11060 | } |
| 11061 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11062 | 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] | 11063 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11064 | char **err, unsigned int *timeout) |
| 11065 | { |
| 11066 | const char *error; |
| 11067 | |
| 11068 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 11069 | if (error == PARSE_TIME_OVER) { |
| 11070 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 11071 | args[1], args[0]); |
| 11072 | return -1; |
| 11073 | } |
| 11074 | else if (error == PARSE_TIME_UNDER) { |
| 11075 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 11076 | args[1], args[0]); |
| 11077 | return -1; |
| 11078 | } |
| 11079 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11080 | memprintf(err, "%s: invalid timeout", args[0]); |
| 11081 | return -1; |
| 11082 | } |
| 11083 | return 0; |
| 11084 | } |
| 11085 | |
| 11086 | 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] | 11087 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11088 | char **err) |
| 11089 | { |
| 11090 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11091 | file, line, err, &hlua_timeout_session); |
| 11092 | } |
| 11093 | |
| 11094 | 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] | 11095 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11096 | char **err) |
| 11097 | { |
| 11098 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11099 | file, line, err, &hlua_timeout_task); |
| 11100 | } |
| 11101 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11102 | 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] | 11103 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11104 | char **err) |
| 11105 | { |
| 11106 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11107 | file, line, err, &hlua_timeout_applet); |
| 11108 | } |
| 11109 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11110 | 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] | 11111 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11112 | char **err) |
| 11113 | { |
| 11114 | char *error; |
| 11115 | |
| 11116 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 11117 | if (*error != '\0') { |
| 11118 | memprintf(err, "%s: invalid number", args[0]); |
| 11119 | return -1; |
| 11120 | } |
| 11121 | return 0; |
| 11122 | } |
| 11123 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11124 | 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] | 11125 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11126 | char **err) |
| 11127 | { |
| 11128 | char *error; |
| 11129 | |
| 11130 | if (*(args[1]) == 0) { |
| 11131 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 11132 | return -1; |
| 11133 | } |
| 11134 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 11135 | if (*error != '\0') { |
| 11136 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 11137 | return -1; |
| 11138 | } |
| 11139 | return 0; |
| 11140 | } |
| 11141 | |
| 11142 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11143 | /* This function is called by the main configuration key "lua-load". It loads and |
| 11144 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 11145 | * the main lua entry point. |
| 11146 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 11147 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 11148 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11149 | * |
| 11150 | * In some error case, LUA set an error message in top of the stack. This function |
| 11151 | * 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] | 11152 | * |
| 11153 | * This function can fail with an abort() due to an Lua critical error. |
| 11154 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 11155 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11156 | */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11157 | static int hlua_load_state(char *filename, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11158 | { |
| 11159 | int error; |
| 11160 | |
| 11161 | /* Just load and compile the file. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11162 | error = luaL_loadfile(L, filename); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11163 | if (error) { |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11164 | memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1)); |
| 11165 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11166 | return -1; |
| 11167 | } |
| 11168 | |
| 11169 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11170 | error = lua_pcall(L, 0, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11171 | switch (error) { |
| 11172 | case LUA_OK: |
| 11173 | break; |
| 11174 | case LUA_ERRRUN: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11175 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1)); |
| 11176 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11177 | return -1; |
| 11178 | case LUA_ERRMEM: |
Thierry Fournier | de6145f | 2020-11-29 00:55:53 +0100 | [diff] [blame] | 11179 | memprintf(err, "Lua out of memory error\n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11180 | return -1; |
| 11181 | case LUA_ERRERR: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11182 | memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1)); |
| 11183 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11184 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 11185 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11186 | case LUA_ERRGCMM: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11187 | memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1)); |
| 11188 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11189 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 11190 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11191 | default: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11192 | memprintf(err, "Lua unknown 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; |
| 11195 | } |
| 11196 | |
| 11197 | return 0; |
| 11198 | } |
| 11199 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11200 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 11201 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11202 | char **err) |
| 11203 | { |
| 11204 | if (*(args[1]) == 0) { |
| 11205 | memprintf(err, "'%s' expects a file name as parameter.\n", args[0]); |
| 11206 | return -1; |
| 11207 | } |
| 11208 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11209 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11210 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11211 | ha_set_thread(NULL); |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 11212 | return hlua_load_state(args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11213 | } |
| 11214 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11215 | 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] | 11216 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11217 | char **err) |
| 11218 | { |
| 11219 | int len; |
| 11220 | |
| 11221 | if (*(args[1]) == 0) { |
| 11222 | memprintf(err, "'%s' expects a file as parameter.\n", args[0]); |
| 11223 | return -1; |
| 11224 | } |
| 11225 | |
| 11226 | if (per_thread_load == NULL) { |
| 11227 | /* allocate the first entry large enough to store the final NULL */ |
| 11228 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 11229 | if (per_thread_load == NULL) { |
| 11230 | memprintf(err, "out of memory error"); |
| 11231 | return -1; |
| 11232 | } |
| 11233 | } |
| 11234 | |
| 11235 | /* count used entries */ |
| 11236 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 11237 | ; |
| 11238 | |
| 11239 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 11240 | if (per_thread_load == NULL) { |
| 11241 | memprintf(err, "out of memory error"); |
| 11242 | return -1; |
| 11243 | } |
| 11244 | |
| 11245 | per_thread_load[len] = strdup(args[1]); |
| 11246 | per_thread_load[len + 1] = NULL; |
| 11247 | |
| 11248 | if (per_thread_load[len] == NULL) { |
| 11249 | memprintf(err, "out of memory error"); |
| 11250 | return -1; |
| 11251 | } |
| 11252 | |
| 11253 | /* loading for thread 1 only */ |
| 11254 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11255 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11256 | return hlua_load_state(args[1], hlua_states[1], err); |
| 11257 | } |
| 11258 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11259 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 11260 | * in the given <ctx>. |
| 11261 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 11262 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11263 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 11264 | lua_getglobal(L, "package"); /* push package variable */ |
| 11265 | lua_pushstring(L, path); /* push given path */ |
| 11266 | lua_pushstring(L, ";"); /* push semicolon */ |
| 11267 | lua_getfield(L, -3, type); /* push old path */ |
| 11268 | lua_concat(L, 3); /* concatenate to new path */ |
| 11269 | lua_setfield(L, -2, type); /* store new path */ |
| 11270 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11271 | |
| 11272 | return 0; |
| 11273 | } |
| 11274 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11275 | 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] | 11276 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11277 | char **err) |
| 11278 | { |
| 11279 | char *path; |
| 11280 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11281 | struct prepend_path *p = NULL; |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 11282 | size_t i; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11283 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11284 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11285 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11286 | } |
| 11287 | |
| 11288 | if (!(*args[1])) { |
| 11289 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11290 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11291 | } |
| 11292 | path = args[1]; |
| 11293 | |
| 11294 | if (*args[2]) { |
| 11295 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 11296 | 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] | 11297 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11298 | } |
| 11299 | type = args[2]; |
| 11300 | } |
| 11301 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11302 | p = calloc(1, sizeof(*p)); |
| 11303 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11304 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11305 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11306 | } |
| 11307 | p->path = strdup(path); |
| 11308 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11309 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11310 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11311 | } |
| 11312 | p->type = strdup(type); |
| 11313 | if (p->type == 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 err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11316 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 11317 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11318 | |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 11319 | /* Handle the global state and the per-thread state for the first |
| 11320 | * thread. The remaining threads will be initialized based on |
| 11321 | * prepend_path_list. |
| 11322 | */ |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 11323 | for (i = 0; i < 2; i++) { |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 11324 | lua_State *L = hlua_states[i]; |
| 11325 | const char *error; |
| 11326 | |
| 11327 | if (setjmp(safe_ljmp_env) != 0) { |
| 11328 | lua_atpanic(L, hlua_panic_safe); |
| 11329 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11330 | error = lua_tostring(L, -1); |
| 11331 | else |
| 11332 | error = "critical error"; |
| 11333 | fprintf(stderr, "lua-prepend-path: %s.\n", error); |
| 11334 | exit(1); |
| 11335 | } else { |
| 11336 | lua_atpanic(L, hlua_panic_ljmp); |
| 11337 | } |
| 11338 | |
| 11339 | hlua_prepend_path(L, type, path); |
| 11340 | |
| 11341 | lua_atpanic(L, hlua_panic_safe); |
| 11342 | } |
| 11343 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11344 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11345 | |
| 11346 | err2: |
| 11347 | free(p->type); |
| 11348 | free(p->path); |
| 11349 | err: |
| 11350 | free(p); |
| 11351 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11352 | } |
| 11353 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11354 | /* configuration keywords declaration */ |
| 11355 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11356 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11357 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11358 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11359 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 11360 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 11361 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11362 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11363 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11364 | { 0, NULL, NULL }, |
| 11365 | }}; |
| 11366 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 11367 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 11368 | |
William Lallemand | 5213918 | 2022-03-30 15:05:42 +0200 | [diff] [blame] | 11369 | #ifdef USE_OPENSSL |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 11370 | |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11371 | /* |
| 11372 | * This function replace a ckch_store by another one, and rebuild the ckch_inst and all its dependencies. |
| 11373 | * It does the sam as "cli_io_handler_commit_cert" but for lua, the major |
| 11374 | * difference is that the yield in lua and for the CLI is not handled the same |
| 11375 | * way. |
| 11376 | */ |
| 11377 | __LJMP static int hlua_ckch_commit_yield(lua_State *L, int status, lua_KContext ctx) |
| 11378 | { |
| 11379 | struct ckch_inst **lua_ckchi = lua_touserdata(L, -1); |
| 11380 | struct ckch_store **lua_ckchs = lua_touserdata(L, -2); |
| 11381 | struct ckch_inst *ckchi = *lua_ckchi; |
| 11382 | struct ckch_store *old_ckchs = lua_ckchs[0]; |
| 11383 | struct ckch_store *new_ckchs = lua_ckchs[1]; |
| 11384 | struct hlua *hlua; |
| 11385 | char *err = NULL; |
| 11386 | int y = 1; |
| 11387 | |
| 11388 | hlua = hlua_gethlua(L); |
| 11389 | |
| 11390 | /* get the first ckchi to copy */ |
| 11391 | if (ckchi == NULL) |
| 11392 | ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs); |
| 11393 | |
| 11394 | /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */ |
| 11395 | list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) { |
| 11396 | struct ckch_inst *new_inst; |
| 11397 | |
| 11398 | /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */ |
| 11399 | if (y % 10 == 0) { |
| 11400 | |
| 11401 | *lua_ckchi = ckchi; |
| 11402 | |
| 11403 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 11404 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_ckch_commit_yield, TICK_ETERNITY, 0)); |
| 11405 | } |
| 11406 | |
| 11407 | if (ckch_inst_rebuild(new_ckchs, ckchi, &new_inst, &err)) |
| 11408 | goto error; |
| 11409 | |
| 11410 | /* link the new ckch_inst to the duplicate */ |
| 11411 | LIST_APPEND(&new_ckchs->ckch_inst, &new_inst->by_ckchs); |
| 11412 | y++; |
| 11413 | } |
| 11414 | |
| 11415 | /* The generation is finished, we can insert everything */ |
| 11416 | ckch_store_replace(old_ckchs, new_ckchs); |
| 11417 | |
| 11418 | lua_pop(L, 2); /* pop the lua_ckchs and ckchi */ |
| 11419 | |
| 11420 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11421 | |
| 11422 | return 0; |
| 11423 | |
| 11424 | error: |
| 11425 | ckch_store_free(new_ckchs); |
| 11426 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11427 | WILL_LJMP(luaL_error(L, "%s", err)); |
| 11428 | free(err); |
| 11429 | |
| 11430 | return 0; |
| 11431 | } |
| 11432 | |
| 11433 | /* |
| 11434 | * Replace a ckch_store <filename> in the ckchs_tree with a ckch_store created |
| 11435 | * from the table in parameter. |
| 11436 | * |
| 11437 | * This is equivalent to "set ssl cert" + "commit ssl cert" over the CLI, which |
| 11438 | * means it does not need to have a transaction since everything is done in the |
| 11439 | * same function. |
| 11440 | * |
| 11441 | * CertCache.set{filename="", crt="", key="", sctl="", ocsp="", issuer=""} |
| 11442 | * |
| 11443 | */ |
| 11444 | __LJMP static int hlua_ckch_set(lua_State *L) |
| 11445 | { |
| 11446 | struct hlua *hlua; |
| 11447 | struct ckch_inst **lua_ckchi; |
| 11448 | struct ckch_store **lua_ckchs; |
| 11449 | struct ckch_store *old_ckchs = NULL; |
| 11450 | struct ckch_store *new_ckchs = NULL; |
| 11451 | int errcode = 0; |
| 11452 | char *err = NULL; |
| 11453 | struct cert_exts *cert_ext = NULL; |
| 11454 | char *filename; |
| 11455 | struct cert_key_and_chain *ckch; |
| 11456 | int ret; |
| 11457 | |
| 11458 | if (lua_type(L, -1) != LUA_TTABLE) |
| 11459 | WILL_LJMP(luaL_error(L, "'CertCache.set' needs a table as argument")); |
| 11460 | |
| 11461 | hlua = hlua_gethlua(L); |
| 11462 | |
| 11463 | /* FIXME: this should not return an error but should come back later */ |
| 11464 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 11465 | WILL_LJMP(luaL_error(L, "CertCache already under lock")); |
| 11466 | |
| 11467 | ret = lua_getfield(L, -1, "filename"); |
| 11468 | if (ret != LUA_TSTRING) { |
| 11469 | memprintf(&err, "%sNo filename specified!\n", err ? err : ""); |
| 11470 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11471 | goto end; |
| 11472 | } |
| 11473 | filename = (char *)lua_tostring(L, -1); |
| 11474 | |
| 11475 | |
| 11476 | /* look for the filename in the tree */ |
| 11477 | old_ckchs = ckchs_lookup(filename); |
| 11478 | if (!old_ckchs) { |
| 11479 | memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n", err ? err : ""); |
| 11480 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11481 | goto end; |
| 11482 | } |
| 11483 | /* TODO: handle extra_files_noext */ |
| 11484 | |
| 11485 | new_ckchs = ckchs_dup(old_ckchs); |
| 11486 | if (!new_ckchs) { |
| 11487 | memprintf(&err, "%sCannot allocate memory!\n", err ? err : ""); |
| 11488 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11489 | goto end; |
| 11490 | } |
| 11491 | |
| 11492 | ckch = new_ckchs->ckch; |
| 11493 | |
| 11494 | /* loop on the field in the table, which have the same name as the |
| 11495 | * possible extensions of files */ |
| 11496 | lua_pushnil(L); |
| 11497 | while (lua_next(L, 1)) { |
| 11498 | int i; |
| 11499 | const char *field = lua_tostring(L, -2); |
| 11500 | char *payload = (char *)lua_tostring(L, -1); |
| 11501 | |
| 11502 | if (!field || strcmp(field, "filename") == 0) { |
| 11503 | lua_pop(L, 1); |
| 11504 | continue; |
| 11505 | } |
| 11506 | |
| 11507 | for (i = 0; field && cert_exts[i].ext != NULL; i++) { |
| 11508 | if (strcmp(field, cert_exts[i].ext) == 0) { |
| 11509 | cert_ext = &cert_exts[i]; |
| 11510 | break; |
| 11511 | } |
| 11512 | } |
| 11513 | |
| 11514 | /* this is the default type, the field is not supported */ |
| 11515 | if (cert_ext == NULL) { |
| 11516 | memprintf(&err, "%sUnsupported field '%s'\n", err ? err : "", field); |
| 11517 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11518 | goto end; |
| 11519 | } |
| 11520 | |
| 11521 | /* appply the change on the duplicate */ |
| 11522 | if (cert_exts->load(filename, payload, ckch, &err) != 0) { |
| 11523 | memprintf(&err, "%sCan't load the payload\n", err ? err : ""); |
| 11524 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11525 | goto end; |
| 11526 | } |
| 11527 | lua_pop(L, 1); |
| 11528 | } |
| 11529 | |
| 11530 | /* store the pointers on the lua stack */ |
| 11531 | lua_ckchs = lua_newuserdata(L, sizeof(struct ckch_store *) * 2); |
| 11532 | lua_ckchs[0] = old_ckchs; |
| 11533 | lua_ckchs[1] = new_ckchs; |
| 11534 | lua_ckchi = lua_newuserdata(L, sizeof(struct ckch_inst *)); |
| 11535 | *lua_ckchi = NULL; |
| 11536 | |
| 11537 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 11538 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_ckch_commit_yield, TICK_ETERNITY, 0)); |
| 11539 | |
| 11540 | end: |
| 11541 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11542 | |
| 11543 | if (errcode & ERR_CODE) { |
| 11544 | ckch_store_free(new_ckchs); |
| 11545 | WILL_LJMP(luaL_error(L, "%s", err)); |
| 11546 | } |
| 11547 | free(err); |
| 11548 | |
| 11549 | return 0; |
| 11550 | } |
| 11551 | |
William Lallemand | 5213918 | 2022-03-30 15:05:42 +0200 | [diff] [blame] | 11552 | #else |
| 11553 | |
| 11554 | __LJMP static int hlua_ckch_set(lua_State *L) |
| 11555 | { |
| 11556 | WILL_LJMP(luaL_error(L, "'CertCache.set' needs an HAProxy built with OpenSSL")); |
| 11557 | |
| 11558 | return 0; |
| 11559 | } |
| 11560 | #endif /* ! USE_OPENSSL */ |
| 11561 | |
| 11562 | |
| 11563 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 11564 | /* This function can fail with an abort() due to an Lua critical error. |
| 11565 | * We are in the initialisation process of HAProxy, this abort() is |
| 11566 | * tolerated. |
| 11567 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11568 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11569 | { |
| 11570 | struct hlua_init_function *init; |
| 11571 | const char *msg; |
| 11572 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 11573 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11574 | const char *kind; |
| 11575 | const char *trace; |
| 11576 | int return_status = 1; |
| 11577 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 11578 | int nres; |
| 11579 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11580 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11581 | /* disable memory limit checks if limit is not set */ |
| 11582 | if (!hlua_global_allocator.limit) |
| 11583 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 11584 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 11585 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11586 | if (setjmp(safe_ljmp_env) != 0) { |
| 11587 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11588 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11589 | error = lua_tostring(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11590 | else |
| 11591 | error = "critical error"; |
| 11592 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 11593 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11594 | } else { |
| 11595 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11596 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 11597 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11598 | hlua_fcn_post_init(L); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11599 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11600 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11601 | lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11602 | |
| 11603 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11604 | ret = lua_resume(L, L, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11605 | #else |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11606 | ret = lua_resume(L, L, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11607 | #endif |
| 11608 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11609 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11610 | |
| 11611 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11612 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 11613 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11614 | |
| 11615 | case LUA_ERRERR: |
| 11616 | kind = "message handler error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11617 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11618 | case LUA_ERRRUN: |
| 11619 | if (!kind) |
| 11620 | kind = "runtime error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11621 | msg = lua_tostring(L, -1); |
| 11622 | lua_settop(L, 0); /* Empty the stack. */ |
| 11623 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 11624 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11625 | if (msg) |
| 11626 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 11627 | else |
| 11628 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
| 11629 | return_status = 0; |
| 11630 | break; |
| 11631 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11632 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11633 | /* Unknown error */ |
| 11634 | kind = "Unknown error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11635 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11636 | case LUA_YIELD: |
| 11637 | /* yield is not configured at this step, this state doesn't happen */ |
| 11638 | if (!kind) |
| 11639 | kind = "yield not allowed"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11640 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11641 | case LUA_ERRMEM: |
| 11642 | if (!kind) |
| 11643 | kind = "out of memory error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11644 | lua_settop(L, 0); |
| 11645 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 11646 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11647 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 11648 | return_status = 0; |
| 11649 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11650 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11651 | if (!return_status) |
| 11652 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11653 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11654 | |
| 11655 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11656 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11657 | } |
| 11658 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11659 | int hlua_post_init() |
| 11660 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11661 | int ret; |
| 11662 | int i; |
| 11663 | int errors; |
| 11664 | char *err = NULL; |
| 11665 | struct hlua_function *fcn; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11666 | struct hlua_reg_filter *reg_flt; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11667 | |
Willy Tarreau | b131049 | 2021-08-30 09:35:18 +0200 | [diff] [blame] | 11668 | #if defined(USE_OPENSSL) |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11669 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11670 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11671 | int saved_used_backed = global.ssl_used_backend; |
| 11672 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11673 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11674 | global.ssl_used_backend = saved_used_backed; |
| 11675 | } |
| 11676 | #endif |
| 11677 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11678 | /* Perform post init of common thread */ |
| 11679 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11680 | ha_set_thread(&ha_thread_info[0]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11681 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 11682 | if (ret == 0) |
| 11683 | return 0; |
| 11684 | |
| 11685 | /* init remaining lua states and load files */ |
| 11686 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 11687 | |
| 11688 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11689 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11690 | |
| 11691 | /* Init lua state */ |
| 11692 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 11693 | |
| 11694 | /* Load lua files */ |
| 11695 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 11696 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 11697 | if (ret != 0) { |
| 11698 | ha_alert("Lua init: %s\n", err); |
| 11699 | return 0; |
| 11700 | } |
| 11701 | } |
| 11702 | } |
| 11703 | |
| 11704 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11705 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11706 | |
| 11707 | /* Execute post init for all states */ |
| 11708 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 11709 | |
| 11710 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11711 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11712 | |
| 11713 | /* run post init */ |
| 11714 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 11715 | if (ret == 0) |
| 11716 | return 0; |
| 11717 | } |
| 11718 | |
| 11719 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11720 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11721 | |
| 11722 | /* control functions registering. Each function must have: |
| 11723 | * - only the function_ref[0] set positive and all other to -1 |
| 11724 | * - only the function_ref[0] set to -1 and all other positive |
| 11725 | * This ensure a same reference is not used both in shared |
| 11726 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11727 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11728 | * complicated to found for the end user. |
| 11729 | */ |
| 11730 | errors = 0; |
| 11731 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 11732 | ret = 0; |
| 11733 | for (i = 1; i < global.nbthread + 1; i++) { |
| 11734 | if (fcn->function_ref[i] == -1) |
| 11735 | ret--; |
| 11736 | else |
| 11737 | ret++; |
| 11738 | } |
| 11739 | if (abs(ret) != global.nbthread) { |
| 11740 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 11741 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 11742 | errors++; |
| 11743 | continue; |
| 11744 | } |
| 11745 | |
| 11746 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11747 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 11748 | "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] | 11749 | "exclusive.\n", fcn->name); |
| 11750 | errors++; |
| 11751 | } |
| 11752 | } |
| 11753 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11754 | /* Do the same with registered filters */ |
| 11755 | list_for_each_entry(reg_flt, &referenced_filters, l) { |
| 11756 | ret = 0; |
| 11757 | for (i = 1; i < global.nbthread + 1; i++) { |
| 11758 | if (reg_flt->flt_ref[i] == -1) |
| 11759 | ret--; |
| 11760 | else |
| 11761 | ret++; |
| 11762 | } |
| 11763 | if (abs(ret) != global.nbthread) { |
| 11764 | ha_alert("Lua filter '%s' is not referenced in all thread. " |
| 11765 | "Expect function in all thread or in none thread.\n", reg_flt->name); |
| 11766 | errors++; |
| 11767 | continue; |
| 11768 | } |
| 11769 | |
| 11770 | if ((reg_flt->flt_ref[0] == -1) == (ret < 0)) { |
| 11771 | ha_alert("Lua filter '%s' is referenced both ins shared Lua context (through lua-load) " |
| 11772 | "and per-thread Lua context (through lua-load-per-thread). these two context " |
| 11773 | "exclusive.\n", fcn->name); |
| 11774 | errors++; |
| 11775 | } |
| 11776 | } |
| 11777 | |
| 11778 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11779 | if (errors > 0) |
| 11780 | return 0; |
| 11781 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11782 | /* 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] | 11783 | * -1 in order to have probably a segfault if someone use it |
| 11784 | */ |
| 11785 | hlua_state_id = -1; |
| 11786 | |
| 11787 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11788 | } |
| 11789 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11790 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 11791 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 11792 | * 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] | 11793 | * allocation. <nsize> is the requested new size. A new allocation is |
| 11794 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11795 | * zero. This one verifies that the limits are respected but is optimized |
| 11796 | * 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] | 11797 | * |
| 11798 | * Warning: while this API ressembles glibc's realloc() a lot, glibc surpasses |
| 11799 | * POSIX by making realloc(ptr,0) an effective free(), but others do not do |
| 11800 | * that and will simply allocate zero as if it were the result of malloc(0), |
| 11801 | * so mapping this onto realloc() will lead to memory leaks on non-glibc |
| 11802 | * systems. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11803 | */ |
| 11804 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 11805 | { |
| 11806 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11807 | size_t limit, old, new; |
| 11808 | |
| 11809 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 11810 | * for accounting anymore. |
| 11811 | */ |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 11812 | if (likely(~zone->limit == 0)) { |
| 11813 | if (!nsize) |
| 11814 | ha_free(&ptr); |
| 11815 | else |
| 11816 | ptr = realloc(ptr, nsize); |
| 11817 | return ptr; |
| 11818 | } |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11819 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 11820 | if (!ptr) |
| 11821 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11822 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11823 | /* enforce strict limits across all threads */ |
| 11824 | limit = zone->limit; |
| 11825 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 11826 | do { |
| 11827 | new = old + nsize - osize; |
| 11828 | if (unlikely(nsize && limit && new > limit)) |
| 11829 | return NULL; |
| 11830 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11831 | |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 11832 | if (!nsize) |
| 11833 | ha_free(&ptr); |
| 11834 | else |
| 11835 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11836 | |
| 11837 | if (unlikely(!ptr && nsize)) // failed |
| 11838 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 11839 | |
| 11840 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11841 | return ptr; |
| 11842 | } |
| 11843 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11844 | /* 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] | 11845 | * We are in the initialisation process of HAProxy, this abort() is |
| 11846 | * tolerated. |
| 11847 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11848 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 11849 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11850 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11851 | int idx; |
| 11852 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11853 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11854 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 11855 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11856 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11857 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11858 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11859 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11860 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11861 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11862 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11863 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11864 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11865 | *context = NULL; |
| 11866 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 11867 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 11868 | * the Lua function can fail with an abort. We are in the initialisation |
| 11869 | * process of HAProxy, this abort() is tolerated. |
| 11870 | */ |
| 11871 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11872 | /* Call post initialisation function in safe environment. */ |
| 11873 | if (setjmp(safe_ljmp_env) != 0) { |
| 11874 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11875 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11876 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 11877 | else |
| 11878 | error_msg = "critical error"; |
| 11879 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 11880 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11881 | } else { |
| 11882 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 11883 | } |
| 11884 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11885 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11886 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 11887 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 11888 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 11889 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11890 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 11891 | #endif |
| 11892 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11893 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 11894 | #endif |
| 11895 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 11896 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11897 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11898 | /* Apply configured prepend path */ |
| 11899 | list_for_each_entry(pp, &prepend_path_list, l) |
| 11900 | hlua_prepend_path(L, pp->type, pp->path); |
| 11901 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11902 | /* |
| 11903 | * |
| 11904 | * Create "core" object. |
| 11905 | * |
| 11906 | */ |
| 11907 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 11908 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11909 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11910 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11911 | /* set the thread id */ |
| 11912 | hlua_class_const_int(L, "thread", thread_num); |
| 11913 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11914 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 11915 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11916 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11917 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11918 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11919 | hlua_class_function(L, "register_init", hlua_register_init); |
| 11920 | hlua_class_function(L, "register_task", hlua_register_task); |
| 11921 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 11922 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 11923 | hlua_class_function(L, "register_action", hlua_register_action); |
| 11924 | hlua_class_function(L, "register_service", hlua_register_service); |
| 11925 | hlua_class_function(L, "register_cli", hlua_register_cli); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11926 | hlua_class_function(L, "register_filter", hlua_register_filter); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11927 | hlua_class_function(L, "yield", hlua_yield); |
| 11928 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 11929 | hlua_class_function(L, "sleep", hlua_sleep); |
| 11930 | hlua_class_function(L, "msleep", hlua_msleep); |
| 11931 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 11932 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 11933 | hlua_class_function(L, "set_map", hlua_set_map); |
| 11934 | hlua_class_function(L, "del_map", hlua_del_map); |
| 11935 | hlua_class_function(L, "tcp", hlua_socket_new); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 11936 | hlua_class_function(L, "httpclient", hlua_httpclient_new); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11937 | hlua_class_function(L, "log", hlua_log); |
| 11938 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 11939 | hlua_class_function(L, "Info", hlua_log_info); |
| 11940 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 11941 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 11942 | hlua_class_function(L, "done", hlua_done); |
| 11943 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11944 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11945 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 11946 | |
| 11947 | /* |
| 11948 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11949 | * Create "act" object. |
| 11950 | * |
| 11951 | */ |
| 11952 | |
| 11953 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11954 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11955 | |
| 11956 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11957 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 11958 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 11959 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 11960 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 11961 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 11962 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 11963 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 11964 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11965 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11966 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 11967 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11968 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11969 | |
| 11970 | /* |
| 11971 | * |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11972 | * Create "Filter" object. |
| 11973 | * |
| 11974 | */ |
| 11975 | |
| 11976 | /* This table entry is the object "filter" base. */ |
| 11977 | lua_newtable(L); |
| 11978 | |
| 11979 | /* push flags and constants */ |
| 11980 | hlua_class_const_int(L, "CONTINUE", 1); |
| 11981 | hlua_class_const_int(L, "WAIT", 0); |
| 11982 | hlua_class_const_int(L, "ERROR", -1); |
| 11983 | |
| 11984 | hlua_class_const_int(L, "FLT_CFG_FL_HTX", FLT_CFG_FL_HTX); |
| 11985 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11986 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
| 11987 | hlua_class_function(L, "register_data_filter", hlua_register_data_filter); |
| 11988 | hlua_class_function(L, "unregister_data_filter", hlua_unregister_data_filter); |
| 11989 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11990 | lua_setglobal(L, "filter"); |
| 11991 | |
| 11992 | /* |
| 11993 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11994 | * Register class Map |
| 11995 | * |
| 11996 | */ |
| 11997 | |
| 11998 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11999 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12000 | |
| 12001 | /* register pattern types. */ |
| 12002 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12003 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 12004 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12005 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12006 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 12007 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12008 | |
| 12009 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12010 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12011 | |
| 12012 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12013 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12014 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12015 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12016 | lua_pushstring(L, "__index"); |
| 12017 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12018 | |
| 12019 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12020 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 12021 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12022 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12023 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12024 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 12025 | /* Register previous table in the registry with reference and named entry. |
| 12026 | * The function hlua_register_metatable() pops the stack, so we |
| 12027 | * previously create a copy of the table. |
| 12028 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12029 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 12030 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12031 | |
| 12032 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12033 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12034 | |
| 12035 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12036 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12037 | |
| 12038 | /* |
| 12039 | * |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12040 | * Register "CertCache" class |
| 12041 | * |
| 12042 | */ |
| 12043 | |
| 12044 | /* Create and fill the metatable. */ |
| 12045 | lua_newtable(L); |
| 12046 | /* Register */ |
| 12047 | hlua_class_function(L, "set", hlua_ckch_set); |
| 12048 | lua_setglobal(L, CLASS_CERTCACHE); /* Create global object called Regex */ |
| 12049 | |
| 12050 | /* |
| 12051 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12052 | * Register class Channel |
| 12053 | * |
| 12054 | */ |
| 12055 | |
| 12056 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12057 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12058 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12059 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12060 | lua_pushstring(L, "__index"); |
| 12061 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12062 | |
| 12063 | /* Register . */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12064 | hlua_class_function(L, "data", hlua_channel_get_data); |
| 12065 | hlua_class_function(L, "line", hlua_channel_get_line); |
| 12066 | hlua_class_function(L, "set", hlua_channel_set_data); |
| 12067 | hlua_class_function(L, "remove", hlua_channel_del_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12068 | hlua_class_function(L, "append", hlua_channel_append); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12069 | hlua_class_function(L, "prepend", hlua_channel_prepend); |
| 12070 | hlua_class_function(L, "insert", hlua_channel_insert_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12071 | hlua_class_function(L, "send", hlua_channel_send); |
| 12072 | hlua_class_function(L, "forward", hlua_channel_forward); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12073 | hlua_class_function(L, "input", hlua_channel_get_in_len); |
| 12074 | hlua_class_function(L, "output", hlua_channel_get_out_len); |
| 12075 | hlua_class_function(L, "may_recv", hlua_channel_may_recv); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12076 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 12077 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12078 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12079 | /* Deprecated API */ |
| 12080 | hlua_class_function(L, "get", hlua_channel_get); |
| 12081 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 12082 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 12083 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 12084 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 12085 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12086 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12087 | |
| 12088 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12089 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12090 | |
| 12091 | /* |
| 12092 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12093 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12094 | * |
| 12095 | */ |
| 12096 | |
| 12097 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12098 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12099 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12100 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12101 | lua_pushstring(L, "__index"); |
| 12102 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12103 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12104 | /* Browse existing fetches and create the associated |
| 12105 | * object method. |
| 12106 | */ |
| 12107 | sf = NULL; |
| 12108 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12109 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 12110 | * by an underscore. |
| 12111 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 12112 | strlcpy2(trash.area, sf->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12113 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12114 | if (*p == '.' || *p == '-' || *p == '+') |
| 12115 | *p = '_'; |
| 12116 | |
| 12117 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12118 | lua_pushstring(L, trash.area); |
| 12119 | lua_pushlightuserdata(L, sf); |
| 12120 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 12121 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12122 | } |
| 12123 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12124 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12125 | |
| 12126 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12127 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12128 | |
| 12129 | /* |
| 12130 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12131 | * Register class Converters |
| 12132 | * |
| 12133 | */ |
| 12134 | |
| 12135 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12136 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12137 | |
| 12138 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12139 | lua_pushstring(L, "__index"); |
| 12140 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12141 | |
| 12142 | /* Browse existing converters and create the associated |
| 12143 | * object method. |
| 12144 | */ |
| 12145 | sc = NULL; |
| 12146 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12147 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 12148 | * by an underscore. |
| 12149 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 12150 | strlcpy2(trash.area, sc->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12151 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12152 | if (*p == '.' || *p == '-' || *p == '+') |
| 12153 | *p = '_'; |
| 12154 | |
| 12155 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12156 | lua_pushstring(L, trash.area); |
| 12157 | lua_pushlightuserdata(L, sc); |
| 12158 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 12159 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12160 | } |
| 12161 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12162 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12163 | |
| 12164 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12165 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12166 | |
| 12167 | /* |
| 12168 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12169 | * Register class HTTP |
| 12170 | * |
| 12171 | */ |
| 12172 | |
| 12173 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12174 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12175 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12176 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12177 | lua_pushstring(L, "__index"); |
| 12178 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12179 | |
| 12180 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12181 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 12182 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 12183 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 12184 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 12185 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 12186 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 12187 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 12188 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 12189 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 12190 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12191 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12192 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 12193 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 12194 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 12195 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 12196 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 12197 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 12198 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12199 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12200 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12201 | |
| 12202 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12203 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12204 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 12205 | /* |
| 12206 | * |
| 12207 | * Register class HTTPMessage |
| 12208 | * |
| 12209 | */ |
| 12210 | |
| 12211 | /* Create and fill the metatable. */ |
| 12212 | lua_newtable(L); |
| 12213 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 12214 | /* Create and fill the __index entry. */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 12215 | lua_pushstring(L, "__index"); |
| 12216 | lua_newtable(L); |
| 12217 | |
| 12218 | /* Register Lua functions. */ |
| 12219 | hlua_class_function(L, "is_resp", hlua_http_msg_is_resp); |
| 12220 | hlua_class_function(L, "get_stline", hlua_http_msg_get_stline); |
| 12221 | hlua_class_function(L, "get_headers", hlua_http_msg_get_headers); |
| 12222 | hlua_class_function(L, "del_header", hlua_http_msg_del_hdr); |
| 12223 | hlua_class_function(L, "rep_header", hlua_http_msg_rep_hdr); |
| 12224 | hlua_class_function(L, "rep_value", hlua_http_msg_rep_val); |
| 12225 | hlua_class_function(L, "add_header", hlua_http_msg_add_hdr); |
| 12226 | hlua_class_function(L, "set_header", hlua_http_msg_set_hdr); |
| 12227 | hlua_class_function(L, "set_method", hlua_http_msg_set_meth); |
| 12228 | hlua_class_function(L, "set_path", hlua_http_msg_set_path); |
| 12229 | hlua_class_function(L, "set_query", hlua_http_msg_set_query); |
| 12230 | hlua_class_function(L, "set_uri", hlua_http_msg_set_uri); |
| 12231 | hlua_class_function(L, "set_status", hlua_http_msg_set_status); |
| 12232 | hlua_class_function(L, "is_full", hlua_http_msg_is_full); |
| 12233 | hlua_class_function(L, "may_recv", hlua_http_msg_may_recv); |
| 12234 | hlua_class_function(L, "eom", hlua_http_msg_is_eom); |
| 12235 | hlua_class_function(L, "input", hlua_http_msg_get_in_len); |
| 12236 | hlua_class_function(L, "output", hlua_http_msg_get_out_len); |
| 12237 | |
| 12238 | hlua_class_function(L, "body", hlua_http_msg_get_body); |
| 12239 | hlua_class_function(L, "set", hlua_http_msg_set_data); |
| 12240 | hlua_class_function(L, "remove", hlua_http_msg_del_data); |
| 12241 | hlua_class_function(L, "append", hlua_http_msg_append); |
| 12242 | hlua_class_function(L, "prepend", hlua_http_msg_prepend); |
| 12243 | hlua_class_function(L, "insert", hlua_http_msg_insert_data); |
| 12244 | hlua_class_function(L, "set_eom", hlua_http_msg_set_eom); |
| 12245 | hlua_class_function(L, "unset_eom", hlua_http_msg_unset_eom); |
| 12246 | |
| 12247 | hlua_class_function(L, "send", hlua_http_msg_send); |
| 12248 | hlua_class_function(L, "forward", hlua_http_msg_forward); |
| 12249 | |
| 12250 | lua_rawset(L, -3); |
| 12251 | |
| 12252 | /* Register previous table in the registry with reference and named entry. */ |
| 12253 | class_http_msg_ref = hlua_register_metatable(L, CLASS_HTTP_MSG); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12254 | |
| 12255 | /* |
| 12256 | * |
| 12257 | * Register class HTTPClient |
| 12258 | * |
| 12259 | */ |
| 12260 | |
| 12261 | /* Create and fill the metatable. */ |
| 12262 | lua_newtable(L); |
| 12263 | lua_pushstring(L, "__index"); |
| 12264 | lua_newtable(L); |
| 12265 | hlua_class_function(L, "get", hlua_httpclient_get); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 12266 | hlua_class_function(L, "head", hlua_httpclient_head); |
| 12267 | hlua_class_function(L, "put", hlua_httpclient_put); |
| 12268 | hlua_class_function(L, "post", hlua_httpclient_post); |
| 12269 | hlua_class_function(L, "delete", hlua_httpclient_delete); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12270 | lua_settable(L, -3); /* Sets the __index entry. */ |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 12271 | /* Register the garbage collector entry. */ |
| 12272 | lua_pushstring(L, "__gc"); |
| 12273 | lua_pushcclosure(L, hlua_httpclient_gc, 0); |
| 12274 | lua_settable(L, -3); /* Push the last 2 entries in the table at index -3 */ |
| 12275 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12276 | |
| 12277 | |
| 12278 | class_httpclient_ref = hlua_register_metatable(L, CLASS_HTTPCLIENT); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12279 | /* |
| 12280 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12281 | * Register class AppletTCP |
| 12282 | * |
| 12283 | */ |
| 12284 | |
| 12285 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12286 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12287 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12288 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12289 | lua_pushstring(L, "__index"); |
| 12290 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12291 | |
| 12292 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12293 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 12294 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 12295 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 12296 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 12297 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 12298 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 12299 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 12300 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12301 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12302 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12303 | |
| 12304 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12305 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12306 | |
| 12307 | /* |
| 12308 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12309 | * Register class AppletHTTP |
| 12310 | * |
| 12311 | */ |
| 12312 | |
| 12313 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12314 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12315 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12316 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12317 | lua_pushstring(L, "__index"); |
| 12318 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12319 | |
| 12320 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12321 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 12322 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 12323 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 12324 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 12325 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 12326 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 12327 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 12328 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 12329 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 12330 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 12331 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12332 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12333 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12334 | |
| 12335 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12336 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12337 | |
| 12338 | /* |
| 12339 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12340 | * Register class TXN |
| 12341 | * |
| 12342 | */ |
| 12343 | |
| 12344 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12345 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12346 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12347 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12348 | lua_pushstring(L, "__index"); |
| 12349 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12350 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 12351 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12352 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 12353 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 12354 | hlua_class_function(L, "set_var", hlua_set_var); |
| 12355 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 12356 | hlua_class_function(L, "get_var", hlua_get_var); |
| 12357 | hlua_class_function(L, "done", hlua_txn_done); |
| 12358 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 12359 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 12360 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 12361 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 12362 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 12363 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 12364 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 12365 | hlua_class_function(L, "log", hlua_txn_log); |
| 12366 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 12367 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 12368 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 12369 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 12370 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12371 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12372 | |
| 12373 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12374 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12375 | |
| 12376 | /* |
| 12377 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 12378 | * Register class reply |
| 12379 | * |
| 12380 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12381 | lua_newtable(L); |
| 12382 | lua_pushstring(L, "__index"); |
| 12383 | lua_newtable(L); |
| 12384 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 12385 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 12386 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 12387 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 12388 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 12389 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 12390 | |
| 12391 | |
| 12392 | /* |
| 12393 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12394 | * Register class Socket |
| 12395 | * |
| 12396 | */ |
| 12397 | |
| 12398 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12399 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12400 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12401 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12402 | lua_pushstring(L, "__index"); |
| 12403 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12404 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 12405 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12406 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 12407 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12408 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 12409 | hlua_class_function(L, "send", hlua_socket_send); |
| 12410 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 12411 | hlua_class_function(L, "close", hlua_socket_close); |
| 12412 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 12413 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 12414 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 12415 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12416 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12417 | 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] | 12418 | |
| 12419 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12420 | lua_pushstring(L, "__gc"); |
| 12421 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 12422 | 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] | 12423 | |
| 12424 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12425 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12426 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12427 | lua_atpanic(L, hlua_panic_safe); |
| 12428 | |
| 12429 | return L; |
| 12430 | } |
| 12431 | |
| 12432 | void hlua_init(void) { |
| 12433 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 12434 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12435 | #ifdef USE_OPENSSL |
| 12436 | struct srv_kw *kw; |
| 12437 | int tmp_error; |
| 12438 | char *error; |
| 12439 | char *args[] = { /* SSL client configuration. */ |
| 12440 | "ssl", |
| 12441 | "verify", |
| 12442 | "none", |
| 12443 | NULL |
| 12444 | }; |
| 12445 | #endif |
| 12446 | |
| 12447 | /* Init post init function list head */ |
| 12448 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 12449 | LIST_INIT(&hlua_init_functions[i]); |
| 12450 | |
| 12451 | /* Init state for common/shared lua parts */ |
| 12452 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12453 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12454 | hlua_states[0] = hlua_init_state(0); |
| 12455 | |
| 12456 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 12457 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12458 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12459 | hlua_states[1] = hlua_init_state(1); |
| 12460 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12461 | /* Proxy and server configuration initialisation. */ |
William Lallemand | 6bb77b9 | 2021-07-28 15:48:16 +0200 | [diff] [blame] | 12462 | 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] | 12463 | if (!socket_proxy) { |
| 12464 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 12465 | exit(1); |
| 12466 | } |
| 12467 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12468 | |
| 12469 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12470 | socket_tcp = new_server(socket_proxy); |
| 12471 | if (!socket_tcp) { |
| 12472 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 12473 | exit(1); |
| 12474 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12475 | |
| 12476 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12477 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12478 | socket_ssl = new_server(socket_proxy); |
| 12479 | if (!socket_ssl) { |
| 12480 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 12481 | exit(1); |
| 12482 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12483 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12484 | socket_ssl->use_ssl = 1; |
| 12485 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12486 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 12487 | for (i = 0; args[i] != NULL; i++) { |
| 12488 | 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] | 12489 | /* |
| 12490 | * |
| 12491 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 12492 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12493 | * features like client certificates and ssl_verify. |
| 12494 | * |
| 12495 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12496 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12497 | if (tmp_error != 0) { |
| 12498 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 12499 | abort(); /* This must be never arrives because the command line |
| 12500 | not editable by the user. */ |
| 12501 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 12502 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12503 | } |
| 12504 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12505 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 12506 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 12507 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12508 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 12509 | static void hlua_deinit() |
| 12510 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 12511 | int thr; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12512 | struct hlua_reg_filter *reg_flt, *reg_flt_bck; |
| 12513 | |
| 12514 | list_for_each_entry_safe(reg_flt, reg_flt_bck, &referenced_filters, l) |
| 12515 | release_hlua_reg_filter(reg_flt); |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 12516 | |
| 12517 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 12518 | if (hlua_states[thr]) |
| 12519 | lua_close(hlua_states[thr]); |
| 12520 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 12521 | |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 12522 | srv_drop(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 12523 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 12524 | #ifdef USE_OPENSSL |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 12525 | srv_drop(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 12526 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 12527 | |
| 12528 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 12529 | } |
| 12530 | |
| 12531 | REGISTER_POST_DEINIT(hlua_deinit); |
| 12532 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12533 | static void hlua_register_build_options(void) |
| 12534 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12535 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12536 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12537 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 12538 | hap_register_build_opts(ptr, 1); |
| 12539 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12540 | |
| 12541 | INITCALL0(STG_REGISTER, hlua_register_build_options); |