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 | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 37 | #include <haproxy/filters.h> |
Willy Tarreau | 5413a87 | 2020-06-02 19:33:08 +0200 | [diff] [blame] | 38 | #include <haproxy/h1.h> |
Willy Tarreau | 8641605 | 2020-06-04 09:20:54 +0200 | [diff] [blame] | 39 | #include <haproxy/hlua.h> |
Willy Tarreau | 8c79400 | 2020-06-04 10:05:25 +0200 | [diff] [blame] | 40 | #include <haproxy/hlua_fcn.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 41 | #include <haproxy/http_ana.h> |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 42 | #include <haproxy/http_client.h> |
Willy Tarreau | 126ba3a | 2020-06-04 18:26:43 +0200 | [diff] [blame] | 43 | #include <haproxy/http_fetch.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 44 | #include <haproxy/http_htx.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 45 | #include <haproxy/http_rules.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 46 | #include <haproxy/log.h> |
Willy Tarreau | 2cd5809 | 2020-06-04 15:10:43 +0200 | [diff] [blame] | 47 | #include <haproxy/map.h> |
Willy Tarreau | 8efbdfb | 2020-06-04 11:29:21 +0200 | [diff] [blame] | 48 | #include <haproxy/obj_type.h> |
Willy Tarreau | 225a90a | 2020-06-04 15:06:28 +0200 | [diff] [blame] | 49 | #include <haproxy/pattern.h> |
Willy Tarreau | 469509b | 2020-06-04 15:13:30 +0200 | [diff] [blame] | 50 | #include <haproxy/payload.h> |
Willy Tarreau | 3d6ee40 | 2021-05-08 20:28:07 +0200 | [diff] [blame] | 51 | #include <haproxy/proxy.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 52 | #include <haproxy/regex.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 53 | #include <haproxy/sample.h> |
Willy Tarreau | 5edca2f | 2022-05-27 09:25:10 +0200 | [diff] [blame] | 54 | #include <haproxy/sc_strm.h> |
Willy Tarreau | 198e92a | 2021-03-05 10:23:32 +0100 | [diff] [blame] | 55 | #include <haproxy/server.h> |
Willy Tarreau | 48d25b3 | 2020-06-04 18:58:52 +0200 | [diff] [blame] | 56 | #include <haproxy/session.h> |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 57 | #include <haproxy/ssl_ckch.h> |
| 58 | #include <haproxy/ssl_sock.h> |
Willy Tarreau | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 59 | #include <haproxy/stats-t.h> |
Willy Tarreau | cb086c6 | 2022-05-27 09:47:12 +0200 | [diff] [blame] | 60 | #include <haproxy/stconn.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 61 | #include <haproxy/stream.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 62 | #include <haproxy/task.h> |
Willy Tarreau | 8b550af | 2020-06-04 17:42:48 +0200 | [diff] [blame] | 63 | #include <haproxy/tcp_rules.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 64 | #include <haproxy/thread.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 65 | #include <haproxy/tools.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 66 | #include <haproxy/vars.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 67 | #include <haproxy/xref.h> |
| 68 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 69 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 70 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 71 | * macro is used only for identifying the function that can |
| 72 | * not return because a longjmp is executed. |
| 73 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 74 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 75 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 76 | */ |
| 77 | #define __LJMP |
Willy Tarreau | 4e7cc33 | 2018-10-20 17:45:48 +0200 | [diff] [blame] | 78 | #define WILL_LJMP(func) do { func; my_unreachable(); } while(0) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 79 | #define MAY_LJMP(func) func |
| 80 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 81 | /* This couple of function executes securely some Lua calls outside of |
| 82 | * the lua runtime environment. Each Lua call can return a longjmp |
| 83 | * if it encounter a memory error. |
| 84 | * |
| 85 | * Lua documentation extract: |
| 86 | * |
| 87 | * If an error happens outside any protected environment, Lua calls |
| 88 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 89 | * exiting the host application. Your panic function can avoid this |
| 90 | * exit by never returning (e.g., doing a long jump to your own |
| 91 | * recovery point outside Lua). |
| 92 | * |
| 93 | * The panic function runs as if it were a message handler (see |
Willy Tarreau | 3dfb7da | 2022-03-02 22:33:39 +0100 | [diff] [blame] | 94 | * #2.3); in particular, the error message is at the top of the |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 95 | * stack. However, there is no guarantee about stack space. To push |
| 96 | * anything on the stack, the panic function must first check the |
Willy Tarreau | 3dfb7da | 2022-03-02 22:33:39 +0100 | [diff] [blame] | 97 | * available space (see #4.2). |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 98 | * |
| 99 | * We must check all the Lua entry point. This includes: |
| 100 | * - The include/proto/hlua.h exported functions |
| 101 | * - the task wrapper function |
| 102 | * - The action wrapper function |
| 103 | * - The converters wrapper function |
| 104 | * - The sample-fetch wrapper functions |
| 105 | * |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 106 | * It is tolerated that the initialisation function returns an abort. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 107 | * Before each Lua abort, an error message is written on stderr. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 108 | * |
| 109 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 110 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 111 | * because they must be exists in the program stack when the longjmp |
| 112 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 113 | * |
| 114 | * Note that the Lua processing is not really thread safe. It provides |
| 115 | * heavy system which consists to add our own lock function in the Lua |
| 116 | * code and recompile the library. This system will probably not accepted |
| 117 | * by maintainers of various distribs. |
| 118 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 119 | * Our main execution point of the Lua is the function lua_resume(). A |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 120 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 121 | * of function and a lua_unlock() at the end of the function. So I |
| 122 | * conclude that the Lua thread safe mode just perform a mutex around |
| 123 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 124 | * easier for distro maintainers. |
| 125 | * |
| 126 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 127 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 128 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 129 | */ |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 130 | __decl_spinlock(hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 131 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 132 | static int hlua_panic_safe(lua_State *L) { return 0; } |
Willy Tarreau | 6a51090 | 2021-07-14 19:41:25 +0200 | [diff] [blame] | 133 | static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); return 0; } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 134 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 135 | /* This is the chained list of struct hlua_function referenced |
| 136 | * for haproxy action, sample-fetches, converters, cli and |
| 137 | * applet bindings. It is used for a post-initialisation control. |
| 138 | */ |
| 139 | static struct list referenced_functions = LIST_HEAD_INIT(referenced_functions); |
| 140 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 141 | /* This variable is used only during initialization to identify the Lua state |
| 142 | * currently being initialized. 0 is the common lua state, 1 to n are the Lua |
| 143 | * states dedicated to each thread (in this case hlua_state_id==tid+1). |
| 144 | */ |
| 145 | static int hlua_state_id; |
| 146 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 147 | /* This is a NULL-terminated list of lua file which are referenced to load per thread */ |
| 148 | static char **per_thread_load = NULL; |
| 149 | |
| 150 | lua_State *hlua_init_state(int thread_id); |
| 151 | |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 152 | /* This function takes the Lua global lock. Keep this function's visibility |
| 153 | * global so that it can appear in stack dumps and performance profiles! |
| 154 | */ |
| 155 | void lua_take_global_lock() |
| 156 | { |
| 157 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
| 158 | } |
| 159 | |
| 160 | static inline void lua_drop_global_lock() |
| 161 | { |
| 162 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
| 163 | } |
| 164 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 165 | #define SET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 166 | ({ \ |
| 167 | int ret; \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 168 | if ((__HLUA)->state_id == 0) \ |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 169 | lua_take_global_lock(); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 170 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 171 | lua_atpanic(__L, hlua_panic_safe); \ |
| 172 | ret = 0; \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 173 | if ((__HLUA)->state_id == 0) \ |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 174 | lua_drop_global_lock(); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 175 | } else { \ |
| 176 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 177 | ret = 1; \ |
| 178 | } \ |
| 179 | ret; \ |
| 180 | }) |
| 181 | |
| 182 | /* If we are the last function catching Lua errors, we |
| 183 | * must reset the panic function. |
| 184 | */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 185 | #define RESET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 186 | do { \ |
| 187 | lua_atpanic(__L, hlua_panic_safe); \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 188 | if ((__HLUA)->state_id == 0) \ |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 189 | lua_drop_global_lock(); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 190 | } while(0) |
| 191 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 192 | #define SET_SAFE_LJMP(__HLUA) \ |
| 193 | SET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 194 | |
| 195 | #define RESET_SAFE_LJMP(__HLUA) \ |
| 196 | RESET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 197 | |
| 198 | #define SET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 199 | SET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 200 | |
| 201 | #define RESET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 202 | RESET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 203 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 204 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 205 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 206 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 207 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 208 | /* unused: 0x08, 0x10 */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 209 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 210 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 211 | |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 212 | /* The main Lua execution context. The 0 index is the |
| 213 | * common state shared by all threads. |
| 214 | */ |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 215 | static lua_State *hlua_states[MAX_THREADS + 1]; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 216 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 217 | #define HLUA_FLT_CB_FINAL 0x00000001 |
| 218 | #define HLUA_FLT_CB_RETVAL 0x00000002 |
| 219 | #define HLUA_FLT_CB_ARG_CHN 0x00000004 |
| 220 | #define HLUA_FLT_CB_ARG_HTTP_MSG 0x00000008 |
| 221 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 222 | #define HLUA_FLT_CTX_FL_PAYLOAD 0x00000001 |
| 223 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 224 | struct hlua_reg_filter { |
| 225 | char *name; |
| 226 | int flt_ref[MAX_THREADS + 1]; |
| 227 | int fun_ref[MAX_THREADS + 1]; |
| 228 | struct list l; |
| 229 | }; |
| 230 | |
| 231 | struct hlua_flt_config { |
| 232 | struct hlua_reg_filter *reg; |
| 233 | int ref[MAX_THREADS + 1]; |
| 234 | char **args; |
| 235 | }; |
| 236 | |
| 237 | struct hlua_flt_ctx { |
| 238 | int ref; /* ref to the filter lua object */ |
| 239 | struct hlua *hlua[2]; /* lua runtime context (0: request, 1: response) */ |
| 240 | unsigned int cur_off[2]; /* current offset (0: request, 1: response) */ |
| 241 | unsigned int cur_len[2]; /* current forwardable length (0: request, 1: response) */ |
| 242 | unsigned int flags; /* HLUA_FLT_CTX_FL_* */ |
| 243 | }; |
| 244 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 245 | /* appctx context used by the cosockets */ |
| 246 | struct hlua_csk_ctx { |
| 247 | int connected; |
| 248 | struct xref xref; /* cross reference with the Lua object owner. */ |
| 249 | struct list wake_on_read; |
| 250 | struct list wake_on_write; |
| 251 | struct appctx *appctx; |
| 252 | int die; |
| 253 | }; |
| 254 | |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 255 | /* appctx context used by TCP services */ |
| 256 | struct hlua_tcp_ctx { |
| 257 | struct hlua *hlua; |
| 258 | int flags; |
| 259 | struct task *task; |
| 260 | }; |
| 261 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 262 | /* appctx context used by HTTP services */ |
| 263 | struct hlua_http_ctx { |
| 264 | struct hlua *hlua; |
| 265 | int left_bytes; /* The max amount of bytes that we can read. */ |
| 266 | int flags; |
| 267 | int status; |
| 268 | const char *reason; |
| 269 | struct task *task; |
| 270 | }; |
| 271 | |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 272 | /* used by registered CLI keywords */ |
| 273 | struct hlua_cli_ctx { |
| 274 | struct hlua *hlua; |
| 275 | struct task *task; |
| 276 | struct hlua_function *fcn; |
| 277 | }; |
| 278 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 279 | DECLARE_STATIC_POOL(pool_head_hlua_flt_ctx, "hlua_flt_ctx", sizeof(struct hlua_flt_ctx)); |
| 280 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 281 | static int hlua_filter_from_payload(struct filter *filter); |
| 282 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 283 | /* This is the chained list of struct hlua_flt referenced |
| 284 | * for haproxy filters. It is used for a post-initialisation control. |
| 285 | */ |
| 286 | static struct list referenced_filters = LIST_HEAD_INIT(referenced_filters); |
| 287 | |
| 288 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 289 | /* This is the memory pool containing struct lua for applets |
| 290 | * (including cli). |
| 291 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 292 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 293 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 294 | /* Used for Socket connection. */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 295 | static struct proxy *socket_proxy; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 296 | static struct server *socket_tcp; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 297 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 298 | static struct server *socket_ssl; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 299 | #endif |
| 300 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 301 | /* List head of the function called at the initialisation time. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 302 | struct list hlua_init_functions[MAX_THREADS + 1]; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 303 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 304 | /* The following variables contains the reference of the different |
| 305 | * Lua classes. These references are useful for identify metadata |
| 306 | * associated with an object. |
| 307 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 308 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 309 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 310 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 311 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 312 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 313 | static int class_http_ref; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 314 | static int class_http_msg_ref; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 315 | static int class_httpclient_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 316 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 317 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 318 | static int class_applet_http_ref; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 319 | static int class_txn_reply_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 320 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 321 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 322 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 323 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 324 | * because a task may remain alive during all the haproxy execution. |
| 325 | */ |
| 326 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 327 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 328 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 329 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 330 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 331 | * it is used for preventing infinite loops. |
| 332 | * |
| 333 | * I test the scheer with an infinite loop containing one incrementation |
| 334 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 335 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 336 | * to one interrupt each 10 000 instructions. |
| 337 | * |
| 338 | * configured | Number of |
| 339 | * instructions | loops executed |
| 340 | * between two | in milions |
| 341 | * forced yields | |
| 342 | * ---------------+--------------- |
| 343 | * 10 | 160 |
| 344 | * 500 | 670 |
| 345 | * 1000 | 680 |
| 346 | * 5000 | 700 |
| 347 | * 7000 | 700 |
| 348 | * 8000 | 700 |
| 349 | * 9000 | 710 <- ceil |
| 350 | * 10000 | 710 |
| 351 | * 100000 | 710 |
| 352 | * 1000000 | 710 |
| 353 | * |
| 354 | */ |
| 355 | static unsigned int hlua_nb_instruction = 10000; |
| 356 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 357 | /* Descriptor for the memory allocation state. The limit is pre-initialised to |
| 358 | * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it |
| 359 | * is replaced with ~0 during post_init after everything was loaded. This way |
| 360 | * it is guaranteed that if limit is ~0 the boot is complete and that if it's |
| 361 | * zero it's not yet limited and proper accounting is required. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 362 | */ |
| 363 | struct hlua_mem_allocator { |
| 364 | size_t allocated; |
| 365 | size_t limit; |
| 366 | }; |
| 367 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 368 | static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 369 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 370 | /* These functions converts types between HAProxy internal args or |
| 371 | * sample and LUA types. Another function permits to check if the |
| 372 | * LUA stack contains arguments according with an required ARG_T |
| 373 | * format. |
| 374 | */ |
| 375 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 376 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 377 | __LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 378 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 379 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 380 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 381 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 382 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 383 | __LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 384 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 385 | struct prepend_path { |
| 386 | struct list l; |
| 387 | char *type; |
| 388 | char *path; |
| 389 | }; |
| 390 | |
| 391 | static struct list prepend_path_list = LIST_HEAD_INIT(prepend_path_list); |
| 392 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 393 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 394 | do { \ |
| 395 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 396 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 397 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 398 | } while (0) |
| 399 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 400 | static inline struct hlua_function *new_hlua_function() |
| 401 | { |
| 402 | struct hlua_function *fcn; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 403 | int i; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 404 | |
| 405 | fcn = calloc(1, sizeof(*fcn)); |
| 406 | if (!fcn) |
| 407 | return NULL; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 408 | LIST_APPEND(&referenced_functions, &fcn->l); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 409 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 410 | fcn->function_ref[i] = -1; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 411 | return fcn; |
| 412 | } |
| 413 | |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 414 | static inline void release_hlua_function(struct hlua_function *fcn) |
| 415 | { |
| 416 | if (!fcn) |
| 417 | return; |
| 418 | if (fcn->name) |
| 419 | ha_free(&fcn->name); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 420 | LIST_DELETE(&fcn->l); |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 421 | ha_free(&fcn); |
| 422 | } |
| 423 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 424 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 425 | static inline int fcn_ref_to_stack_id(struct hlua_function *fcn) |
| 426 | { |
| 427 | if (fcn->function_ref[0] == -1) |
| 428 | return tid + 1; |
| 429 | return 0; |
| 430 | } |
| 431 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 432 | /* Create a new registered filter. Only its name is filled */ |
| 433 | static inline struct hlua_reg_filter *new_hlua_reg_filter(const char *name) |
| 434 | { |
| 435 | struct hlua_reg_filter *reg_flt; |
| 436 | int i; |
| 437 | |
| 438 | reg_flt = calloc(1, sizeof(*reg_flt)); |
| 439 | if (!reg_flt) |
| 440 | return NULL; |
| 441 | reg_flt->name = strdup(name); |
| 442 | if (!reg_flt->name) { |
| 443 | free(reg_flt); |
| 444 | return NULL; |
| 445 | } |
| 446 | LIST_APPEND(&referenced_filters, ®_flt->l); |
| 447 | for (i = 0; i < MAX_THREADS + 1; i++) { |
| 448 | reg_flt->flt_ref[i] = -1; |
| 449 | reg_flt->fun_ref[i] = -1; |
| 450 | } |
| 451 | return reg_flt; |
| 452 | } |
| 453 | |
| 454 | /* Release a registered filter */ |
| 455 | static inline void release_hlua_reg_filter(struct hlua_reg_filter *reg_flt) |
| 456 | { |
| 457 | if (!reg_flt) |
| 458 | return; |
| 459 | if (reg_flt->name) |
| 460 | ha_free(®_flt->name); |
| 461 | LIST_DELETE(®_flt->l); |
| 462 | ha_free(®_flt); |
| 463 | } |
| 464 | |
| 465 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 466 | static inline int reg_flt_to_stack_id(struct hlua_reg_filter *reg_flt) |
| 467 | { |
| 468 | if (reg_flt->fun_ref[0] == -1) |
| 469 | return tid + 1; |
| 470 | return 0; |
| 471 | } |
| 472 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 473 | /* Used to check an Lua function type in the stack. It creates and |
| 474 | * returns a reference of the function. This function throws an |
| 475 | * error if the rgument is not a "function". |
| 476 | */ |
| 477 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 478 | { |
| 479 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 480 | const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno)); |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 481 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 482 | } |
| 483 | lua_pushvalue(L, argno); |
| 484 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 485 | } |
| 486 | |
Christopher Faulet | ba9e21d | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 487 | /* Used to check an Lua table type in the stack. It creates and |
| 488 | * returns a reference of the table. This function throws an |
| 489 | * error if the rgument is not a "table". |
| 490 | */ |
| 491 | __LJMP unsigned int hlua_checktable(lua_State *L, int argno) |
| 492 | { |
| 493 | if (!lua_istable(L, argno)) { |
| 494 | const char *msg = lua_pushfstring(L, "table expected, got %s", luaL_typename(L, argno)); |
| 495 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 496 | } |
| 497 | lua_pushvalue(L, argno); |
| 498 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 499 | } |
| 500 | |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 501 | __LJMP const char *hlua_traceback(lua_State *L, const char* sep) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 502 | { |
| 503 | lua_Debug ar; |
| 504 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 505 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 506 | |
| 507 | while (lua_getstack(L, level++, &ar)) { |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 508 | /* Fill fields: |
| 509 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 510 | * 'l': fills in the field currentline; |
| 511 | * 'n': fills in the field name and namewhat; |
| 512 | * 't': fills in the field istailcall; |
| 513 | */ |
| 514 | lua_getinfo(L, "Slnt", &ar); |
| 515 | |
Willy Tarreau | 5c14340 | 2022-06-19 17:35:53 +0200 | [diff] [blame] | 516 | /* skip these empty entries, usually they come from deep C functions */ |
| 517 | if (ar.currentline < 0 && *ar.what == 'C' && !*ar.namewhat && !ar.name) |
| 518 | continue; |
| 519 | |
| 520 | /* Add separator */ |
| 521 | if (b_data(msg)) |
| 522 | chunk_appendf(msg, "%s", sep); |
| 523 | |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 524 | /* Append code localisation */ |
| 525 | if (ar.currentline > 0) |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 526 | chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 527 | else |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 528 | chunk_appendf(msg, "%s: ", ar.short_src); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 529 | |
| 530 | /* |
| 531 | * Get function name |
| 532 | * |
| 533 | * if namewhat is no empty, name is defined. |
| 534 | * what contains "Lua" for Lua function, "C" for C function, |
| 535 | * or "main" for main code. |
| 536 | */ |
| 537 | 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] | 538 | chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */ |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 539 | |
| 540 | 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] | 541 | chunk_appendf(msg, "in main chunk"); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 542 | |
| 543 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 544 | chunk_appendf(msg, "in function line %d", ar.linedefined); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 545 | |
| 546 | else /* nothing left... */ |
| 547 | chunk_appendf(msg, "?"); |
| 548 | |
| 549 | |
| 550 | /* Display tailed call */ |
| 551 | if (ar.istailcall) |
| 552 | chunk_appendf(msg, " ..."); |
| 553 | } |
| 554 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 555 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 559 | /* This function check the number of arguments available in the |
| 560 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 561 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 562 | */ |
| 563 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 564 | { |
| 565 | if (lua_gettop(L) == nb) |
| 566 | return; |
| 567 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 568 | } |
| 569 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 570 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 571 | * and the line number where the error is encountered. |
| 572 | */ |
| 573 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 574 | { |
| 575 | va_list argp; |
| 576 | va_start(argp, fmt); |
| 577 | luaL_where(L, 1); |
| 578 | lua_pushvfstring(L, fmt, argp); |
| 579 | va_end(argp); |
| 580 | lua_concat(L, 2); |
| 581 | return 1; |
| 582 | } |
| 583 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 584 | /* This functions is used with sample fetch and converters. It |
| 585 | * converts the HAProxy configuration argument in a lua stack |
| 586 | * values. |
| 587 | * |
| 588 | * It takes an array of "arg", and each entry of the array is |
| 589 | * converted and pushed in the LUA stack. |
| 590 | */ |
| 591 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 592 | { |
| 593 | switch (arg->type) { |
| 594 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 595 | case ARGT_TIME: |
| 596 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 597 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 598 | break; |
| 599 | |
| 600 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 601 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 602 | break; |
| 603 | |
| 604 | case ARGT_IPV4: |
| 605 | case ARGT_IPV6: |
| 606 | case ARGT_MSK4: |
| 607 | case ARGT_MSK6: |
| 608 | case ARGT_FE: |
| 609 | case ARGT_BE: |
| 610 | case ARGT_TAB: |
| 611 | case ARGT_SRV: |
| 612 | case ARGT_USR: |
| 613 | case ARGT_MAP: |
| 614 | default: |
| 615 | lua_pushnil(L); |
| 616 | break; |
| 617 | } |
| 618 | return 1; |
| 619 | } |
| 620 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 621 | /* 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] | 622 | * 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] | 623 | * with sample fetch wrappers. The input arguments are given to the |
| 624 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 625 | */ |
| 626 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 627 | { |
| 628 | switch (lua_type(L, ud)) { |
| 629 | |
| 630 | case LUA_TNUMBER: |
| 631 | case LUA_TBOOLEAN: |
| 632 | arg->type = ARGT_SINT; |
| 633 | arg->data.sint = lua_tointeger(L, ud); |
| 634 | break; |
| 635 | |
| 636 | case LUA_TSTRING: |
| 637 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 638 | 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] | 639 | /* 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] | 640 | 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] | 641 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 642 | break; |
| 643 | |
| 644 | case LUA_TUSERDATA: |
| 645 | case LUA_TNIL: |
| 646 | case LUA_TTABLE: |
| 647 | case LUA_TFUNCTION: |
| 648 | case LUA_TTHREAD: |
| 649 | case LUA_TLIGHTUSERDATA: |
| 650 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 651 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 652 | break; |
| 653 | } |
| 654 | return 1; |
| 655 | } |
| 656 | |
| 657 | /* the following functions are used to convert a struct sample |
| 658 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 659 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 660 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 661 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 662 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 663 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 664 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 665 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 666 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 667 | break; |
| 668 | |
| 669 | case SMP_T_BIN: |
| 670 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 671 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 672 | break; |
| 673 | |
| 674 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 675 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 676 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 677 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 678 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 679 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 680 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 681 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 682 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 683 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 684 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 685 | 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] | 686 | break; |
| 687 | default: |
| 688 | lua_pushnil(L); |
| 689 | break; |
| 690 | } |
| 691 | break; |
| 692 | |
| 693 | case SMP_T_IPV4: |
| 694 | case SMP_T_IPV6: |
| 695 | 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] | 696 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 697 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 698 | 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] | 699 | else |
| 700 | lua_pushnil(L); |
| 701 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 702 | default: |
| 703 | lua_pushnil(L); |
| 704 | break; |
| 705 | } |
| 706 | return 1; |
| 707 | } |
| 708 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 709 | /* the following functions are used to convert a struct sample |
| 710 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 711 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 712 | */ |
| 713 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 714 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 715 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 716 | |
| 717 | case SMP_T_BIN: |
| 718 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 719 | 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] | 720 | break; |
| 721 | |
| 722 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 723 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 724 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 725 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 726 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 727 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 728 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 729 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 730 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 731 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 732 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 733 | 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] | 734 | break; |
| 735 | default: |
| 736 | lua_pushstring(L, ""); |
| 737 | break; |
| 738 | } |
| 739 | break; |
| 740 | |
| 741 | case SMP_T_SINT: |
| 742 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 743 | case SMP_T_IPV4: |
| 744 | case SMP_T_IPV6: |
| 745 | 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] | 746 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 747 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 748 | 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] | 749 | else |
| 750 | lua_pushstring(L, ""); |
| 751 | break; |
| 752 | default: |
| 753 | lua_pushstring(L, ""); |
| 754 | break; |
| 755 | } |
| 756 | return 1; |
| 757 | } |
| 758 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 759 | /* the following functions are used to convert an Lua type in a |
| 760 | * struct sample. This is useful to provide data from a converter |
| 761 | * to the LUA code. |
| 762 | */ |
| 763 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 764 | { |
| 765 | switch (lua_type(L, ud)) { |
| 766 | |
| 767 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 768 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 769 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 770 | break; |
| 771 | |
| 772 | |
| 773 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 774 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 775 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 776 | break; |
| 777 | |
| 778 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 779 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 780 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 781 | 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] | 782 | /* 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] | 783 | 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] | 784 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 785 | break; |
| 786 | |
| 787 | case LUA_TUSERDATA: |
| 788 | case LUA_TNIL: |
| 789 | case LUA_TTABLE: |
| 790 | case LUA_TFUNCTION: |
| 791 | case LUA_TTHREAD: |
| 792 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 793 | case LUA_TNONE: |
| 794 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 795 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 796 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 797 | break; |
| 798 | } |
| 799 | return 1; |
| 800 | } |
| 801 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 802 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 803 | * 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] | 804 | * 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] | 805 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 806 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 807 | * 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] | 808 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 809 | __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] | 810 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 811 | { |
| 812 | int min_arg; |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 813 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 814 | struct proxy *px; |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 815 | struct userlist *ul; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 816 | struct my_regex *reg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 817 | const char *msg = NULL; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 818 | char *sname, *pname, *err = NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 819 | |
| 820 | idx = 0; |
| 821 | min_arg = ARGM(mask); |
| 822 | mask >>= ARGM_BITS; |
| 823 | |
| 824 | while (1) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 825 | struct buffer tmp = BUF_NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 826 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 827 | /* Check for mandatory arguments. */ |
| 828 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 829 | if (idx < min_arg) { |
| 830 | |
| 831 | /* If miss other argument than the first one, we return an error. */ |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 832 | if (idx > 0) { |
| 833 | msg = "Mandatory argument expected"; |
| 834 | goto error; |
| 835 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 836 | |
| 837 | /* If first argument have a certain type, some default values |
| 838 | * may be used. See the function smp_resolve_args(). |
| 839 | */ |
| 840 | switch (mask & ARGT_MASK) { |
| 841 | |
| 842 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 843 | if (!(p->cap & PR_CAP_FE)) { |
| 844 | msg = "Mandatory argument expected"; |
| 845 | goto error; |
| 846 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 847 | argp[idx].data.prx = p; |
| 848 | argp[idx].type = ARGT_FE; |
| 849 | argp[idx+1].type = ARGT_STOP; |
| 850 | break; |
| 851 | |
| 852 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 853 | if (!(p->cap & PR_CAP_BE)) { |
| 854 | msg = "Mandatory argument expected"; |
| 855 | goto error; |
| 856 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 857 | argp[idx].data.prx = p; |
| 858 | argp[idx].type = ARGT_BE; |
| 859 | argp[idx+1].type = ARGT_STOP; |
| 860 | break; |
| 861 | |
| 862 | case ARGT_TAB: |
| 863 | argp[idx].data.prx = p; |
| 864 | argp[idx].type = ARGT_TAB; |
| 865 | argp[idx+1].type = ARGT_STOP; |
| 866 | break; |
| 867 | |
| 868 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 869 | msg = "Mandatory argument expected"; |
| 870 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 871 | break; |
| 872 | } |
| 873 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 874 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 875 | } |
| 876 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 877 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 878 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 879 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 880 | msg = "Last argument expected"; |
| 881 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 885 | argp[idx].type == ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 886 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 887 | } |
| 888 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 889 | /* Convert some argument types. All string in argp[] are for not |
| 890 | * duplicated yet. |
| 891 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 892 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 893 | case ARGT_SINT: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 894 | if (argp[idx].type != ARGT_SINT) { |
| 895 | msg = "integer expected"; |
| 896 | goto error; |
| 897 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 898 | argp[idx].type = ARGT_SINT; |
| 899 | break; |
| 900 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 901 | case ARGT_TIME: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 902 | if (argp[idx].type != ARGT_SINT) { |
| 903 | msg = "integer expected"; |
| 904 | goto error; |
| 905 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 906 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 907 | break; |
| 908 | |
| 909 | case ARGT_SIZE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 910 | if (argp[idx].type != ARGT_SINT) { |
| 911 | msg = "integer expected"; |
| 912 | goto error; |
| 913 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 914 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 915 | break; |
| 916 | |
| 917 | case ARGT_FE: |
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.prx = proxy_fe_by_name(argp[idx].data.str.area); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 923 | if (!argp[idx].data.prx) { |
| 924 | msg = "frontend doesn't exist"; |
| 925 | goto error; |
| 926 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 927 | argp[idx].type = ARGT_FE; |
| 928 | break; |
| 929 | |
| 930 | case ARGT_BE: |
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 | 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] | 936 | if (!argp[idx].data.prx) { |
| 937 | msg = "backend doesn't exist"; |
| 938 | goto error; |
| 939 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 940 | argp[idx].type = ARGT_BE; |
| 941 | break; |
| 942 | |
| 943 | case ARGT_TAB: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 944 | if (argp[idx].type != ARGT_STR) { |
| 945 | msg = "string expected"; |
| 946 | goto error; |
| 947 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 948 | 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] | 949 | if (!argp[idx].data.t) { |
| 950 | msg = "table doesn't exist"; |
| 951 | goto error; |
| 952 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 953 | argp[idx].type = ARGT_TAB; |
| 954 | break; |
| 955 | |
| 956 | case ARGT_SRV: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 957 | if (argp[idx].type != ARGT_STR) { |
| 958 | msg = "string expected"; |
| 959 | goto error; |
| 960 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 961 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 962 | if (sname) { |
| 963 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 964 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 965 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 966 | if (!px) { |
| 967 | msg = "backend doesn't exist"; |
| 968 | goto error; |
| 969 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 970 | } |
| 971 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 972 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 973 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 974 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 975 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 976 | if (!argp[idx].data.srv) { |
| 977 | msg = "server doesn't exist"; |
| 978 | goto error; |
| 979 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 980 | argp[idx].type = ARGT_SRV; |
| 981 | break; |
| 982 | |
| 983 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 984 | if (argp[idx].type != ARGT_STR) { |
| 985 | msg = "string expected"; |
| 986 | goto error; |
| 987 | } |
| 988 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 989 | msg = "invalid IPv4 address"; |
| 990 | goto error; |
| 991 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 992 | argp[idx].type = ARGT_IPV4; |
| 993 | break; |
| 994 | |
| 995 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 996 | if (argp[idx].type == ARGT_SINT) |
| 997 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 998 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 999 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 1000 | msg = "invalid IPv4 mask"; |
| 1001 | goto error; |
| 1002 | } |
| 1003 | } |
| 1004 | else { |
| 1005 | msg = "integer or string expected"; |
| 1006 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1007 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1008 | argp[idx].type = ARGT_MSK4; |
| 1009 | break; |
| 1010 | |
| 1011 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1012 | if (argp[idx].type != ARGT_STR) { |
| 1013 | msg = "string expected"; |
| 1014 | goto error; |
| 1015 | } |
| 1016 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 1017 | msg = "invalid IPv6 address"; |
| 1018 | goto error; |
| 1019 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1020 | argp[idx].type = ARGT_IPV6; |
| 1021 | break; |
| 1022 | |
| 1023 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1024 | if (argp[idx].type == ARGT_SINT) |
| 1025 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 1026 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1027 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 1028 | msg = "invalid IPv6 mask"; |
| 1029 | goto error; |
| 1030 | } |
| 1031 | } |
| 1032 | else { |
| 1033 | msg = "integer or string expected"; |
| 1034 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1035 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 1036 | argp[idx].type = ARGT_MSK6; |
| 1037 | break; |
| 1038 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 1039 | case ARGT_REG: |
| 1040 | if (argp[idx].type != ARGT_STR) { |
| 1041 | msg = "string expected"; |
| 1042 | goto error; |
| 1043 | } |
| 1044 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 1045 | if (!reg) { |
| 1046 | msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'", |
| 1047 | argp[idx].data.str.area, err); |
| 1048 | free(err); |
| 1049 | goto error; |
| 1050 | } |
| 1051 | argp[idx].type = ARGT_REG; |
| 1052 | argp[idx].data.reg = reg; |
| 1053 | break; |
| 1054 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1055 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1056 | if (argp[idx].type != ARGT_STR) { |
| 1057 | msg = "string expected"; |
| 1058 | goto error; |
| 1059 | } |
| 1060 | if (p->uri_auth && p->uri_auth->userlist && |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1061 | strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0) |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1062 | ul = p->uri_auth->userlist; |
| 1063 | else |
| 1064 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 1065 | |
| 1066 | if (!ul) { |
| 1067 | msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area); |
| 1068 | goto error; |
| 1069 | } |
| 1070 | argp[idx].type = ARGT_USR; |
| 1071 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1072 | break; |
| 1073 | |
| 1074 | case ARGT_STR: |
| 1075 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 1076 | msg = "unable to duplicate string arg"; |
| 1077 | goto error; |
| 1078 | } |
| 1079 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1080 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1081 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1082 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1083 | msg = "type not yet supported"; |
| 1084 | goto error; |
| 1085 | break; |
| 1086 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | /* Check for type of argument. */ |
| 1090 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1091 | msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 1092 | arg_type_names[(mask & ARGT_MASK)], |
| 1093 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 1094 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | /* Next argument. */ |
| 1098 | mask >>= ARGT_BITS; |
| 1099 | idx++; |
| 1100 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1101 | return 0; |
| 1102 | |
| 1103 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 1104 | free_args(argp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1105 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 1106 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1107 | } |
| 1108 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1109 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1110 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1111 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1112 | * |
| 1113 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1114 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 1115 | */ |
| 1116 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 1117 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1118 | struct hlua **hlua = lua_getextraspace(L); |
| 1119 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1120 | } |
| 1121 | static inline void hlua_sethlua(struct hlua *hlua) |
| 1122 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1123 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 1124 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1125 | } |
| 1126 | |
Willy Tarreau | 0b7b639 | 2022-06-19 17:39:33 +0200 | [diff] [blame] | 1127 | /* Will return a non-NULL string indicating the Lua call trace if the caller |
| 1128 | * currently is executing from within a Lua function. One line per entry will |
| 1129 | * be emitted, and each extra line will be prefixed with <pfx>. If a current |
| 1130 | * Lua function is not detected, NULL is returned. |
| 1131 | */ |
| 1132 | const char *hlua_show_current_location(const char *pfx) |
| 1133 | { |
| 1134 | lua_State *L; |
| 1135 | lua_Debug ar; |
| 1136 | |
| 1137 | /* global or per-thread stack initializing ? */ |
| 1138 | if (hlua_state_id != -1 && (L = hlua_states[hlua_state_id]) && lua_getstack(L, 0, &ar)) |
| 1139 | return hlua_traceback(L, pfx); |
| 1140 | |
| 1141 | /* per-thread stack running ? */ |
| 1142 | if (hlua_states[tid + 1] && (L = hlua_states[tid + 1]) && lua_getstack(L, 0, &ar)) |
| 1143 | return hlua_traceback(L, pfx); |
| 1144 | |
| 1145 | /* global stack running ? */ |
| 1146 | if (hlua_states[0] && (L = hlua_states[0]) && lua_getstack(L, 0, &ar)) |
| 1147 | return hlua_traceback(L, pfx); |
| 1148 | |
| 1149 | return NULL; |
| 1150 | } |
| 1151 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1152 | /* This function is used to send logs. It try to send on screen (stderr) |
| 1153 | * and on the default syslog server. |
| 1154 | */ |
| 1155 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 1156 | { |
| 1157 | struct tm tm; |
| 1158 | char *p; |
| 1159 | |
| 1160 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1161 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1162 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1163 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 1164 | /* Break the message if exceed the buffer size. */ |
| 1165 | *(p-4) = ' '; |
| 1166 | *(p-3) = '.'; |
| 1167 | *(p-2) = '.'; |
| 1168 | *(p-1) = '.'; |
| 1169 | break; |
| 1170 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1171 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1172 | *p = *msg; |
| 1173 | else |
| 1174 | *p = '.'; |
| 1175 | } |
| 1176 | *p = '\0'; |
| 1177 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1178 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1179 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 1180 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 1181 | return; |
| 1182 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 1183 | get_localtime(date.tv_sec, &tm); |
| 1184 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1185 | 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] | 1186 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1187 | fflush(stderr); |
| 1188 | } |
| 1189 | } |
| 1190 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1191 | /* This function just ensure that the yield will be always |
| 1192 | * returned with a timeout and permit to set some flags |
| 1193 | */ |
| 1194 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1195 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1196 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1197 | struct hlua *hlua; |
| 1198 | |
| 1199 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1200 | hlua = hlua_gethlua(L); |
| 1201 | if (!hlua) { |
| 1202 | return; |
| 1203 | } |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1204 | |
| 1205 | /* Set the wake timeout. If timeout is required, we set |
| 1206 | * the expiration time. |
| 1207 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1208 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1209 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1210 | hlua->flags |= flags; |
| 1211 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1212 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1213 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1214 | } |
| 1215 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1216 | /* This function initialises the Lua environment stored in the stream. |
| 1217 | * 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] | 1218 | * 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] | 1219 | * |
| 1220 | * This function is particular. it initialises a new Lua thread. If the |
| 1221 | * initialisation fails (example: out of memory error), the lua function |
| 1222 | * throws an error (longjmp). |
| 1223 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1224 | * 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] | 1225 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1226 | * threads appear, the safe environment set a lock to ensure only one |
| 1227 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1228 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1229 | * |
| 1230 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1231 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1232 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1233 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1234 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1235 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1236 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1237 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1238 | 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] | 1239 | { |
| 1240 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1241 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1242 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1243 | lua->wake_time = TICK_ETERNITY; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1244 | lua->state_id = state_id; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1245 | LIST_INIT(&lua->com); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1246 | if (!already_safe) { |
| 1247 | if (!SET_SAFE_LJMP_PARENT(lua)) { |
| 1248 | lua->Tref = LUA_REFNIL; |
| 1249 | return 0; |
| 1250 | } |
| 1251 | } |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1252 | lua->T = lua_newthread(hlua_states[state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1253 | if (!lua->T) { |
| 1254 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1255 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1256 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1257 | return 0; |
| 1258 | } |
| 1259 | hlua_sethlua(lua); |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1260 | lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1261 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1262 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1263 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1264 | return 1; |
| 1265 | } |
| 1266 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1267 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1268 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 1269 | * is not freed. |
| 1270 | */ |
| 1271 | void hlua_ctx_destroy(struct hlua *lua) |
| 1272 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1273 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1274 | return; |
| 1275 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1276 | if (!lua->T) |
| 1277 | goto end; |
| 1278 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1279 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1280 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1281 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1282 | if (!SET_SAFE_LJMP(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1283 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1284 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1285 | RESET_SAFE_LJMP(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1286 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1287 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1288 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1289 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1290 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1291 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1292 | * without error, we run the GC on the thread pointer. Its freed all |
| 1293 | * the unused memory. |
| 1294 | * If the thread is finnish with an error or is currently yielded, |
| 1295 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1296 | * so e run the GC on the main thread. |
| 1297 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1298 | * the garbage collection. |
| 1299 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1300 | if (lua->gc_count) { |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1301 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1302 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1303 | lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1304 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1305 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1306 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1307 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1308 | |
| 1309 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1310 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | /* This function is used to restore the Lua context when a coroutine |
| 1314 | * fails. This function copy the common memory between old coroutine |
| 1315 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1316 | * replaced by the new coroutine. |
| 1317 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1318 | * as string error message and it is copied in the new stack. |
| 1319 | */ |
| 1320 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1321 | { |
| 1322 | lua_State *T; |
| 1323 | int new_ref; |
| 1324 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1325 | /* New Lua coroutine. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1326 | T = lua_newthread(hlua_states[lua->state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1327 | if (!T) |
| 1328 | return 0; |
| 1329 | |
| 1330 | /* Copy last error message. */ |
| 1331 | if (keep_msg) |
| 1332 | lua_xmove(lua->T, T, 1); |
| 1333 | |
| 1334 | /* Copy data between the coroutines. */ |
| 1335 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1336 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1337 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1338 | |
| 1339 | /* Destroy old data. */ |
| 1340 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1341 | |
| 1342 | /* The thread is garbage collected by Lua. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1343 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1344 | |
| 1345 | /* Fill the struct with the new coroutine values. */ |
| 1346 | lua->Mref = new_ref; |
| 1347 | lua->T = T; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1348 | lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1349 | |
| 1350 | /* Set context. */ |
| 1351 | hlua_sethlua(lua); |
| 1352 | |
| 1353 | return 1; |
| 1354 | } |
| 1355 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1356 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1357 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1358 | struct hlua *hlua; |
| 1359 | |
| 1360 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1361 | hlua = hlua_gethlua(L); |
| 1362 | if (!hlua) |
| 1363 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1364 | |
| 1365 | /* Lua cannot yield when its returning from a function, |
| 1366 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1367 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1368 | */ |
| 1369 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1370 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1371 | return; |
| 1372 | } |
| 1373 | |
| 1374 | /* restore the interrupt condition. */ |
| 1375 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1376 | |
| 1377 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1378 | * If the state is not yieldable, trying yield causes an error. |
| 1379 | */ |
| 1380 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1381 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1382 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1383 | /* If we cannot yield, update the clock and check the timeout. */ |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 1384 | clock_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1385 | hlua->run_time += now_ms - hlua->start_time; |
| 1386 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1387 | lua_pushfstring(L, "execution timeout"); |
| 1388 | WILL_LJMP(lua_error(L)); |
| 1389 | } |
| 1390 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1391 | /* Update the start time. */ |
| 1392 | hlua->start_time = now_ms; |
| 1393 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1394 | /* Try to interrupt the process at the end of the current |
| 1395 | * unyieldable function. |
| 1396 | */ |
| 1397 | 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] | 1398 | } |
| 1399 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1400 | /* This function start or resumes the Lua stack execution. If the flag |
| 1401 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1402 | * The function return an error. |
| 1403 | * |
| 1404 | * The function can returns 4 values: |
| 1405 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1406 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1407 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1408 | * - 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] | 1409 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1410 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1411 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1412 | * 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] | 1413 | * LUA code. |
| 1414 | */ |
| 1415 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1416 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1417 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1418 | int nres; |
| 1419 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1420 | int ret; |
| 1421 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1422 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1423 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1424 | /* Initialise run time counter. */ |
| 1425 | if (!HLUA_IS_RUNNING(lua)) |
| 1426 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1427 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1428 | /* Lock the whole Lua execution. This lock must be before the |
| 1429 | * label "resume_execution". |
| 1430 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1431 | if (lua->state_id == 0) |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 1432 | lua_take_global_lock(); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1433 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1434 | resume_execution: |
| 1435 | |
| 1436 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1437 | * instructions. it is used for preventing infinite loops. |
| 1438 | */ |
| 1439 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1440 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1441 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1442 | HLUA_SET_RUN(lua); |
| 1443 | HLUA_CLR_CTRLYIELD(lua); |
| 1444 | HLUA_CLR_WAKERESWR(lua); |
| 1445 | HLUA_CLR_WAKEREQWR(lua); |
Christopher Faulet | 1f43a34 | 2021-08-04 17:58:21 +0200 | [diff] [blame] | 1446 | HLUA_CLR_NOYIELD(lua); |
| 1447 | if (!yield_allowed) |
| 1448 | HLUA_SET_NOYIELD(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1449 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1450 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1451 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1452 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1453 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1454 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1455 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1456 | 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] | 1457 | #else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1458 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1459 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1460 | switch (ret) { |
| 1461 | |
| 1462 | case LUA_OK: |
| 1463 | ret = HLUA_E_OK; |
| 1464 | break; |
| 1465 | |
| 1466 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1467 | /* Check if the execution timeout is expired. It it is the case, we |
| 1468 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1469 | */ |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 1470 | clock_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1471 | lua->run_time += now_ms - lua->start_time; |
| 1472 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1473 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1474 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1475 | break; |
| 1476 | } |
| 1477 | /* Process the forced yield. if the general yield is not allowed or |
| 1478 | * if no task were associated this the current Lua execution |
| 1479 | * coroutine, we resume the execution. Else we want to return in the |
| 1480 | * scheduler and we want to be waked up again, to continue the |
| 1481 | * current Lua execution. So we schedule our own task. |
| 1482 | */ |
| 1483 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1484 | if (!yield_allowed || !lua->task) |
| 1485 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1486 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1487 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1488 | if (!yield_allowed) { |
| 1489 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1490 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1491 | break; |
| 1492 | } |
| 1493 | ret = HLUA_E_AGAIN; |
| 1494 | break; |
| 1495 | |
| 1496 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1497 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1498 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1499 | * because the errors ares the only one mean to return immediately |
| 1500 | * from and lua execution. |
| 1501 | */ |
| 1502 | if (lua->flags & HLUA_EXIT) { |
| 1503 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1504 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1505 | break; |
| 1506 | } |
| 1507 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1508 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1509 | if (!lua_checkstack(lua->T, 1)) { |
| 1510 | ret = HLUA_E_ERR; |
| 1511 | break; |
| 1512 | } |
| 1513 | msg = lua_tostring(lua->T, -1); |
| 1514 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1515 | lua_pop(lua->T, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 1516 | trace = hlua_traceback(lua->T, ", "); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1517 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1518 | 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] | 1519 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1520 | 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] | 1521 | ret = HLUA_E_ERRMSG; |
| 1522 | break; |
| 1523 | |
| 1524 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1525 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1526 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1527 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1528 | break; |
| 1529 | |
| 1530 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1531 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1532 | if (!lua_checkstack(lua->T, 1)) { |
| 1533 | ret = HLUA_E_ERR; |
| 1534 | break; |
| 1535 | } |
| 1536 | msg = lua_tostring(lua->T, -1); |
| 1537 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1538 | lua_pop(lua->T, 1); |
| 1539 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1540 | 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] | 1541 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1542 | 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] | 1543 | ret = HLUA_E_ERRMSG; |
| 1544 | break; |
| 1545 | |
| 1546 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1547 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1548 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1549 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1550 | break; |
| 1551 | } |
| 1552 | |
| 1553 | switch (ret) { |
| 1554 | case HLUA_E_AGAIN: |
| 1555 | break; |
| 1556 | |
| 1557 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1558 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1559 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1560 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1561 | break; |
| 1562 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1563 | case HLUA_E_ETMOUT: |
| 1564 | case HLUA_E_NOMEM: |
| 1565 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1566 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1567 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1568 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1569 | hlua_ctx_renew(lua, 0); |
| 1570 | break; |
| 1571 | |
| 1572 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1573 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1574 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1575 | break; |
| 1576 | } |
| 1577 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1578 | /* This is the main exit point, remove the Lua lock. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1579 | if (lua->state_id == 0) |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 1580 | lua_drop_global_lock(); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1581 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1582 | return ret; |
| 1583 | } |
| 1584 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1585 | /* This function exit the current code. */ |
| 1586 | __LJMP static int hlua_done(lua_State *L) |
| 1587 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1588 | struct hlua *hlua; |
| 1589 | |
| 1590 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1591 | hlua = hlua_gethlua(L); |
| 1592 | if (!hlua) |
| 1593 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1594 | |
| 1595 | hlua->flags |= HLUA_EXIT; |
| 1596 | WILL_LJMP(lua_error(L)); |
| 1597 | |
| 1598 | return 0; |
| 1599 | } |
| 1600 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1601 | /* This function is an LUA binding. It provides a function |
| 1602 | * for deleting ACL from a referenced ACL file. |
| 1603 | */ |
| 1604 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1605 | { |
| 1606 | const char *name; |
| 1607 | const char *key; |
| 1608 | struct pat_ref *ref; |
| 1609 | |
| 1610 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1611 | |
| 1612 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1613 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1614 | |
| 1615 | ref = pat_ref_lookup(name); |
| 1616 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1617 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1618 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1619 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1620 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1621 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1622 | return 0; |
| 1623 | } |
| 1624 | |
| 1625 | /* This function is an LUA binding. It provides a function |
| 1626 | * for deleting map entry from a referenced map file. |
| 1627 | */ |
| 1628 | static int hlua_del_map(lua_State *L) |
| 1629 | { |
| 1630 | const char *name; |
| 1631 | const char *key; |
| 1632 | struct pat_ref *ref; |
| 1633 | |
| 1634 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1635 | |
| 1636 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1637 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1638 | |
| 1639 | ref = pat_ref_lookup(name); |
| 1640 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1641 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1642 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1643 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1644 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1645 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1646 | return 0; |
| 1647 | } |
| 1648 | |
| 1649 | /* This function is an LUA binding. It provides a function |
| 1650 | * for adding ACL pattern from a referenced ACL file. |
| 1651 | */ |
| 1652 | static int hlua_add_acl(lua_State *L) |
| 1653 | { |
| 1654 | const char *name; |
| 1655 | const char *key; |
| 1656 | struct pat_ref *ref; |
| 1657 | |
| 1658 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1659 | |
| 1660 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1661 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1662 | |
| 1663 | ref = pat_ref_lookup(name); |
| 1664 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1665 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1666 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1667 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1668 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1669 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1670 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1671 | return 0; |
| 1672 | } |
| 1673 | |
| 1674 | /* This function is an LUA binding. It provides a function |
| 1675 | * for setting map pattern and sample from a referenced map |
| 1676 | * file. |
| 1677 | */ |
| 1678 | static int hlua_set_map(lua_State *L) |
| 1679 | { |
| 1680 | const char *name; |
| 1681 | const char *key; |
| 1682 | const char *value; |
| 1683 | struct pat_ref *ref; |
| 1684 | |
| 1685 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1686 | |
| 1687 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1688 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1689 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1690 | |
| 1691 | ref = pat_ref_lookup(name); |
| 1692 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1693 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1694 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1695 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1696 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1697 | pat_ref_set(ref, key, value, NULL); |
| 1698 | else |
| 1699 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1700 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1701 | return 0; |
| 1702 | } |
| 1703 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1704 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1705 | * 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] | 1706 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1707 | * the name of the object (_G[<name>] = <metable> ). |
| 1708 | * |
| 1709 | * A metable is a table that modify the standard behavior of a standard |
| 1710 | * access to the associated data. The entries of this new metatable are |
| 1711 | * defined as is: |
| 1712 | * |
| 1713 | * http://lua-users.org/wiki/MetatableEvents |
| 1714 | * |
| 1715 | * __index |
| 1716 | * |
| 1717 | * we access an absent field in a table, the result is nil. This is |
| 1718 | * true, but it is not the whole truth. Actually, such access triggers |
| 1719 | * the interpreter to look for an __index metamethod: If there is no |
| 1720 | * such method, as usually happens, then the access results in nil; |
| 1721 | * otherwise, the metamethod will provide the result. |
| 1722 | * |
| 1723 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1724 | * the key does not appear in the table, but the metatable has an __index |
| 1725 | * property: |
| 1726 | * |
| 1727 | * - if the value is a function, the function is called, passing in the |
| 1728 | * table and the key; the return value of that function is returned as |
| 1729 | * the result. |
| 1730 | * |
| 1731 | * - if the value is another table, the value of the key in that table is |
| 1732 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1733 | * table's metatable has an __index property, then it continues on up) |
| 1734 | * |
| 1735 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1736 | * |
| 1737 | * http://www.lua.org/pil/13.4.1.html |
| 1738 | * |
| 1739 | * __newindex |
| 1740 | * |
| 1741 | * Like __index, but control property assignment. |
| 1742 | * |
| 1743 | * __mode - Control weak references. A string value with one or both |
| 1744 | * of the characters 'k' and 'v' which specifies that the the |
| 1745 | * keys and/or values in the table are weak references. |
| 1746 | * |
| 1747 | * __call - Treat a table like a function. When a table is followed by |
| 1748 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1749 | * a __call key pointing to a function, that function is invoked |
| 1750 | * (passing any specified arguments) and the return value is |
| 1751 | * returned. |
| 1752 | * |
| 1753 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1754 | * called, if the metatable for myTable has a __metatable |
| 1755 | * key, the value of that key is returned instead of the |
| 1756 | * actual metatable. |
| 1757 | * |
| 1758 | * __tostring - Control string representation. When the builtin |
| 1759 | * "tostring( myTable )" function is called, if the metatable |
| 1760 | * for myTable has a __tostring property set to a function, |
| 1761 | * that function is invoked (passing myTable to it) and the |
| 1762 | * return value is used as the string representation. |
| 1763 | * |
| 1764 | * __len - Control table length. When the table length is requested using |
| 1765 | * the length operator ( '#' ), if the metatable for myTable has |
| 1766 | * a __len key pointing to a function, that function is invoked |
| 1767 | * (passing myTable to it) and the return value used as the value |
| 1768 | * of "#myTable". |
| 1769 | * |
| 1770 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1771 | * collected, if the metatable has a __gc field pointing to a |
| 1772 | * function, that function is first invoked, passing the userdata |
| 1773 | * to it. The __gc metamethod is not called for tables. |
| 1774 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1775 | * |
| 1776 | * Special metamethods for redefining standard operators: |
| 1777 | * http://www.lua.org/pil/13.1.html |
| 1778 | * |
| 1779 | * __add "+" |
| 1780 | * __sub "-" |
| 1781 | * __mul "*" |
| 1782 | * __div "/" |
| 1783 | * __unm "!" |
| 1784 | * __pow "^" |
| 1785 | * __concat ".." |
| 1786 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1787 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1788 | * http://www.lua.org/pil/13.2.html |
| 1789 | * |
| 1790 | * __eq "==" |
| 1791 | * __lt "<" |
| 1792 | * __le "<=" |
| 1793 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1794 | |
| 1795 | /* |
| 1796 | * |
| 1797 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1798 | * Class Map |
| 1799 | * |
| 1800 | * |
| 1801 | */ |
| 1802 | |
| 1803 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1804 | * a class session, otherwise it throws an error. |
| 1805 | */ |
| 1806 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1807 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1808 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1809 | } |
| 1810 | |
| 1811 | /* This function is the map constructor. It don't need |
| 1812 | * the class Map object. It creates and return a new Map |
| 1813 | * object. It must be called only during "body" or "init" |
| 1814 | * context because it process some filesystem accesses. |
| 1815 | */ |
| 1816 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1817 | { |
| 1818 | const char *fn; |
| 1819 | int match = PAT_MATCH_STR; |
| 1820 | struct sample_conv conv; |
| 1821 | const char *file = ""; |
| 1822 | int line = 0; |
| 1823 | lua_Debug ar; |
| 1824 | char *err = NULL; |
| 1825 | struct arg args[2]; |
| 1826 | |
| 1827 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1828 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1829 | |
| 1830 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1831 | |
| 1832 | if (lua_gettop(L) >= 2) { |
| 1833 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1834 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1835 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1836 | } |
| 1837 | |
| 1838 | /* Get Lua filename and line number. */ |
| 1839 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1840 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1841 | if (ar.currentline > 0) { /* is there info? */ |
| 1842 | file = ar.short_src; |
| 1843 | line = ar.currentline; |
| 1844 | } |
| 1845 | } |
| 1846 | |
| 1847 | /* fill fake sample_conv struct. */ |
| 1848 | conv.kw = ""; /* unused. */ |
| 1849 | conv.process = NULL; /* unused. */ |
| 1850 | conv.arg_mask = 0; /* unused. */ |
| 1851 | conv.val_args = NULL; /* unused. */ |
| 1852 | conv.out_type = SMP_T_STR; |
| 1853 | conv.private = (void *)(long)match; |
| 1854 | switch (match) { |
| 1855 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1856 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1857 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1858 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1859 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1860 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1861 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1862 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1863 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1864 | default: |
| 1865 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1866 | } |
| 1867 | |
| 1868 | /* fill fake args. */ |
| 1869 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1870 | args[0].data.str.area = strdup(fn); |
| 1871 | args[0].data.str.data = strlen(fn); |
| 1872 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1873 | args[1].type = ARGT_STOP; |
| 1874 | |
| 1875 | /* load the map. */ |
| 1876 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1877 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1878 | * free the err variable. |
| 1879 | */ |
| 1880 | luaL_where(L, 1); |
| 1881 | lua_pushfstring(L, "'new': %s.", err); |
| 1882 | lua_concat(L, 2); |
| 1883 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1884 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1885 | WILL_LJMP(lua_error(L)); |
| 1886 | } |
| 1887 | |
| 1888 | /* create the lua object. */ |
| 1889 | lua_newtable(L); |
| 1890 | lua_pushlightuserdata(L, args[0].data.map); |
| 1891 | lua_rawseti(L, -2, 0); |
| 1892 | |
| 1893 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1894 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1895 | lua_setmetatable(L, -2); |
| 1896 | |
| 1897 | |
| 1898 | return 1; |
| 1899 | } |
| 1900 | |
| 1901 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1902 | { |
| 1903 | struct map_descriptor *desc; |
| 1904 | struct pattern *pat; |
| 1905 | struct sample smp; |
| 1906 | |
| 1907 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1908 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1909 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1910 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1911 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1912 | } |
| 1913 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1914 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1915 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1916 | 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] | 1917 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1918 | } |
| 1919 | |
| 1920 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1921 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1922 | if (str) |
| 1923 | lua_pushstring(L, ""); |
| 1924 | else |
| 1925 | lua_pushnil(L); |
| 1926 | return 1; |
| 1927 | } |
| 1928 | |
| 1929 | /* 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] | 1930 | 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] | 1931 | return 1; |
| 1932 | } |
| 1933 | |
| 1934 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1935 | { |
| 1936 | return _hlua_map_lookup(L, 0); |
| 1937 | } |
| 1938 | |
| 1939 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1940 | { |
| 1941 | return _hlua_map_lookup(L, 1); |
| 1942 | } |
| 1943 | |
| 1944 | /* |
| 1945 | * |
| 1946 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1947 | * Class Socket |
| 1948 | * |
| 1949 | * |
| 1950 | */ |
| 1951 | |
| 1952 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1953 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1954 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1955 | } |
| 1956 | |
| 1957 | /* 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] | 1958 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1959 | * received. |
| 1960 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1961 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1962 | { |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1963 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 1964 | struct stconn *sc = appctx_sc(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1965 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1966 | if (ctx->die) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 1967 | sc_shutw(sc); |
| 1968 | sc_shutr(sc); |
| 1969 | sc_ic(sc)->flags |= CF_READ_NULL; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1970 | notification_wake(&ctx->wake_on_read); |
| 1971 | notification_wake(&ctx->wake_on_write); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 1972 | stream_shutdown(__sc_strm(sc), SF_ERR_KILLED); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1973 | } |
| 1974 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1975 | /* If we can't write, wakeup the pending write signals. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 1976 | if (channel_output_closed(sc_ic(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1977 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1978 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1979 | /* If we can't read, wakeup the pending read signals. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 1980 | if (channel_input_closed(sc_oc(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1981 | notification_wake(&ctx->wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1982 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1983 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1984 | * to be notified whenever the connection completes. |
| 1985 | */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 1986 | if (sc_opposite(sc)->state < SC_ST_EST) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 1987 | applet_need_more_data(appctx); |
Willy Tarreau | b23edc8 | 2022-05-24 16:49:03 +0200 | [diff] [blame] | 1988 | se_need_remote_conn(appctx->sedesc); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 1989 | applet_have_more_data(appctx); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1990 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1991 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1992 | |
| 1993 | /* This function is called after the connect. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1994 | ctx->connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1995 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1996 | /* Wake the tasks which wants to write if the buffer have available space. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 1997 | if (channel_may_recv(sc_ic(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1998 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1999 | |
| 2000 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2001 | if (!channel_is_empty(sc_oc(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2002 | notification_wake(&ctx->wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2003 | |
| 2004 | /* Some data were injected in the buffer, notify the stream |
| 2005 | * interface. |
| 2006 | */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2007 | if (!channel_is_empty(sc_ic(sc))) |
| 2008 | sc_update(sc); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2009 | |
| 2010 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2011 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2012 | */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2013 | if (notification_registered(&ctx->wake_on_write)) |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 2014 | applet_have_more_data(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2015 | } |
| 2016 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2017 | static int hlua_socket_init(struct appctx *appctx) |
| 2018 | { |
| 2019 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
| 2020 | struct stream *s; |
| 2021 | |
| 2022 | if (appctx_finalize_startup(appctx, socket_proxy, &BUF_NULL) == -1) |
| 2023 | goto error; |
| 2024 | |
| 2025 | s = appctx_strm(appctx); |
| 2026 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 2027 | /* Configure "right" stream connector. This stconn is used to connect |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2028 | * and retrieve data from the server. The connection is initialized |
| 2029 | * with the "struct server". |
| 2030 | */ |
Willy Tarreau | 74568cf | 2022-05-27 09:03:30 +0200 | [diff] [blame] | 2031 | sc_set_state(s->scb, SC_ST_ASS); |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2032 | |
| 2033 | /* Force destination server. */ |
| 2034 | s->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
| 2035 | s->target = &socket_tcp->obj_type; |
| 2036 | |
| 2037 | ctx->appctx = appctx; |
| 2038 | return 0; |
| 2039 | |
| 2040 | error: |
| 2041 | return -1; |
| 2042 | } |
| 2043 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2044 | /* This function is called when the "struct stream" is destroyed. |
| 2045 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2046 | * Wake all the pending signals. |
| 2047 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 2048 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2049 | { |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2050 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2051 | struct xref *peer; |
| 2052 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2053 | /* Remove my link in the original objects. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2054 | peer = xref_get_peer_and_lock(&ctx->xref); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2055 | if (peer) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2056 | xref_disconnect(&ctx->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2057 | |
| 2058 | /* Wake all the task waiting for me. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2059 | notification_wake(&ctx->wake_on_read); |
| 2060 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2061 | } |
| 2062 | |
| 2063 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2064 | * uses this object. If the stream does not exists, just quit. |
| 2065 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2066 | * pending signal can rest in the read and write lists. destroy |
| 2067 | * it. |
| 2068 | */ |
| 2069 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 2070 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2071 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2072 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2073 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2074 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2075 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 2076 | |
| 2077 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2078 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2079 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2080 | return 0; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2081 | |
| 2082 | ctx = container_of(peer, struct hlua_csk_ctx, xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2083 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2084 | /* Set the flag which destroy the session. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2085 | ctx->die = 1; |
| 2086 | appctx_wakeup(ctx->appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2087 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2088 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2089 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2090 | return 0; |
| 2091 | } |
| 2092 | |
| 2093 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2094 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2095 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2096 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2097 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2098 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2099 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2100 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2101 | struct hlua *hlua; |
| 2102 | |
| 2103 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2104 | hlua = hlua_gethlua(L); |
| 2105 | if (!hlua) |
| 2106 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2107 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2108 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2109 | |
| 2110 | /* Check if we run on the same thread than the xreator thread. |
| 2111 | * We cannot access to the socket if the thread is different. |
| 2112 | */ |
| 2113 | if (socket->tid != tid) |
| 2114 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2115 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2116 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2117 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2118 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2119 | |
| 2120 | hlua->gc_count--; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2121 | ctx = container_of(peer, struct hlua_csk_ctx, xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2122 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2123 | /* Set the flag which destroy the session. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2124 | ctx->die = 1; |
| 2125 | appctx_wakeup(ctx->appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2126 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2127 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2128 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2129 | return 0; |
| 2130 | } |
| 2131 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2132 | /* The close function calls close_helper. |
| 2133 | */ |
| 2134 | __LJMP static int hlua_socket_close(lua_State *L) |
| 2135 | { |
| 2136 | MAY_LJMP(check_args(L, 1, "close")); |
| 2137 | return hlua_socket_close_helper(L); |
| 2138 | } |
| 2139 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2140 | /* This Lua function assumes that the stack contain three parameters. |
| 2141 | * 1 - USERDATA containing a struct socket |
| 2142 | * 2 - INTEGER with values of the macro defined below |
| 2143 | * If the integer is -1, we must read at most one line. |
| 2144 | * If the integer is -2, we ust read all the data until the |
| 2145 | * end of the stream. |
| 2146 | * If the integer is positive value, we must read a number of |
| 2147 | * bytes corresponding to this value. |
| 2148 | */ |
| 2149 | #define HLSR_READ_LINE (-1) |
| 2150 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2151 | __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] | 2152 | { |
| 2153 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2154 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2155 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2156 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2157 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2158 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2159 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2160 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2161 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2162 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2163 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2164 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 2165 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2166 | struct stream *s; |
| 2167 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2168 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2169 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2170 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2171 | hlua = hlua_gethlua(L); |
| 2172 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2173 | /* Check if this lua stack is schedulable. */ |
| 2174 | if (!hlua || !hlua->task) |
| 2175 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 2176 | "'frontend', 'backend' or 'task'")); |
| 2177 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2178 | /* Check if we run on the same thread than the xreator thread. |
| 2179 | * We cannot access to the socket if the thread is different. |
| 2180 | */ |
| 2181 | if (socket->tid != tid) |
| 2182 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2183 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2184 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2185 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2186 | if (!peer) |
| 2187 | goto no_peer; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2188 | |
| 2189 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2190 | appctx = csk_ctx->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2191 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2192 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2193 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2194 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2195 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2196 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2197 | if (nblk < 0) /* Connection close. */ |
| 2198 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2199 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2200 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2201 | |
| 2202 | /* remove final \r\n. */ |
| 2203 | if (nblk == 1) { |
| 2204 | if (blk1[len1-1] == '\n') { |
| 2205 | len1--; |
| 2206 | skip_at_end++; |
| 2207 | if (blk1[len1-1] == '\r') { |
| 2208 | len1--; |
| 2209 | skip_at_end++; |
| 2210 | } |
| 2211 | } |
| 2212 | } |
| 2213 | else { |
| 2214 | if (blk2[len2-1] == '\n') { |
| 2215 | len2--; |
| 2216 | skip_at_end++; |
| 2217 | if (blk2[len2-1] == '\r') { |
| 2218 | len2--; |
| 2219 | skip_at_end++; |
| 2220 | } |
| 2221 | } |
| 2222 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2223 | } |
| 2224 | |
| 2225 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2226 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2227 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2228 | if (nblk < 0) /* Connection close. */ |
| 2229 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2230 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2231 | goto connection_empty; |
| 2232 | } |
| 2233 | |
| 2234 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2235 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2236 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2237 | if (nblk < 0) /* Connection close. */ |
| 2238 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2239 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2240 | goto connection_empty; |
| 2241 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2242 | missing_bytes = wanted - socket->b.n; |
| 2243 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2244 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2245 | len1 = missing_bytes; |
| 2246 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2247 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2248 | } |
| 2249 | |
| 2250 | len = len1; |
| 2251 | |
| 2252 | luaL_addlstring(&socket->b, blk1, len1); |
| 2253 | if (nblk == 2) { |
| 2254 | len += len2; |
| 2255 | luaL_addlstring(&socket->b, blk2, len2); |
| 2256 | } |
| 2257 | |
| 2258 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2259 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2260 | |
| 2261 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2262 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2263 | |
| 2264 | /* If the pattern reclaim to read all the data |
| 2265 | * in the connection, got out. |
| 2266 | */ |
| 2267 | if (wanted == HLSR_READ_ALL) |
| 2268 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2269 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2270 | goto connection_empty; |
| 2271 | |
| 2272 | /* Return result. */ |
| 2273 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2274 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2275 | return 1; |
| 2276 | |
| 2277 | connection_closed: |
| 2278 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2279 | xref_unlock(&socket->xref, peer); |
| 2280 | |
| 2281 | no_peer: |
| 2282 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2283 | /* If the buffer containds data. */ |
| 2284 | if (socket->b.n > 0) { |
| 2285 | luaL_pushresult(&socket->b); |
| 2286 | return 1; |
| 2287 | } |
| 2288 | lua_pushnil(L); |
| 2289 | lua_pushstring(L, "connection closed."); |
| 2290 | return 2; |
| 2291 | |
| 2292 | connection_empty: |
| 2293 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2294 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_read, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2295 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2296 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2297 | } |
| 2298 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2299 | 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] | 2300 | return 0; |
| 2301 | } |
| 2302 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2303 | /* This Lua function gets two parameters. The first one can be string |
| 2304 | * or a number. If the string is "*l", the user requires one line. If |
| 2305 | * 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] | 2306 | * If the value is a number, the user require a number of bytes equal |
| 2307 | * to the value. The default value is "*l" (a line). |
| 2308 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2309 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2310 | * integer takes this values: |
| 2311 | * -1 : read a line |
| 2312 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2313 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2314 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2315 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2316 | * concatenated with the read data. |
| 2317 | */ |
| 2318 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2319 | { |
| 2320 | int wanted = HLSR_READ_LINE; |
| 2321 | const char *pattern; |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2322 | int lastarg, type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2323 | char *error; |
| 2324 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2325 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2326 | |
| 2327 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2328 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2329 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2330 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
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 | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2338 | /* check for pattern. */ |
| 2339 | if (lua_gettop(L) >= 2) { |
| 2340 | type = lua_type(L, 2); |
| 2341 | if (type == LUA_TSTRING) { |
| 2342 | pattern = lua_tostring(L, 2); |
| 2343 | if (strcmp(pattern, "*a") == 0) |
| 2344 | wanted = HLSR_READ_ALL; |
| 2345 | else if (strcmp(pattern, "*l") == 0) |
| 2346 | wanted = HLSR_READ_LINE; |
| 2347 | else { |
| 2348 | wanted = strtoll(pattern, &error, 10); |
| 2349 | if (*error != '\0') |
| 2350 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2351 | } |
| 2352 | } |
| 2353 | else if (type == LUA_TNUMBER) { |
| 2354 | wanted = lua_tointeger(L, 2); |
| 2355 | if (wanted < 0) |
| 2356 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2357 | } |
| 2358 | } |
| 2359 | |
| 2360 | /* Set pattern. */ |
| 2361 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2362 | |
| 2363 | /* Check if we would replace the top by itself. */ |
| 2364 | if (lua_gettop(L) != 2) |
| 2365 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2366 | |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2367 | /* Save index of the top of the stack because since buffers are used, it |
| 2368 | * may change |
| 2369 | */ |
| 2370 | lastarg = lua_gettop(L); |
| 2371 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2372 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2373 | luaL_buffinit(L, &socket->b); |
| 2374 | |
| 2375 | /* Check prefix. */ |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2376 | if (lastarg >= 3) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2377 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2378 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2379 | pattern = lua_tolstring(L, 3, &len); |
| 2380 | luaL_addlstring(&socket->b, pattern, len); |
| 2381 | } |
| 2382 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2383 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2384 | } |
| 2385 | |
| 2386 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2387 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2388 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2389 | 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] | 2390 | { |
| 2391 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2392 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2393 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2394 | struct appctx *appctx; |
| 2395 | size_t buf_len; |
| 2396 | const char *buf; |
| 2397 | int len; |
| 2398 | int send_len; |
| 2399 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2400 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2401 | struct stream *s; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2402 | struct stconn *sc; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2403 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2404 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2405 | hlua = hlua_gethlua(L); |
| 2406 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2407 | /* Check if this lua stack is schedulable. */ |
| 2408 | if (!hlua || !hlua->task) |
| 2409 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2410 | "'frontend', 'backend' or 'task'")); |
| 2411 | |
| 2412 | /* Get object */ |
| 2413 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2414 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2415 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2416 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2417 | /* Check if we run on the same thread than the xreator thread. |
| 2418 | * We cannot access to the socket if the thread is different. |
| 2419 | */ |
| 2420 | if (socket->tid != tid) |
| 2421 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2422 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2423 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2424 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2425 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2426 | lua_pushinteger(L, -1); |
| 2427 | return 1; |
| 2428 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2429 | |
| 2430 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2431 | appctx = csk_ctx->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2432 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2433 | s = __sc_strm(sc); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2434 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2435 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2436 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2437 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2438 | lua_pushinteger(L, -1); |
| 2439 | return 1; |
| 2440 | } |
| 2441 | |
| 2442 | /* Update the input buffer data. */ |
| 2443 | buf += sent; |
| 2444 | send_len = buf_len - sent; |
| 2445 | |
| 2446 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2447 | if (sent >= buf_len) { |
| 2448 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2449 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2450 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2451 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2452 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2453 | * the request buffer if its not required. |
| 2454 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2455 | if (s->req.buf.size == 0) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2456 | if (!sc_alloc_ibuf(sc, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2457 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2458 | } |
| 2459 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2460 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2461 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2462 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2463 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2464 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2465 | |
| 2466 | /* send data */ |
| 2467 | if (len < send_len) |
| 2468 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2469 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2470 | |
| 2471 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2472 | * the data" (-2) are not expected because the available length |
| 2473 | * is tested. |
| 2474 | * Other unknown error are also not expected. |
| 2475 | */ |
| 2476 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2477 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2478 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2479 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2480 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2481 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2482 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2483 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2484 | return 1; |
| 2485 | } |
| 2486 | |
| 2487 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2488 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2489 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2490 | s->req.rex = TICK_ETERNITY; |
| 2491 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2492 | |
| 2493 | /* Update length sent. */ |
| 2494 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2495 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2496 | |
| 2497 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2498 | if (sent + len >= buf_len) { |
| 2499 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2500 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2501 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2502 | |
| 2503 | hlua_socket_write_yield_return: |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2504 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2505 | xref_unlock(&socket->xref, peer); |
| 2506 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2507 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2508 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2509 | 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] | 2510 | return 0; |
| 2511 | } |
| 2512 | |
| 2513 | /* This function initiate the send of data. It just check the input |
| 2514 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2515 | * 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] | 2516 | * "hlua_socket_write_yield" that can yield. |
| 2517 | * |
| 2518 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2519 | * the associated object. The second is a string buffer. The third is |
| 2520 | * a facultative integer that represents where is the buffer position |
| 2521 | * of the start of the data that can send. The first byte is the |
| 2522 | * position "1". The default value is "1". The fourth argument is a |
| 2523 | * facultative integer that represents where is the buffer position |
| 2524 | * of the end of the data that can send. The default is the last byte. |
| 2525 | */ |
| 2526 | static int hlua_socket_send(struct lua_State *L) |
| 2527 | { |
| 2528 | int i; |
| 2529 | int j; |
| 2530 | const char *buf; |
| 2531 | size_t buf_len; |
| 2532 | |
| 2533 | /* Check number of arguments. */ |
| 2534 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2535 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2536 | |
| 2537 | /* Get the string. */ |
| 2538 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2539 | |
| 2540 | /* Get and check j. */ |
| 2541 | if (lua_gettop(L) == 4) { |
| 2542 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2543 | if (j < 0) |
| 2544 | j = buf_len + j + 1; |
| 2545 | if (j > buf_len) |
| 2546 | j = buf_len + 1; |
| 2547 | lua_pop(L, 1); |
| 2548 | } |
| 2549 | else |
| 2550 | j = buf_len; |
| 2551 | |
| 2552 | /* Get and check i. */ |
| 2553 | if (lua_gettop(L) == 3) { |
| 2554 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2555 | if (i < 0) |
| 2556 | i = buf_len + i + 1; |
| 2557 | if (i > buf_len) |
| 2558 | i = buf_len + 1; |
| 2559 | lua_pop(L, 1); |
| 2560 | } else |
| 2561 | i = 1; |
| 2562 | |
| 2563 | /* Check bth i and j. */ |
| 2564 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2565 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2566 | return 1; |
| 2567 | } |
| 2568 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2569 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2570 | return 1; |
| 2571 | } |
| 2572 | if (i == 0) |
| 2573 | i = 1; |
| 2574 | if (j == 0) |
| 2575 | j = 1; |
| 2576 | |
| 2577 | /* Pop the string. */ |
| 2578 | lua_pop(L, 1); |
| 2579 | |
| 2580 | /* Update the buffer length. */ |
| 2581 | buf += i - 1; |
| 2582 | buf_len = j - i + 1; |
| 2583 | lua_pushlstring(L, buf, buf_len); |
| 2584 | |
| 2585 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2586 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2587 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2588 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2589 | } |
| 2590 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2591 | #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] | 2592 | __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] | 2593 | { |
| 2594 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2595 | int ret; |
| 2596 | int len; |
| 2597 | char *p; |
| 2598 | |
| 2599 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2600 | if (ret <= 0) { |
| 2601 | lua_pushnil(L); |
| 2602 | return 1; |
| 2603 | } |
| 2604 | |
| 2605 | if (ret == AF_UNIX) { |
| 2606 | lua_pushstring(L, buffer+1); |
| 2607 | return 1; |
| 2608 | } |
| 2609 | else if (ret == AF_INET6) { |
| 2610 | buffer[0] = '['; |
| 2611 | len = strlen(buffer); |
| 2612 | buffer[len] = ']'; |
| 2613 | len++; |
| 2614 | buffer[len] = ':'; |
| 2615 | len++; |
| 2616 | p = buffer; |
| 2617 | } |
| 2618 | else if (ret == AF_INET) { |
| 2619 | p = buffer + 1; |
| 2620 | len = strlen(p); |
| 2621 | p[len] = ':'; |
| 2622 | len++; |
| 2623 | } |
| 2624 | else { |
| 2625 | lua_pushnil(L); |
| 2626 | return 1; |
| 2627 | } |
| 2628 | |
| 2629 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2630 | lua_pushnil(L); |
| 2631 | return 1; |
| 2632 | } |
| 2633 | |
| 2634 | lua_pushstring(L, p); |
| 2635 | return 1; |
| 2636 | } |
| 2637 | |
| 2638 | /* Returns information about the peer of the connection. */ |
| 2639 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2640 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2641 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2642 | struct xref *peer; |
| 2643 | struct appctx *appctx; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2644 | struct stconn *sc; |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2645 | const struct sockaddr_storage *dst; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2646 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2647 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2648 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2649 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2650 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2651 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2652 | /* Check if we run on the same thread than the xreator thread. |
| 2653 | * We cannot access to the socket if the thread is different. |
| 2654 | */ |
| 2655 | if (socket->tid != tid) |
| 2656 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2657 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2658 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2659 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2660 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2661 | lua_pushnil(L); |
| 2662 | return 1; |
| 2663 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2664 | |
| 2665 | appctx = container_of(peer, struct hlua_csk_ctx, xref)->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2666 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2667 | dst = sc_dst(sc_opposite(sc)); |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2668 | if (!dst) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2669 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2670 | lua_pushnil(L); |
| 2671 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2672 | } |
| 2673 | |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2674 | ret = MAY_LJMP(hlua_socket_info(L, dst)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2675 | xref_unlock(&socket->xref, peer); |
| 2676 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2677 | } |
| 2678 | |
| 2679 | /* Returns information about my connection side. */ |
| 2680 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2681 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2682 | struct hlua_socket *socket; |
| 2683 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2684 | struct appctx *appctx; |
| 2685 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2686 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2687 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2688 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2689 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2690 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2691 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2692 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2693 | /* Check if we run on the same thread than the xreator thread. |
| 2694 | * We cannot access to the socket if the thread is different. |
| 2695 | */ |
| 2696 | if (socket->tid != tid) |
| 2697 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2698 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2699 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2700 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2701 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2702 | lua_pushnil(L); |
| 2703 | return 1; |
| 2704 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2705 | |
| 2706 | appctx = container_of(peer, struct hlua_csk_ctx, xref)->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2707 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2708 | |
Willy Tarreau | fd9417b | 2022-05-18 16:23:22 +0200 | [diff] [blame] | 2709 | conn = sc_conn(s->scb); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2710 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2711 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2712 | lua_pushnil(L); |
| 2713 | return 1; |
| 2714 | } |
| 2715 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2716 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2717 | xref_unlock(&socket->xref, peer); |
| 2718 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2719 | } |
| 2720 | |
| 2721 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2722 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2723 | .obj_type = OBJ_TYPE_APPLET, |
| 2724 | .name = "<LUA_TCP>", |
| 2725 | .fct = hlua_socket_handler, |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2726 | .init = hlua_socket_init, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2727 | .release = hlua_socket_release, |
| 2728 | }; |
| 2729 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2730 | __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] | 2731 | { |
| 2732 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2733 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2734 | struct xref *peer; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2735 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2736 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2737 | struct stream *s; |
| 2738 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2739 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2740 | hlua = hlua_gethlua(L); |
| 2741 | if (!hlua) |
| 2742 | return 0; |
| 2743 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2744 | /* Check if we run on the same thread than the xreator thread. |
| 2745 | * We cannot access to the socket if the thread is different. |
| 2746 | */ |
| 2747 | if (socket->tid != tid) |
| 2748 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2749 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2750 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2751 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2752 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2753 | lua_pushnil(L); |
| 2754 | lua_pushstring(L, "Can't connect"); |
| 2755 | return 2; |
| 2756 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2757 | |
| 2758 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2759 | appctx = csk_ctx->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2760 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2761 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2762 | /* Check if we run on the same thread than the xreator thread. |
| 2763 | * We cannot access to the socket if the thread is different. |
| 2764 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2765 | if (socket->tid != tid) { |
| 2766 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2767 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2768 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2769 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2770 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2771 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2772 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2773 | lua_pushnil(L); |
| 2774 | lua_pushstring(L, "Can't connect"); |
| 2775 | return 2; |
| 2776 | } |
| 2777 | |
Willy Tarreau | 8e7c6e6 | 2022-05-18 17:58:02 +0200 | [diff] [blame] | 2778 | appctx = __sc_appctx(s->scf); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2779 | |
| 2780 | /* Check for connection established. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2781 | if (csk_ctx->connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2782 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2783 | lua_pushinteger(L, 1); |
| 2784 | return 1; |
| 2785 | } |
| 2786 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2787 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2788 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2789 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2790 | } |
| 2791 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2792 | 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] | 2793 | return 0; |
| 2794 | } |
| 2795 | |
| 2796 | /* This function fail or initite the connection. */ |
| 2797 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2798 | { |
| 2799 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2800 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2801 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2802 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2803 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2804 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2805 | int low, high; |
| 2806 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2807 | struct xref *peer; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2808 | struct stconn *sc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2809 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2810 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2811 | if (lua_gettop(L) < 2) |
| 2812 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2813 | |
| 2814 | /* Get args. */ |
| 2815 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2816 | |
| 2817 | /* Check if we run on the same thread than the xreator thread. |
| 2818 | * We cannot access to the socket if the thread is different. |
| 2819 | */ |
| 2820 | if (socket->tid != tid) |
| 2821 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2822 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2823 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2824 | if (lua_gettop(L) >= 3) { |
| 2825 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2826 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2827 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2828 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2829 | * that are not enclosed within square brackets. |
| 2830 | */ |
| 2831 | if (port > 0) { |
| 2832 | luaL_buffinit(L, &b); |
| 2833 | luaL_addstring(&b, ip); |
| 2834 | luaL_addchar(&b, ':'); |
| 2835 | luaL_pushresult(&b); |
| 2836 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2837 | } |
| 2838 | } |
| 2839 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2840 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2841 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2842 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2843 | lua_pushnil(L); |
| 2844 | return 1; |
| 2845 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2846 | |
| 2847 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2848 | 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] | 2849 | if (!addr) { |
| 2850 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2851 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2852 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2853 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2854 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2855 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2856 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2857 | if (port == -1) { |
| 2858 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2859 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2860 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2861 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2862 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2863 | if (port == -1) { |
| 2864 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2865 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2866 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2867 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2868 | } |
| 2869 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2870 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2871 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2872 | appctx = csk_ctx->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2873 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2874 | s = __sc_strm(sc); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2875 | |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2876 | if (!sockaddr_alloc(&sc_opposite(sc)->dst, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2877 | xref_unlock(&socket->xref, peer); |
| 2878 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2879 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2880 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2881 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2882 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2883 | if (!hlua) |
| 2884 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2885 | |
| 2886 | /* inform the stream that we want to be notified whenever the |
| 2887 | * connection completes. |
| 2888 | */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 2889 | applet_need_more_data(appctx); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 2890 | applet_have_more_data(appctx); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2891 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2892 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2893 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2894 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2895 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2896 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2897 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2898 | } |
| 2899 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2900 | |
| 2901 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2902 | /* Return yield waiting for connection. */ |
| 2903 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2904 | 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] | 2905 | |
| 2906 | return 0; |
| 2907 | } |
| 2908 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2909 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2910 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2911 | { |
| 2912 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2913 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2914 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2915 | |
| 2916 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2917 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2918 | |
| 2919 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2920 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2921 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2922 | lua_pushnil(L); |
| 2923 | return 1; |
| 2924 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2925 | |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2926 | s = appctx_strm(container_of(peer, struct hlua_csk_ctx, xref)->appctx); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2927 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2928 | s->target = &socket_ssl->obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2929 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2930 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2931 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2932 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2933 | |
| 2934 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2935 | { |
| 2936 | return 0; |
| 2937 | } |
| 2938 | |
| 2939 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2940 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2941 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2942 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2943 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2944 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2945 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2946 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2947 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2948 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2949 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2950 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2951 | /* convert the timeout to millis */ |
| 2952 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2953 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2954 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2955 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2956 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2957 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2958 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2959 | 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] | 2960 | |
| 2961 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2962 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2963 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2964 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2965 | /* Check if we run on the same thread than the xreator thread. |
| 2966 | * We cannot access to the socket if the thread is different. |
| 2967 | */ |
| 2968 | if (socket->tid != tid) |
| 2969 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2970 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2971 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2972 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2973 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2974 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2975 | WILL_LJMP(lua_error(L)); |
| 2976 | return 0; |
| 2977 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2978 | |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2979 | s = appctx_strm(container_of(peer, struct hlua_csk_ctx, xref)->appctx); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2980 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2981 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2982 | s->req.rto = tmout; |
| 2983 | s->req.wto = tmout; |
| 2984 | s->res.rto = tmout; |
| 2985 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2986 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2987 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2988 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2989 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2990 | |
| 2991 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2992 | task_queue(s->task); |
| 2993 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2994 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2995 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2996 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2997 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2998 | } |
| 2999 | |
| 3000 | __LJMP static int hlua_socket_new(lua_State *L) |
| 3001 | { |
| 3002 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3003 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3004 | struct appctx *appctx; |
| 3005 | |
| 3006 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3007 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3008 | hlua_pusherror(L, "socket: full stack"); |
| 3009 | goto out_fail_conf; |
| 3010 | } |
| 3011 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3012 | /* Create the object: obj[0] = userdata. */ |
| 3013 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3014 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3015 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3016 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 3017 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3018 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3019 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3020 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 3021 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 3022 | goto out_fail_conf; |
| 3023 | } |
| 3024 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3025 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3026 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 3027 | lua_setmetatable(L, -2); |
| 3028 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3029 | /* Create the applet context */ |
Christopher Faulet | 6095d57 | 2022-05-16 17:09:48 +0200 | [diff] [blame] | 3030 | appctx = appctx_new_here(&update_applet, NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3031 | if (!appctx) { |
| 3032 | hlua_pusherror(L, "socket: out of memory"); |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 3033 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3034 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3035 | ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
| 3036 | ctx->connected = 0; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3037 | ctx->die = 0; |
| 3038 | LIST_INIT(&ctx->wake_on_write); |
| 3039 | LIST_INIT(&ctx->wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 3040 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 3041 | if (appctx_init(appctx) == -1) { |
| 3042 | hlua_pusherror(L, "socket: fail to init applet."); |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 3043 | goto out_fail_appctx; |
| 3044 | } |
| 3045 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3046 | /* Initialise cross reference between stream and Lua socket object. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3047 | xref_create(&socket->xref, &ctx->xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3048 | return 1; |
| 3049 | |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 3050 | out_fail_appctx: |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 3051 | appctx_free_on_early_error(appctx); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3052 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3053 | WILL_LJMP(lua_error(L)); |
| 3054 | return 0; |
| 3055 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 3056 | |
| 3057 | /* |
| 3058 | * |
| 3059 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3060 | * Class Channel |
| 3061 | * |
| 3062 | * |
| 3063 | */ |
| 3064 | |
| 3065 | /* Returns the struct hlua_channel join to the class channel in the |
| 3066 | * stack entry "ud" or throws an argument error. |
| 3067 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3068 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3069 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3070 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3071 | } |
| 3072 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3073 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 3074 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3075 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 3076 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3077 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3078 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3079 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3080 | return 0; |
| 3081 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3082 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3083 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3084 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3085 | |
| 3086 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3087 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 3088 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3089 | return 1; |
| 3090 | } |
| 3091 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3092 | /* Helper function returning a filter attached to a channel at the position <ud> |
| 3093 | * 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] | 3094 | * filter is attached, NULL is returned and <offset> and <len> are not |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3095 | * initialized. |
| 3096 | */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3097 | 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] | 3098 | { |
| 3099 | struct filter *filter = NULL; |
| 3100 | |
| 3101 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 3102 | struct hlua_flt_ctx *flt_ctx; |
| 3103 | |
| 3104 | filter = lua_touserdata (L, -1); |
| 3105 | flt_ctx = filter->ctx; |
| 3106 | if (hlua_filter_from_payload(filter)) { |
| 3107 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 3108 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 3109 | } |
| 3110 | } |
| 3111 | |
| 3112 | lua_pop(L, 1); |
| 3113 | return filter; |
| 3114 | } |
| 3115 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3116 | /* Copies <len> bytes of data present in the channel's buffer, starting at the |
| 3117 | * 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] | 3118 | * responsibility to ensure <len> and <offset> are valid. It always return the |
| 3119 | * length of the built string. <len> may be 0, in this case, an empty string is |
| 3120 | * created and 0 is returned. |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3121 | */ |
| 3122 | 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] | 3123 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3124 | size_t block1, block2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3125 | luaL_Buffer b; |
| 3126 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3127 | block1 = len; |
| 3128 | if (block1 > b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset))) |
| 3129 | block1 = b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset)); |
| 3130 | block2 = len - block1; |
| 3131 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3132 | luaL_buffinit(L, &b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3133 | luaL_addlstring(&b, b_peek(&chn->buf, offset), block1); |
| 3134 | if (block2) |
| 3135 | luaL_addlstring(&b, b_orig(&chn->buf), block2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3136 | luaL_pushresult(&b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3137 | return len; |
| 3138 | } |
| 3139 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3140 | /* Inserts the string <str> to the channel's buffer at the offset <offset>. This |
| 3141 | * function returns -1 if data cannot be copied. Otherwise, it returns the |
| 3142 | * number of bytes copied. |
| 3143 | */ |
| 3144 | static int _hlua_channel_insert(struct channel *chn, lua_State *L, struct ist str, size_t offset) |
| 3145 | { |
| 3146 | int ret = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3147 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3148 | /* Nothing to do, just return */ |
| 3149 | if (unlikely(istlen(str) == 0)) |
| 3150 | goto end; |
| 3151 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3152 | if (istlen(str) > c_room(chn)) { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3153 | ret = -1; |
| 3154 | goto end; |
| 3155 | } |
| 3156 | ret = b_insert_blk(&chn->buf, offset, istptr(str), istlen(str)); |
| 3157 | |
| 3158 | end: |
| 3159 | return ret; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3160 | } |
| 3161 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3162 | /* Removes <len> bytes of data at the absolute position <offset>. |
| 3163 | */ |
| 3164 | static void _hlua_channel_delete(struct channel *chn, size_t offset, size_t len) |
| 3165 | { |
| 3166 | size_t end = offset + len; |
| 3167 | |
| 3168 | if (b_peek(&chn->buf, end) != b_tail(&chn->buf)) |
| 3169 | b_move(&chn->buf, b_peek_ofs(&chn->buf, end), |
| 3170 | b_data(&chn->buf) - end, -len); |
| 3171 | b_sub(&chn->buf, len); |
| 3172 | } |
| 3173 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3174 | /* Copies input data in the channel's buffer. It is possible to set a specific |
| 3175 | * offset (0 by default) and a length (all remaining input data starting for the |
| 3176 | * offset by default). If there is not enough input data and more data can be |
| 3177 | * received, this function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3178 | * |
| 3179 | * From an action, All input data are considered. For a filter, the offset and |
| 3180 | * 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] | 3181 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3182 | __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] | 3183 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3184 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3185 | struct filter *filter; |
| 3186 | size_t input, output; |
| 3187 | int offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3188 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3189 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3190 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3191 | output = co_data(chn); |
| 3192 | input = ci_data(chn); |
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 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3195 | if (filter && !hlua_filter_from_payload(filter)) |
| 3196 | WILL_LJMP(lua_error(L)); |
| 3197 | |
| 3198 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3199 | if (lua_gettop(L) > 1) { |
| 3200 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3201 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3202 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3203 | offset += output; |
| 3204 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3205 | lua_pushfstring(L, "offset out of range."); |
| 3206 | WILL_LJMP(lua_error(L)); |
| 3207 | } |
| 3208 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3209 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3210 | if (lua_gettop(L) == 3) { |
| 3211 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3212 | if (!len) |
| 3213 | goto dup; |
| 3214 | if (len == -1) |
| 3215 | len = global.tune.bufsize; |
| 3216 | if (len < 0) { |
| 3217 | lua_pushfstring(L, "length out of range."); |
| 3218 | WILL_LJMP(lua_error(L)); |
| 3219 | } |
| 3220 | } |
| 3221 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3222 | if (offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3223 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3224 | /* Yield waiting for more data, as requested */ |
| 3225 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_data_yield, TICK_ETERNITY, 0)); |
| 3226 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3227 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3228 | } |
| 3229 | |
| 3230 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3231 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3232 | return 1; |
| 3233 | } |
| 3234 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3235 | /* Copies the first line (including the trailing LF) of input data in the |
| 3236 | * channel's buffer. It is possible to set a specific offset (0 by default) and |
| 3237 | * a length (all remaining input data starting for the offset by default). If |
| 3238 | * there is not enough input data and more data can be received, the function |
| 3239 | * yields. If a length is explicitly specified, no more data are |
| 3240 | * copied. Otherwise, if no LF is found and more data can be received, this |
| 3241 | * function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3242 | * |
| 3243 | * From an action, All input data are considered. For a filter, the offset and |
| 3244 | * 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] | 3245 | */ |
| 3246 | __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] | 3247 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3248 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3249 | struct filter *filter; |
| 3250 | size_t l, input, output; |
| 3251 | int offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3252 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3253 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3254 | output = co_data(chn); |
| 3255 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3256 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3257 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3258 | if (filter && !hlua_filter_from_payload(filter)) |
| 3259 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3260 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3261 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3262 | if (lua_gettop(L) > 1) { |
| 3263 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3264 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3265 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3266 | offset += output; |
| 3267 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3268 | lua_pushfstring(L, "offset out of range."); |
| 3269 | WILL_LJMP(lua_error(L)); |
| 3270 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3271 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3272 | |
| 3273 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3274 | if (lua_gettop(L) == 3) { |
| 3275 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3276 | if (!len) |
| 3277 | goto dup; |
| 3278 | if (len == -1) |
| 3279 | len = global.tune.bufsize; |
| 3280 | if (len < 0) { |
| 3281 | lua_pushfstring(L, "length out of range."); |
| 3282 | WILL_LJMP(lua_error(L)); |
| 3283 | } |
| 3284 | } |
| 3285 | |
| 3286 | for (l = 0; l < len; l++) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3287 | if (l + offset >= output + input) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3288 | break; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3289 | if (*(b_peek(&chn->buf, offset + l)) == '\n') { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3290 | len = l+1; |
| 3291 | goto dup; |
| 3292 | } |
| 3293 | } |
| 3294 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3295 | if (offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3296 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3297 | /* Yield waiting for more data */ |
| 3298 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_line_yield, TICK_ETERNITY, 0)); |
| 3299 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3300 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3301 | } |
| 3302 | |
| 3303 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3304 | _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3305 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3306 | } |
| 3307 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3308 | /* [ DEPRECATED ] |
| 3309 | * |
| 3310 | * Duplicate all input data foud in the channel's buffer. The data are not |
| 3311 | * removed from the buffer. This function relies on _hlua_channel_dup(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3312 | * |
| 3313 | * From an action, All input data are considered. For a filter, the offset and |
| 3314 | * 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] | 3315 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3316 | __LJMP static int hlua_channel_dup(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3317 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3318 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3319 | struct filter *filter; |
| 3320 | size_t offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3321 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3322 | MAY_LJMP(check_args(L, 1, "dup")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3323 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3324 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3325 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3326 | WILL_LJMP(lua_error(L)); |
| 3327 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3328 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3329 | offset = co_data(chn); |
| 3330 | len = ci_data(chn); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3331 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3332 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3333 | if (filter && !hlua_filter_from_payload(filter)) |
| 3334 | WILL_LJMP(lua_error(L)); |
| 3335 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3336 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3337 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3338 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3339 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3340 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3341 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3342 | return 1; |
| 3343 | } |
| 3344 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3345 | /* [ DEPRECATED ] |
| 3346 | * |
| 3347 | * Get all input data foud in the channel's buffer. The data are removed from |
| 3348 | * the buffer after the copy. This function relies on _hlua_channel_dup() and |
| 3349 | * _hlua_channel_delete(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3350 | * |
| 3351 | * From an action, All input data are considered. For a filter, the offset and |
| 3352 | * 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] | 3353 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3354 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3355 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3356 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3357 | struct filter *filter; |
| 3358 | size_t offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3359 | int ret; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3360 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3361 | MAY_LJMP(check_args(L, 1, "get")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3362 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3363 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3364 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3365 | WILL_LJMP(lua_error(L)); |
| 3366 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3367 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3368 | offset = co_data(chn); |
| 3369 | len = ci_data(chn); |
| 3370 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3371 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3372 | if (filter && !hlua_filter_from_payload(filter)) |
| 3373 | WILL_LJMP(lua_error(L)); |
| 3374 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3375 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3376 | lua_pushnil(L); |
| 3377 | return 1; |
| 3378 | } |
| 3379 | |
| 3380 | ret = _hlua_channel_dup(chn, L, offset, len); |
| 3381 | _hlua_channel_delete(chn, offset, ret); |
| 3382 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3383 | } |
| 3384 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3385 | /* This functions consumes and returns one line. If the channel is closed, |
| 3386 | * 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] | 3387 | * 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] | 3388 | * value. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3389 | * |
| 3390 | * From an action, All input data are considered. For a filter, the offset and |
| 3391 | * 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] | 3392 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3393 | __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] | 3394 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3395 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3396 | struct filter *filter; |
| 3397 | size_t l, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3398 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3399 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3400 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3401 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3402 | offset = co_data(chn); |
| 3403 | len = ci_data(chn); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3404 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3405 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3406 | if (filter && !hlua_filter_from_payload(filter)) |
| 3407 | WILL_LJMP(lua_error(L)); |
| 3408 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3409 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3410 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3411 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3412 | } |
| 3413 | |
| 3414 | for (l = 0; l < len; l++) { |
| 3415 | if (*(b_peek(&chn->buf, offset+l)) == '\n') { |
| 3416 | len = l+1; |
| 3417 | goto dup; |
| 3418 | } |
| 3419 | } |
| 3420 | |
| 3421 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3422 | /* Yield waiting for more data */ |
| 3423 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0)); |
| 3424 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3425 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3426 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3427 | ret = _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3428 | _hlua_channel_delete(chn, offset, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3429 | return 1; |
| 3430 | } |
| 3431 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3432 | /* [ DEPRECATED ] |
| 3433 | * |
| 3434 | * Check arguments for the function "hlua_channel_getline_yield". |
| 3435 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3436 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3437 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3438 | struct channel *chn; |
| 3439 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3440 | MAY_LJMP(check_args(L, 1, "getline")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3441 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3442 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3443 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3444 | WILL_LJMP(lua_error(L)); |
| 3445 | } |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3446 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3447 | } |
| 3448 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3449 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3450 | * available input data are returned. The offset may be negactive to start from |
| 3451 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3452 | * size. |
| 3453 | */ |
| 3454 | __LJMP static int hlua_channel_get_data(lua_State *L) |
| 3455 | { |
| 3456 | struct channel *chn; |
| 3457 | |
| 3458 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3459 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 3460 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3461 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3462 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3463 | WILL_LJMP(lua_error(L)); |
| 3464 | } |
| 3465 | return MAY_LJMP(hlua_channel_get_data_yield(L, 0, 0)); |
| 3466 | } |
| 3467 | |
| 3468 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3469 | * available input data are returned. The offset may be negactive to start from |
| 3470 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3471 | * size. |
| 3472 | */ |
| 3473 | __LJMP static int hlua_channel_get_line(lua_State *L) |
| 3474 | { |
| 3475 | struct channel *chn; |
| 3476 | |
| 3477 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3478 | WILL_LJMP(luaL_error(L, "'line' expects at most 2 arguments")); |
| 3479 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3480 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3481 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3482 | WILL_LJMP(lua_error(L)); |
| 3483 | } |
| 3484 | return MAY_LJMP(hlua_channel_get_line_yield(L, 0, 0)); |
| 3485 | } |
| 3486 | |
| 3487 | /* Appends a string into the input side of channel. It returns the length of the |
| 3488 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3489 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3490 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3491 | * |
| 3492 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3493 | */ |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3494 | __LJMP static int hlua_channel_append(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3495 | { |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3496 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3497 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3498 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3499 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3500 | int ret; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3501 | |
| 3502 | MAY_LJMP(check_args(L, 2, "append")); |
| 3503 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3504 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3505 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3506 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3507 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3508 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3509 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3510 | offset = co_data(chn); |
| 3511 | len = ci_data(chn); |
| 3512 | |
| 3513 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3514 | if (filter && !hlua_filter_from_payload(filter)) |
| 3515 | WILL_LJMP(lua_error(L)); |
| 3516 | |
| 3517 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3518 | if (ret > 0 && filter) { |
| 3519 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3520 | |
| 3521 | flt_update_offsets(filter, chn, ret); |
| 3522 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3523 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3524 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3525 | return 1; |
| 3526 | } |
| 3527 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3528 | /* Prepends a string into the input side of channel. It returns the length of the |
| 3529 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3530 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3531 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3532 | * |
| 3533 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3534 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3535 | __LJMP static int hlua_channel_prepend(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3536 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3537 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3538 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3539 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3540 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3541 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3542 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3543 | MAY_LJMP(check_args(L, 2, "prepend")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3544 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3545 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3546 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3547 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3548 | WILL_LJMP(lua_error(L)); |
| 3549 | } |
| 3550 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3551 | offset = co_data(chn); |
| 3552 | len = ci_data(chn); |
| 3553 | |
| 3554 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3555 | if (filter && !hlua_filter_from_payload(filter)) |
| 3556 | WILL_LJMP(lua_error(L)); |
| 3557 | |
| 3558 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3559 | if (ret > 0 && filter) { |
| 3560 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3561 | |
| 3562 | flt_update_offsets(filter, chn, ret); |
| 3563 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3564 | } |
| 3565 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3566 | lua_pushinteger(L, ret); |
| 3567 | return 1; |
| 3568 | } |
| 3569 | |
| 3570 | /* Inserts a given amount of input data at the given offset by a string |
| 3571 | * content. By default the string is appended at the end of input data. It |
| 3572 | * returns the length of the written string, or -1 if the channel is closed or |
| 3573 | * if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3574 | * |
| 3575 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3576 | */ |
| 3577 | __LJMP static int hlua_channel_insert_data(lua_State *L) |
| 3578 | { |
| 3579 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3580 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3581 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3582 | size_t sz, input, output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3583 | int ret, offset; |
| 3584 | |
| 3585 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 3586 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
| 3587 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3588 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3589 | |
| 3590 | output = co_data(chn); |
| 3591 | input = ci_data(chn); |
| 3592 | |
| 3593 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3594 | if (filter && !hlua_filter_from_payload(filter)) |
| 3595 | WILL_LJMP(lua_error(L)); |
| 3596 | |
| 3597 | offset = input + output; |
| 3598 | if (lua_gettop(L) > 2) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3599 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3600 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3601 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3602 | offset += output; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3603 | if (offset < output || offset > output + input) { |
| 3604 | lua_pushfstring(L, "offset out of range."); |
| 3605 | WILL_LJMP(lua_error(L)); |
| 3606 | } |
| 3607 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3608 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3609 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3610 | WILL_LJMP(lua_error(L)); |
| 3611 | } |
| 3612 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3613 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3614 | if (ret > 0 && filter) { |
| 3615 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3616 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3617 | flt_update_offsets(filter, chn, ret); |
| 3618 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3619 | } |
| 3620 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3621 | lua_pushinteger(L, ret); |
| 3622 | return 1; |
| 3623 | } |
| 3624 | /* Replaces a given amount of input data at the given offset by a string |
| 3625 | * content. By default all remaining data are removed (offset = 0 and len = |
| 3626 | * -1). It returns the length of the written string, or -1 if the channel is |
| 3627 | * closed or if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3628 | * |
| 3629 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3630 | */ |
| 3631 | __LJMP static int hlua_channel_set_data(lua_State *L) |
| 3632 | { |
| 3633 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3634 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3635 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3636 | size_t sz, input, output; |
| 3637 | int ret, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3638 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3639 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 3640 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 3641 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3642 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3643 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3644 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3645 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3646 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3647 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3648 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3649 | output = co_data(chn); |
| 3650 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3651 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3652 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3653 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3654 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3655 | |
| 3656 | offset = output; |
| 3657 | if (lua_gettop(L) > 2) { |
| 3658 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3659 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3660 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3661 | offset += output; |
| 3662 | if (offset < output || offset > input + output) { |
| 3663 | lua_pushfstring(L, "offset out of range."); |
| 3664 | WILL_LJMP(lua_error(L)); |
| 3665 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3666 | } |
| 3667 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3668 | len = output + input - offset; |
| 3669 | if (lua_gettop(L) == 4) { |
| 3670 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 3671 | if (!len) |
| 3672 | goto set; |
| 3673 | if (len == -1) |
| 3674 | len = output + input - offset; |
| 3675 | if (len < 0 || offset + len > output + input) { |
| 3676 | lua_pushfstring(L, "length out of range."); |
| 3677 | WILL_LJMP(lua_error(L)); |
| 3678 | } |
| 3679 | } |
| 3680 | |
| 3681 | set: |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3682 | /* 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] | 3683 | if (sz > c_room(chn) + len) |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3684 | lua_pushinteger(L, -1); |
| 3685 | else { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3686 | _hlua_channel_delete(chn, offset, len); |
| 3687 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3688 | if (filter) { |
| 3689 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3690 | |
| 3691 | len -= (ret > 0 ? ret : 0); |
| 3692 | flt_update_offsets(filter, chn, -len); |
| 3693 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3694 | } |
| 3695 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3696 | lua_pushinteger(L, ret); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3697 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3698 | return 1; |
| 3699 | } |
| 3700 | |
| 3701 | /* 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] | 3702 | * 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] | 3703 | * the removed data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3704 | * |
| 3705 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3706 | */ |
| 3707 | __LJMP static int hlua_channel_del_data(lua_State *L) |
| 3708 | { |
| 3709 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3710 | struct filter *filter; |
| 3711 | size_t input, output; |
| 3712 | int offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3713 | |
| 3714 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3715 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
| 3716 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3717 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3718 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3719 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3720 | WILL_LJMP(lua_error(L)); |
| 3721 | } |
| 3722 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3723 | output = co_data(chn); |
| 3724 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3725 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3726 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3727 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3728 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3729 | |
| 3730 | offset = output; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 3731 | if (lua_gettop(L) > 1) { |
| 3732 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3733 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3734 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3735 | offset += output; |
| 3736 | if (offset < output || offset > input + output) { |
| 3737 | lua_pushfstring(L, "offset out of range."); |
| 3738 | WILL_LJMP(lua_error(L)); |
| 3739 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3740 | } |
| 3741 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3742 | len = output + input - offset; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 3743 | if (lua_gettop(L) == 3) { |
| 3744 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3745 | if (!len) |
| 3746 | goto end; |
| 3747 | if (len == -1) |
| 3748 | len = output + input - offset; |
| 3749 | if (len < 0 || offset + len > output + input) { |
| 3750 | lua_pushfstring(L, "length out of range."); |
| 3751 | WILL_LJMP(lua_error(L)); |
| 3752 | } |
| 3753 | } |
| 3754 | |
| 3755 | _hlua_channel_delete(chn, offset, len); |
| 3756 | if (filter) { |
| 3757 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3758 | |
| 3759 | flt_update_offsets(filter, chn, -len); |
| 3760 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3761 | } |
| 3762 | |
| 3763 | end: |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3764 | lua_pushinteger(L, len); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3765 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3766 | } |
| 3767 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3768 | /* Append data in the output side of the buffer. This data is immediately |
| 3769 | * sent. The function returns the amount of data written. If the buffer |
| 3770 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3771 | * if the channel is closed. |
| 3772 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3773 | __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] | 3774 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3775 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3776 | struct filter *filter; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3777 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3778 | size_t offset, len, sz; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3779 | int l, ret; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3780 | struct hlua *hlua; |
| 3781 | |
| 3782 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3783 | hlua = hlua_gethlua(L); |
| 3784 | if (!hlua) { |
| 3785 | lua_pushnil(L); |
| 3786 | return 1; |
| 3787 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3788 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3789 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3790 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3791 | l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3792 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3793 | offset = co_data(chn); |
| 3794 | len = ci_data(chn); |
| 3795 | |
| 3796 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3797 | if (filter && !hlua_filter_from_payload(filter)) |
| 3798 | WILL_LJMP(lua_error(L)); |
| 3799 | |
| 3800 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3801 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3802 | lua_pushinteger(L, -1); |
| 3803 | return 1; |
| 3804 | } |
| 3805 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3806 | len = c_room(chn); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3807 | if (len > sz -l) { |
| 3808 | if (filter) { |
| 3809 | lua_pushinteger(L, -1); |
| 3810 | return 1; |
| 3811 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3812 | len = sz - l; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3813 | } |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3814 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3815 | ret = _hlua_channel_insert(chn, L, ist2(str, len), offset); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3816 | if (ret == -1) { |
| 3817 | lua_pop(L, 1); |
| 3818 | lua_pushinteger(L, -1); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3819 | return 1; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3820 | } |
| 3821 | if (ret) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3822 | if (filter) { |
| 3823 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3824 | |
| 3825 | |
| 3826 | flt_update_offsets(filter, chn, ret); |
| 3827 | FLT_OFF(filter, chn) += ret; |
| 3828 | flt_ctx->cur_off[CHN_IDX(chn)] += ret; |
| 3829 | } |
| 3830 | else |
| 3831 | c_adv(chn, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3832 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3833 | l += ret; |
| 3834 | lua_pop(L, 1); |
| 3835 | lua_pushinteger(L, l); |
| 3836 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3837 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3838 | if (l < sz) { |
| 3839 | /* Yield only if the channel's output is not empty. |
| 3840 | * Otherwise it means we cannot add more data. */ |
| 3841 | if (co_data(chn) == 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3842 | return 1; |
| 3843 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3844 | /* If we are waiting for space in the response buffer, we |
| 3845 | * must set the flag WAKERESWR. This flag required the task |
| 3846 | * wake up if any activity is detected on the response buffer. |
| 3847 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3848 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3849 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3850 | else |
| 3851 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3852 | 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] | 3853 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3854 | |
| 3855 | return 1; |
| 3856 | } |
| 3857 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3858 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3859 | * yield the LUA process, and resume it without checking the |
| 3860 | * input arguments. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3861 | * |
| 3862 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3863 | */ |
| 3864 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3865 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3866 | struct channel *chn; |
| 3867 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3868 | MAY_LJMP(check_args(L, 2, "send")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3869 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3870 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3871 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3872 | WILL_LJMP(lua_error(L)); |
| 3873 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3874 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3875 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3876 | } |
| 3877 | |
| 3878 | /* This function forward and amount of butes. The data pass from |
| 3879 | * the input side of the buffer to the output side, and can be |
| 3880 | * forwarded. This function never fails. |
| 3881 | * |
| 3882 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3883 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3884 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3885 | __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] | 3886 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3887 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3888 | struct filter *filter; |
| 3889 | size_t offset, len, fwd; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3890 | int l, max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3891 | struct hlua *hlua; |
| 3892 | |
| 3893 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3894 | hlua = hlua_gethlua(L); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3895 | if (!hlua) { |
| 3896 | lua_pushnil(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3897 | return 1; |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3898 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3899 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3900 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3901 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3902 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3903 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3904 | offset = co_data(chn); |
| 3905 | len = ci_data(chn); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3906 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3907 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3908 | if (filter && !hlua_filter_from_payload(filter)) |
| 3909 | WILL_LJMP(lua_error(L)); |
| 3910 | |
| 3911 | max = fwd - l; |
| 3912 | if (max > len) |
| 3913 | max = len; |
| 3914 | |
| 3915 | if (filter) { |
| 3916 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3917 | |
| 3918 | FLT_OFF(filter, chn) += max; |
| 3919 | flt_ctx->cur_off[CHN_IDX(chn)] += max; |
| 3920 | flt_ctx->cur_len[CHN_IDX(chn)] -= max; |
| 3921 | } |
| 3922 | else |
| 3923 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3924 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3925 | l += max; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3926 | lua_pop(L, 1); |
| 3927 | lua_pushinteger(L, l); |
| 3928 | |
| 3929 | /* Check if it miss bytes to forward. */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3930 | if (l < fwd) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3931 | /* The the input channel or the output channel are closed, we |
| 3932 | * must return the amount of data forwarded. |
| 3933 | */ |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3934 | 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] | 3935 | return 1; |
| 3936 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3937 | /* If we are waiting for space data in the response buffer, we |
| 3938 | * must set the flag WAKERESWR. This flag required the task |
| 3939 | * wake up if any activity is detected on the response buffer. |
| 3940 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3941 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3942 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3943 | else |
| 3944 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3945 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3946 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3947 | 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] | 3948 | } |
| 3949 | |
| 3950 | return 1; |
| 3951 | } |
| 3952 | |
| 3953 | /* Just check the input and prepare the stack for the previous |
| 3954 | * function "hlua_channel_forward_yield" |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3955 | * |
| 3956 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3957 | */ |
| 3958 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3959 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3960 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3961 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3962 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3963 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3964 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3965 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3966 | WILL_LJMP(lua_error(L)); |
| 3967 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3968 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3969 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3970 | } |
| 3971 | |
| 3972 | /* Just returns the number of bytes available in the input |
| 3973 | * side of the buffer. This function never fails. |
| 3974 | */ |
| 3975 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3976 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3977 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3978 | struct filter *filter; |
| 3979 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3980 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3981 | MAY_LJMP(check_args(L, 1, "input")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3982 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3983 | |
| 3984 | output = co_data(chn); |
| 3985 | input = ci_data(chn); |
| 3986 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3987 | if (filter || !IS_HTX_STRM(chn_strm(chn))) |
| 3988 | lua_pushinteger(L, input); |
| 3989 | else { |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3990 | struct htx *htx = htxbuf(&chn->buf); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3991 | |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3992 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3993 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3994 | return 1; |
| 3995 | } |
| 3996 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3997 | /* Returns true if the channel is full. */ |
| 3998 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3999 | { |
| 4000 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 4001 | |
| 4002 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 4003 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 4004 | /* ignore the reserve, we are not on a producer side (ie in an |
| 4005 | * applet). |
| 4006 | */ |
| 4007 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 4008 | return 1; |
| 4009 | } |
| 4010 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4011 | /* Returns true if the channel may still receive data. */ |
| 4012 | __LJMP static int hlua_channel_may_recv(lua_State *L) |
| 4013 | { |
| 4014 | struct channel *chn; |
| 4015 | |
| 4016 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 4017 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4018 | lua_pushboolean(L, (!channel_input_closed(chn) && channel_may_recv(chn))); |
| 4019 | return 1; |
| 4020 | } |
| 4021 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 4022 | /* Returns true if the channel is the response channel. */ |
| 4023 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 4024 | { |
| 4025 | struct channel *chn; |
| 4026 | |
| 4027 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 4028 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4029 | |
| 4030 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 4031 | return 1; |
| 4032 | } |
| 4033 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4034 | /* Just returns the number of bytes available in the output |
| 4035 | * side of the buffer. This function never fails. |
| 4036 | */ |
| 4037 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 4038 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4039 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4040 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4041 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4042 | MAY_LJMP(check_args(L, 1, "output")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4043 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4044 | |
| 4045 | output = co_data(chn); |
| 4046 | input = ci_data(chn); |
| 4047 | hlua_channel_filter(L, 1, chn, &output, &input); |
| 4048 | |
| 4049 | lua_pushinteger(L, output); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4050 | return 1; |
| 4051 | } |
| 4052 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4053 | /* |
| 4054 | * |
| 4055 | * |
| 4056 | * Class Fetches |
| 4057 | * |
| 4058 | * |
| 4059 | */ |
| 4060 | |
| 4061 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4062 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4063 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4064 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4065 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4066 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4067 | } |
| 4068 | |
| 4069 | /* This function creates and push in the stack a fetch object according |
| 4070 | * with a current TXN. |
| 4071 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4072 | 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] | 4073 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4074 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4075 | |
| 4076 | /* Check stack size. */ |
| 4077 | if (!lua_checkstack(L, 3)) |
| 4078 | return 0; |
| 4079 | |
| 4080 | /* Create the object: obj[0] = userdata. |
| 4081 | * Note that the base of the Fetches object is the |
| 4082 | * transaction object. |
| 4083 | */ |
| 4084 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4085 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4086 | lua_rawseti(L, -2, 0); |
| 4087 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4088 | hsmp->s = txn->s; |
| 4089 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4090 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4091 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4092 | |
| 4093 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 4094 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 4095 | lua_setmetatable(L, -2); |
| 4096 | |
| 4097 | return 1; |
| 4098 | } |
| 4099 | |
| 4100 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 4101 | * It uses closure argument to store the associated sample-fetch. It |
| 4102 | * returns only one argument or throws an error. An error is thrown |
| 4103 | * only if an error is encountered during the argument parsing. If |
| 4104 | * the "sample-fetch" function fails, nil is returned. |
| 4105 | */ |
| 4106 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 4107 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4108 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4109 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4110 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4111 | int i; |
| 4112 | struct sample smp; |
| 4113 | |
| 4114 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4115 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4116 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4117 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4118 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4119 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 4120 | /* Check execution authorization. */ |
| 4121 | if (f->use & SMP_USE_HTTP_ANY && |
| 4122 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 4123 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 4124 | "is not available in Lua services", f->kw); |
| 4125 | WILL_LJMP(lua_error(L)); |
| 4126 | } |
| 4127 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4128 | /* Get extra arguments. */ |
| 4129 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 4130 | if (i >= ARGM_NBARGS) |
| 4131 | break; |
| 4132 | hlua_lua2arg(L, i + 2, &args[i]); |
| 4133 | } |
| 4134 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4135 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4136 | |
| 4137 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4138 | 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] | 4139 | |
| 4140 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4141 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4142 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4143 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4144 | } |
| 4145 | |
| 4146 | /* Initialise the sample. */ |
| 4147 | memset(&smp, 0, sizeof(smp)); |
| 4148 | |
| 4149 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4150 | 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] | 4151 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4152 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4153 | lua_pushstring(L, ""); |
| 4154 | else |
| 4155 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4156 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4157 | } |
| 4158 | |
| 4159 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4160 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4161 | hlua_smp2lua_str(L, &smp); |
| 4162 | else |
| 4163 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4164 | |
| 4165 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4166 | free_args(args); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4167 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4168 | |
| 4169 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4170 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4171 | WILL_LJMP(lua_error(L)); |
| 4172 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4173 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4174 | |
| 4175 | /* |
| 4176 | * |
| 4177 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4178 | * Class Converters |
| 4179 | * |
| 4180 | * |
| 4181 | */ |
| 4182 | |
| 4183 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4184 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4185 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4186 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4187 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4188 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4189 | } |
| 4190 | |
| 4191 | /* This function creates and push in the stack a Converters object |
| 4192 | * according with a current TXN. |
| 4193 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4194 | 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] | 4195 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4196 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4197 | |
| 4198 | /* Check stack size. */ |
| 4199 | if (!lua_checkstack(L, 3)) |
| 4200 | return 0; |
| 4201 | |
| 4202 | /* Create the object: obj[0] = userdata. |
| 4203 | * Note that the base of the Converters object is the |
| 4204 | * same than the TXN object. |
| 4205 | */ |
| 4206 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4207 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4208 | lua_rawseti(L, -2, 0); |
| 4209 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4210 | hsmp->s = txn->s; |
| 4211 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4212 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4213 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4214 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4215 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4216 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 4217 | lua_setmetatable(L, -2); |
| 4218 | |
| 4219 | return 1; |
| 4220 | } |
| 4221 | |
| 4222 | /* This function is an LUA binding. It is called with each converter. |
| 4223 | * It uses closure argument to store the associated converter. It |
| 4224 | * returns only one argument or throws an error. An error is thrown |
| 4225 | * only if an error is encountered during the argument parsing. If |
| 4226 | * the converter function function fails, nil is returned. |
| 4227 | */ |
| 4228 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 4229 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4230 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4231 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4232 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4233 | int i; |
| 4234 | struct sample smp; |
| 4235 | |
| 4236 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4237 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4238 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4239 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4240 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4241 | |
| 4242 | /* Get extra arguments. */ |
| 4243 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 4244 | if (i >= ARGM_NBARGS) |
| 4245 | break; |
| 4246 | hlua_lua2arg(L, i + 3, &args[i]); |
| 4247 | } |
| 4248 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4249 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4250 | |
| 4251 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4252 | 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] | 4253 | |
| 4254 | /* Run the special args checker. */ |
| 4255 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 4256 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4257 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4258 | } |
| 4259 | |
| 4260 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4261 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4262 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 4263 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4264 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4265 | } |
| 4266 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4267 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 4268 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4269 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4270 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4271 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4272 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4273 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4274 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4275 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 4276 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4277 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4278 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4279 | } |
| 4280 | |
| 4281 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4282 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4283 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4284 | lua_pushstring(L, ""); |
| 4285 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4286 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4287 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4288 | } |
| 4289 | |
| 4290 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4291 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4292 | hlua_smp2lua_str(L, &smp); |
| 4293 | else |
| 4294 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4295 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4296 | free_args(args); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4297 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4298 | |
| 4299 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4300 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4301 | WILL_LJMP(lua_error(L)); |
| 4302 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4303 | } |
| 4304 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4305 | /* |
| 4306 | * |
| 4307 | * |
| 4308 | * Class AppletTCP |
| 4309 | * |
| 4310 | * |
| 4311 | */ |
| 4312 | |
| 4313 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4314 | * a class stream, otherwise it throws an error. |
| 4315 | */ |
| 4316 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 4317 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4318 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4319 | } |
| 4320 | |
| 4321 | /* This function creates and push in the stack an Applet object |
| 4322 | * according with a current TXN. |
| 4323 | */ |
| 4324 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 4325 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4326 | struct hlua_appctx *luactx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 4327 | struct stream *s = appctx_strm(ctx); |
Christopher Faulet | 2da02ae | 2022-02-24 13:45:27 +0100 | [diff] [blame] | 4328 | struct proxy *p; |
| 4329 | |
| 4330 | ALREADY_CHECKED(s); |
| 4331 | p = s->be; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4332 | |
| 4333 | /* Check stack size. */ |
| 4334 | if (!lua_checkstack(L, 3)) |
| 4335 | return 0; |
| 4336 | |
| 4337 | /* Create the object: obj[0] = userdata. |
| 4338 | * Note that the base of the Converters object is the |
| 4339 | * same than the TXN object. |
| 4340 | */ |
| 4341 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4342 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4343 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4344 | luactx->appctx = ctx; |
| 4345 | luactx->htxn.s = s; |
| 4346 | luactx->htxn.p = p; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4347 | |
| 4348 | /* Create the "f" field that contains a list of fetches. */ |
| 4349 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4350 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4351 | return 0; |
| 4352 | lua_settable(L, -3); |
| 4353 | |
| 4354 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4355 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4356 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4357 | return 0; |
| 4358 | lua_settable(L, -3); |
| 4359 | |
| 4360 | /* Create the "c" field that contains a list of converters. */ |
| 4361 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4362 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4363 | return 0; |
| 4364 | lua_settable(L, -3); |
| 4365 | |
| 4366 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4367 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4368 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4369 | return 0; |
| 4370 | lua_settable(L, -3); |
| 4371 | |
| 4372 | /* Pop a class stream metatable and affect it to the table. */ |
| 4373 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 4374 | lua_setmetatable(L, -2); |
| 4375 | |
| 4376 | return 1; |
| 4377 | } |
| 4378 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4379 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 4380 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4381 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4382 | struct stream *s; |
| 4383 | const char *name; |
| 4384 | size_t len; |
| 4385 | struct sample smp; |
| 4386 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4387 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4388 | 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] | 4389 | |
| 4390 | /* It is useles to retrieve the stream, but this function |
| 4391 | * runs only in a stream context. |
| 4392 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4393 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4394 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4395 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4396 | |
| 4397 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4398 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4399 | hlua_lua2smp(L, 3, &smp); |
| 4400 | |
| 4401 | /* Store the sample in a variable. */ |
| 4402 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4403 | |
| 4404 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4405 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4406 | else |
| 4407 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4408 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4409 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4410 | } |
| 4411 | |
| 4412 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 4413 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4414 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4415 | struct stream *s; |
| 4416 | const char *name; |
| 4417 | size_t len; |
| 4418 | struct sample smp; |
| 4419 | |
| 4420 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4421 | |
| 4422 | /* It is useles to retrieve the stream, but this function |
| 4423 | * runs only in a stream context. |
| 4424 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4425 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4426 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4427 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4428 | |
| 4429 | /* Unset the variable. */ |
| 4430 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4431 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4432 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4433 | } |
| 4434 | |
| 4435 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 4436 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4437 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4438 | struct stream *s; |
| 4439 | const char *name; |
| 4440 | size_t len; |
| 4441 | struct sample smp; |
| 4442 | |
| 4443 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4444 | |
| 4445 | /* It is useles to retrieve the stream, but this function |
| 4446 | * runs only in a stream context. |
| 4447 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4448 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4449 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4450 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4451 | |
| 4452 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 4453 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4454 | lua_pushnil(L); |
| 4455 | return 1; |
| 4456 | } |
| 4457 | |
| 4458 | return hlua_smp2lua(L, &smp); |
| 4459 | } |
| 4460 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4461 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 4462 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4463 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4464 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4465 | struct hlua *hlua; |
| 4466 | |
| 4467 | /* 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] | 4468 | if (!s->hlua) |
| 4469 | return 0; |
| 4470 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4471 | |
| 4472 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4473 | |
| 4474 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4475 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4476 | |
| 4477 | /* Get and store new value. */ |
| 4478 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4479 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4480 | |
| 4481 | return 0; |
| 4482 | } |
| 4483 | |
| 4484 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 4485 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4486 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4487 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4488 | struct hlua *hlua; |
| 4489 | |
| 4490 | /* 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] | 4491 | if (!s->hlua) { |
| 4492 | lua_pushnil(L); |
| 4493 | return 1; |
| 4494 | } |
| 4495 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4496 | |
| 4497 | /* Push configuration index in the stack. */ |
| 4498 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4499 | |
| 4500 | return 1; |
| 4501 | } |
| 4502 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4503 | /* If expected data not yet available, it returns a yield. This function |
| 4504 | * consumes the data in the buffer. It returns a string containing the |
| 4505 | * data. This string can be empty. |
| 4506 | */ |
| 4507 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4508 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4509 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4510 | struct stconn *sc = appctx_sc(luactx->appctx); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4511 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4512 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4513 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4514 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4515 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4516 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4517 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4518 | ret = co_getline_nc(sc_oc(sc), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4519 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4520 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4521 | if (ret == 0) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4522 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4523 | 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] | 4524 | } |
| 4525 | |
| 4526 | /* End of data: commit the total strings and return. */ |
| 4527 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4528 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4529 | return 1; |
| 4530 | } |
| 4531 | |
| 4532 | /* Ensure that the block 2 length is usable. */ |
| 4533 | if (ret == 1) |
| 4534 | len2 = 0; |
| 4535 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4536 | /* don't check the max length read and don't check. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4537 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4538 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4539 | |
| 4540 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4541 | co_skip(sc_oc(sc), len1 + len2); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4542 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4543 | return 1; |
| 4544 | } |
| 4545 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4546 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4547 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 4548 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4549 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4550 | |
| 4551 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4552 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4553 | |
| 4554 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 4555 | } |
| 4556 | |
| 4557 | /* If expected data not yet available, it returns a yield. This function |
| 4558 | * consumes the data in the buffer. It returns a string containing the |
| 4559 | * data. This string can be empty. |
| 4560 | */ |
| 4561 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4562 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4563 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4564 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4565 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4566 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4567 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4568 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4569 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4570 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4571 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4572 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4573 | ret = co_getblk_nc(sc_oc(sc), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4574 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4575 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4576 | if (ret == 0) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4577 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4578 | 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] | 4579 | } |
| 4580 | |
| 4581 | /* End of data: commit the total strings and return. */ |
| 4582 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4583 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4584 | return 1; |
| 4585 | } |
| 4586 | |
| 4587 | /* Ensure that the block 2 length is usable. */ |
| 4588 | if (ret == 1) |
| 4589 | len2 = 0; |
| 4590 | |
| 4591 | if (len == -1) { |
| 4592 | |
| 4593 | /* If len == -1, catenate all the data avalaile and |
| 4594 | * yield because we want to get all the data until |
| 4595 | * the end of data stream. |
| 4596 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4597 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4598 | luaL_addlstring(&luactx->b, blk2, len2); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4599 | co_skip(sc_oc(sc), len1 + len2); |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4600 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4601 | 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] | 4602 | |
| 4603 | } else { |
| 4604 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4605 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4606 | if (len1 > len) |
| 4607 | len1 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4608 | luaL_addlstring(&luactx->b, blk1, len1); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4609 | len -= len1; |
| 4610 | |
| 4611 | /* Copy the second block. */ |
| 4612 | if (len2 > len) |
| 4613 | len2 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4614 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4615 | len -= len2; |
| 4616 | |
| 4617 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4618 | co_skip(sc_oc(sc), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4619 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4620 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4621 | if (len > 0) { |
| 4622 | lua_pushinteger(L, len); |
| 4623 | lua_replace(L, 2); |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4624 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4625 | 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] | 4626 | } |
| 4627 | |
| 4628 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4629 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4630 | return 1; |
| 4631 | } |
| 4632 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4633 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4634 | hlua_pusherror(L, "Lua: internal error"); |
| 4635 | WILL_LJMP(lua_error(L)); |
| 4636 | return 0; |
| 4637 | } |
| 4638 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4639 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4640 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 4641 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4642 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4643 | int len = -1; |
| 4644 | |
| 4645 | if (lua_gettop(L) > 2) |
| 4646 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4647 | if (lua_gettop(L) >= 2) { |
| 4648 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4649 | lua_pop(L, 1); |
| 4650 | } |
| 4651 | |
| 4652 | /* Confirm or set the required length */ |
| 4653 | lua_pushinteger(L, len); |
| 4654 | |
| 4655 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4656 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4657 | |
| 4658 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4659 | } |
| 4660 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4661 | /* Append data in the output side of the buffer. This data is immediately |
| 4662 | * sent. The function returns the amount of data written. If the buffer |
| 4663 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4664 | * if the channel is closed. |
| 4665 | */ |
| 4666 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4667 | { |
| 4668 | size_t len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4669 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4670 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4671 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4672 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4673 | struct channel *chn = sc_ic(sc); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4674 | int max; |
| 4675 | |
| 4676 | /* Get the max amount of data which can write as input in the channel. */ |
| 4677 | max = channel_recv_max(chn); |
| 4678 | if (max > (len - l)) |
| 4679 | max = len - l; |
| 4680 | |
| 4681 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4682 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4683 | |
| 4684 | /* update counters. */ |
| 4685 | l += max; |
| 4686 | lua_pop(L, 1); |
| 4687 | lua_pushinteger(L, l); |
| 4688 | |
| 4689 | /* If some data is not send, declares the situation to the |
| 4690 | * applet, and returns a yield. |
| 4691 | */ |
| 4692 | if (l < len) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4693 | sc_need_room(sc); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4694 | 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] | 4695 | } |
| 4696 | |
| 4697 | return 1; |
| 4698 | } |
| 4699 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4700 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4701 | * yield the LUA process, and resume it without checking the |
| 4702 | * input arguments. |
| 4703 | */ |
| 4704 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4705 | { |
| 4706 | MAY_LJMP(check_args(L, 2, "send")); |
| 4707 | lua_pushinteger(L, 0); |
| 4708 | |
| 4709 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4710 | } |
| 4711 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4712 | /* |
| 4713 | * |
| 4714 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4715 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4716 | * |
| 4717 | * |
| 4718 | */ |
| 4719 | |
| 4720 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4721 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4722 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4723 | __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] | 4724 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4725 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4726 | } |
| 4727 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4728 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4729 | * according with a current TXN. |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4730 | * It relies on the caller to have already reserved the room in ctx->svcctx |
| 4731 | * for the local storage of hlua_http_ctx. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4732 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4733 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4734 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4735 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4736 | struct hlua_appctx *luactx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4737 | struct hlua_txn htxn; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 4738 | struct stream *s = appctx_strm(ctx); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4739 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4740 | struct htx *htx; |
| 4741 | struct htx_blk *blk; |
| 4742 | struct htx_sl *sl; |
| 4743 | struct ist path; |
| 4744 | unsigned long long len = 0; |
| 4745 | int32_t pos; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4746 | struct http_uri_parser parser; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4747 | |
| 4748 | /* Check stack size. */ |
| 4749 | if (!lua_checkstack(L, 3)) |
| 4750 | return 0; |
| 4751 | |
| 4752 | /* Create the object: obj[0] = userdata. |
| 4753 | * Note that the base of the Converters object is the |
| 4754 | * same than the TXN object. |
| 4755 | */ |
| 4756 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4757 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4758 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4759 | luactx->appctx = ctx; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4760 | http_ctx->status = 200; /* Default status code returned. */ |
| 4761 | http_ctx->reason = NULL; /* Use default reason based on status */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4762 | luactx->htxn.s = s; |
| 4763 | luactx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4764 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4765 | /* Create the "f" field that contains a list of fetches. */ |
| 4766 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4767 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4768 | return 0; |
| 4769 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4770 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4771 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4772 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4773 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4774 | return 0; |
| 4775 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4776 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4777 | /* Create the "c" field that contains a list of converters. */ |
| 4778 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4779 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4780 | return 0; |
| 4781 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4782 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4783 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4784 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4785 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4786 | return 0; |
| 4787 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4788 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4789 | htx = htxbuf(&s->req.buf); |
| 4790 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4791 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4792 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4793 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4794 | /* Stores the request method. */ |
| 4795 | lua_pushstring(L, "method"); |
| 4796 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4797 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4798 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4799 | /* Stores the http version. */ |
| 4800 | lua_pushstring(L, "version"); |
| 4801 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4802 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4803 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4804 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4805 | * the array on the top of the stack. |
| 4806 | */ |
| 4807 | lua_pushstring(L, "headers"); |
| 4808 | htxn.s = s; |
| 4809 | htxn.p = px; |
| 4810 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4811 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4812 | return 0; |
| 4813 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4814 | |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4815 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 4816 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4817 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4818 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4819 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4820 | p = path.ptr; |
Tim Duesterhus | 4c8f75f | 2021-11-06 15:14:44 +0100 | [diff] [blame] | 4821 | end = istend(path); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4822 | q = p; |
| 4823 | while (q < end && *q != '?') |
| 4824 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4825 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4826 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4827 | lua_pushstring(L, "path"); |
| 4828 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4829 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4830 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4831 | /* Stores the query string. */ |
| 4832 | lua_pushstring(L, "qs"); |
| 4833 | if (*q == '?') |
| 4834 | q++; |
| 4835 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4836 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4837 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4838 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4839 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4840 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4841 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4842 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4843 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4844 | break; |
| 4845 | if (type == HTX_BLK_DATA) |
| 4846 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4847 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4848 | if (htx->extra != ULLONG_MAX) |
| 4849 | len += htx->extra; |
| 4850 | |
| 4851 | /* Stores the request path. */ |
| 4852 | lua_pushstring(L, "length"); |
| 4853 | lua_pushinteger(L, len); |
| 4854 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4855 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4856 | /* Create an empty array of HTTP request headers. */ |
| 4857 | lua_pushstring(L, "response"); |
| 4858 | lua_newtable(L); |
| 4859 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4860 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4861 | /* Pop a class stream metatable and affect it to the table. */ |
| 4862 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4863 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4864 | |
| 4865 | return 1; |
| 4866 | } |
| 4867 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4868 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4869 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4870 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4871 | struct stream *s; |
| 4872 | const char *name; |
| 4873 | size_t len; |
| 4874 | struct sample smp; |
| 4875 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4876 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4877 | 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] | 4878 | |
| 4879 | /* It is useles to retrieve the stream, but this function |
| 4880 | * runs only in a stream context. |
| 4881 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4882 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4883 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4884 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4885 | |
| 4886 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4887 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4888 | hlua_lua2smp(L, 3, &smp); |
| 4889 | |
| 4890 | /* Store the sample in a variable. */ |
| 4891 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4892 | |
| 4893 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4894 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4895 | else |
| 4896 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4897 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4898 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4899 | } |
| 4900 | |
| 4901 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4902 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4903 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4904 | struct stream *s; |
| 4905 | const char *name; |
| 4906 | size_t len; |
| 4907 | struct sample smp; |
| 4908 | |
| 4909 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4910 | |
| 4911 | /* It is useles to retrieve the stream, but this function |
| 4912 | * runs only in a stream context. |
| 4913 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4914 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4915 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4916 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4917 | |
| 4918 | /* Unset the variable. */ |
| 4919 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4920 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4921 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4922 | } |
| 4923 | |
| 4924 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4925 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4926 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4927 | struct stream *s; |
| 4928 | const char *name; |
| 4929 | size_t len; |
| 4930 | struct sample smp; |
| 4931 | |
| 4932 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4933 | |
| 4934 | /* It is useles to retrieve the stream, but this function |
| 4935 | * runs only in a stream context. |
| 4936 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4937 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4938 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4939 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4940 | |
| 4941 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 4942 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4943 | lua_pushnil(L); |
| 4944 | return 1; |
| 4945 | } |
| 4946 | |
| 4947 | return hlua_smp2lua(L, &smp); |
| 4948 | } |
| 4949 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4950 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4951 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4952 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4953 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4954 | struct hlua *hlua; |
| 4955 | |
| 4956 | /* 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] | 4957 | if (!s->hlua) |
| 4958 | return 0; |
| 4959 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4960 | |
| 4961 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4962 | |
| 4963 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4964 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4965 | |
| 4966 | /* Get and store new value. */ |
| 4967 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4968 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4969 | |
| 4970 | return 0; |
| 4971 | } |
| 4972 | |
| 4973 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4974 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4975 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4976 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4977 | struct hlua *hlua; |
| 4978 | |
| 4979 | /* 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] | 4980 | if (!s->hlua) { |
| 4981 | lua_pushnil(L); |
| 4982 | return 1; |
| 4983 | } |
| 4984 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4985 | |
| 4986 | /* Push configuration index in the stack. */ |
| 4987 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4988 | |
| 4989 | return 1; |
| 4990 | } |
| 4991 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4992 | /* If expected data not yet available, it returns a yield. This function |
| 4993 | * consumes the data in the buffer. It returns a string containing the |
| 4994 | * data. This string can be empty. |
| 4995 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4996 | __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] | 4997 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4998 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4999 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5000 | struct channel *req = sc_oc(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5001 | struct htx *htx; |
| 5002 | struct htx_blk *blk; |
| 5003 | size_t count; |
| 5004 | int stop = 0; |
| 5005 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5006 | htx = htx_from_buf(&req->buf); |
| 5007 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 5008 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 5009 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5010 | while (count && !stop && blk) { |
| 5011 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5012 | uint32_t sz = htx_get_blksz(blk); |
| 5013 | struct ist v; |
| 5014 | uint32_t vlen; |
| 5015 | char *nl; |
| 5016 | |
| 5017 | vlen = sz; |
| 5018 | if (vlen > count) { |
| 5019 | if (type != HTX_BLK_DATA) |
| 5020 | break; |
| 5021 | vlen = count; |
| 5022 | } |
| 5023 | |
| 5024 | switch (type) { |
| 5025 | case HTX_BLK_UNUSED: |
| 5026 | break; |
| 5027 | |
| 5028 | case HTX_BLK_DATA: |
| 5029 | v = htx_get_blk_value(htx, blk); |
| 5030 | v.len = vlen; |
| 5031 | nl = istchr(v, '\n'); |
| 5032 | if (nl != NULL) { |
| 5033 | stop = 1; |
| 5034 | vlen = nl - v.ptr + 1; |
| 5035 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5036 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5037 | break; |
| 5038 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5039 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5040 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5041 | stop = 1; |
| 5042 | break; |
| 5043 | |
| 5044 | default: |
| 5045 | break; |
| 5046 | } |
| 5047 | |
Willy Tarreau | 8424004 | 2022-02-28 16:51:23 +0100 | [diff] [blame] | 5048 | c_rew(req, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5049 | count -= vlen; |
| 5050 | if (sz == vlen) |
| 5051 | blk = htx_remove_blk(htx, blk); |
| 5052 | else { |
| 5053 | htx_cut_data_blk(htx, blk, vlen); |
| 5054 | break; |
| 5055 | } |
| 5056 | } |
| 5057 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5058 | /* The message was fully consumed and no more data are expected |
| 5059 | * (EOM flag set). |
| 5060 | */ |
| 5061 | if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) |
| 5062 | stop = 1; |
| 5063 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5064 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5065 | if (!stop) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 5066 | applet_need_more_data(luactx->appctx); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5067 | 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] | 5068 | } |
| 5069 | |
| 5070 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5071 | luaL_pushresult(&luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5072 | return 1; |
| 5073 | } |
| 5074 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5075 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5076 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5077 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5078 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5079 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5080 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5081 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5082 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5083 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5084 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5085 | } |
| 5086 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5087 | /* If expected data not yet available, it returns a yield. This function |
| 5088 | * consumes the data in the buffer. It returns a string containing the |
| 5089 | * data. This string can be empty. |
| 5090 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5091 | __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] | 5092 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5093 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5094 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5095 | struct channel *req = sc_oc(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5096 | struct htx *htx; |
| 5097 | struct htx_blk *blk; |
| 5098 | size_t count; |
| 5099 | int len; |
| 5100 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5101 | htx = htx_from_buf(&req->buf); |
| 5102 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5103 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 5104 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5105 | while (count && len && blk) { |
| 5106 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5107 | uint32_t sz = htx_get_blksz(blk); |
| 5108 | struct ist v; |
| 5109 | uint32_t vlen; |
| 5110 | |
| 5111 | vlen = sz; |
| 5112 | if (len > 0 && vlen > len) |
| 5113 | vlen = len; |
| 5114 | if (vlen > count) { |
| 5115 | if (type != HTX_BLK_DATA) |
| 5116 | break; |
| 5117 | vlen = count; |
| 5118 | } |
| 5119 | |
| 5120 | switch (type) { |
| 5121 | case HTX_BLK_UNUSED: |
| 5122 | break; |
| 5123 | |
| 5124 | case HTX_BLK_DATA: |
| 5125 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5126 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5127 | break; |
| 5128 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5129 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5130 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5131 | len = 0; |
| 5132 | break; |
| 5133 | |
| 5134 | default: |
| 5135 | break; |
| 5136 | } |
| 5137 | |
Willy Tarreau | 8424004 | 2022-02-28 16:51:23 +0100 | [diff] [blame] | 5138 | c_rew(req, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5139 | count -= vlen; |
| 5140 | if (len > 0) |
| 5141 | len -= vlen; |
| 5142 | if (sz == vlen) |
| 5143 | blk = htx_remove_blk(htx, blk); |
| 5144 | else { |
| 5145 | htx_cut_data_blk(htx, blk, vlen); |
| 5146 | break; |
| 5147 | } |
| 5148 | } |
| 5149 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5150 | /* The message was fully consumed and no more data are expected |
| 5151 | * (EOM flag set). |
| 5152 | */ |
| 5153 | if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) |
| 5154 | len = 0; |
| 5155 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5156 | htx_to_buf(htx, &req->buf); |
| 5157 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5158 | /* If we are no other data available, yield waiting for new data. */ |
| 5159 | if (len) { |
| 5160 | if (len > 0) { |
| 5161 | lua_pushinteger(L, len); |
| 5162 | lua_replace(L, 2); |
| 5163 | } |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 5164 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5165 | 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] | 5166 | } |
| 5167 | |
| 5168 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5169 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5170 | return 1; |
| 5171 | } |
| 5172 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5173 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5174 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 5175 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5176 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5177 | int len = -1; |
| 5178 | |
| 5179 | /* Check arguments. */ |
| 5180 | if (lua_gettop(L) > 2) |
| 5181 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 5182 | if (lua_gettop(L) >= 2) { |
| 5183 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5184 | lua_pop(L, 1); |
| 5185 | } |
| 5186 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5187 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5188 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5189 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5190 | luaL_buffinit(L, &luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5191 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5192 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5193 | } |
| 5194 | |
| 5195 | /* Append data in the output side of the buffer. This data is immediately |
| 5196 | * sent. The function returns the amount of data written. If the buffer |
| 5197 | * cannot contain the data, the function yields. The function returns -1 |
| 5198 | * if the channel is closed. |
| 5199 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5200 | __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] | 5201 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5202 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5203 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5204 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5205 | struct htx *htx = htx_from_buf(&res->buf); |
| 5206 | const char *data; |
| 5207 | size_t len; |
| 5208 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 5209 | int max; |
| 5210 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 5211 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5212 | if (!max) |
| 5213 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5214 | |
| 5215 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5216 | |
| 5217 | /* Get the max amount of data which can write as input in the channel. */ |
| 5218 | if (max > (len - l)) |
| 5219 | max = len - l; |
| 5220 | |
| 5221 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 5222 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5223 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5224 | |
| 5225 | /* update counters. */ |
| 5226 | l += max; |
| 5227 | lua_pop(L, 1); |
| 5228 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5229 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5230 | /* If some data is not send, declares the situation to the |
| 5231 | * applet, and returns a yield. |
| 5232 | */ |
| 5233 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5234 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5235 | htx_to_buf(htx, &res->buf); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5236 | sc_need_room(sc); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5237 | 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] | 5238 | } |
| 5239 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5240 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5241 | return 1; |
| 5242 | } |
| 5243 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5244 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5245 | * yield the LUA process, and resume it without checking the |
| 5246 | * input arguments. |
| 5247 | */ |
| 5248 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 5249 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5250 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5251 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5252 | |
| 5253 | /* We want to send some data. Headers must be sent. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5254 | if (!(http_ctx->flags & APPLET_HDR_SENT)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5255 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 5256 | WILL_LJMP(lua_error(L)); |
| 5257 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5258 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5259 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5260 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5261 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5262 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5263 | } |
| 5264 | |
| 5265 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 5266 | { |
| 5267 | const char *name; |
| 5268 | int ret; |
| 5269 | |
| 5270 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5271 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5272 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 5273 | |
| 5274 | /* Push in the stack the "response" entry. */ |
| 5275 | ret = lua_getfield(L, 1, "response"); |
| 5276 | if (ret != LUA_TTABLE) { |
| 5277 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 5278 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 5279 | WILL_LJMP(lua_error(L)); |
| 5280 | } |
| 5281 | |
| 5282 | /* check if the header is already registered if it is not |
| 5283 | * the case, register it. |
| 5284 | */ |
| 5285 | ret = lua_getfield(L, -1, name); |
| 5286 | if (ret == LUA_TNIL) { |
| 5287 | |
| 5288 | /* Entry not found. */ |
| 5289 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 5290 | |
| 5291 | /* Insert the new header name in the array in the top of the stack. |
| 5292 | * It left the new array in the top of the stack. |
| 5293 | */ |
| 5294 | lua_newtable(L); |
| 5295 | lua_pushvalue(L, 2); |
| 5296 | lua_pushvalue(L, -2); |
| 5297 | lua_settable(L, -4); |
| 5298 | |
| 5299 | } else if (ret != LUA_TTABLE) { |
| 5300 | |
| 5301 | /* corruption error. */ |
| 5302 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 5303 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 5304 | WILL_LJMP(lua_error(L)); |
| 5305 | } |
| 5306 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5307 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5308 | * the header value as new entry. |
| 5309 | */ |
| 5310 | lua_pushvalue(L, 3); |
| 5311 | ret = lua_rawlen(L, -2); |
| 5312 | lua_rawseti(L, -2, ret + 1); |
| 5313 | lua_pushboolean(L, 1); |
| 5314 | return 1; |
| 5315 | } |
| 5316 | |
| 5317 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 5318 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5319 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5320 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5321 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5322 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5323 | |
| 5324 | if (status < 100 || status > 599) { |
| 5325 | lua_pushboolean(L, 0); |
| 5326 | return 1; |
| 5327 | } |
| 5328 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5329 | http_ctx->status = status; |
| 5330 | http_ctx->reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5331 | lua_pushboolean(L, 1); |
| 5332 | return 1; |
| 5333 | } |
| 5334 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5335 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5336 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5337 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5338 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5339 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5340 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5341 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5342 | struct htx *htx; |
| 5343 | struct htx_sl *sl; |
| 5344 | struct h1m h1m; |
| 5345 | const char *status, *reason; |
| 5346 | const char *name, *value; |
| 5347 | size_t nlen, vlen; |
| 5348 | unsigned int flags; |
| 5349 | |
| 5350 | /* Send the message at once. */ |
| 5351 | htx = htx_from_buf(&res->buf); |
| 5352 | h1m_init_res(&h1m); |
| 5353 | |
| 5354 | /* Use the same http version than the request. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5355 | status = ultoa_r(http_ctx->status, trash.area, trash.size); |
| 5356 | reason = http_ctx->reason; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5357 | if (reason == NULL) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5358 | reason = http_get_reason(http_ctx->status); |
| 5359 | if (http_ctx->flags & APPLET_HTTP11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5360 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5361 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 5362 | } |
| 5363 | else { |
| 5364 | flags = HTX_SL_F_IS_RESP; |
| 5365 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 5366 | } |
| 5367 | if (!sl) { |
| 5368 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5369 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5370 | WILL_LJMP(lua_error(L)); |
| 5371 | } |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5372 | sl->info.res.status = http_ctx->status; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5373 | |
| 5374 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 5375 | lua_pushvalue(L, 0); |
| 5376 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 5377 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5378 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5379 | WILL_LJMP(lua_error(L)); |
| 5380 | } |
| 5381 | |
| 5382 | /* Browse the list of headers. */ |
| 5383 | lua_pushnil(L); |
| 5384 | while(lua_next(L, -2) != 0) { |
| 5385 | /* We expect a string as -2. */ |
| 5386 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 5387 | 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] | 5388 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5389 | lua_typename(L, lua_type(L, -2))); |
| 5390 | WILL_LJMP(lua_error(L)); |
| 5391 | } |
| 5392 | name = lua_tolstring(L, -2, &nlen); |
| 5393 | |
| 5394 | /* We expect an array as -1. */ |
| 5395 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 5396 | 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] | 5397 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5398 | name, |
| 5399 | lua_typename(L, lua_type(L, -1))); |
| 5400 | WILL_LJMP(lua_error(L)); |
| 5401 | } |
| 5402 | |
| 5403 | /* Browse the table who is on the top of the stack. */ |
| 5404 | lua_pushnil(L); |
| 5405 | while(lua_next(L, -2) != 0) { |
| 5406 | int id; |
| 5407 | |
| 5408 | /* We expect a number as -2. */ |
| 5409 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 5410 | 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] | 5411 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5412 | name, |
| 5413 | lua_typename(L, lua_type(L, -2))); |
| 5414 | WILL_LJMP(lua_error(L)); |
| 5415 | } |
| 5416 | id = lua_tointeger(L, -2); |
| 5417 | |
| 5418 | /* We expect a string as -2. */ |
| 5419 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 5420 | 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] | 5421 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5422 | name, id, |
| 5423 | lua_typename(L, lua_type(L, -1))); |
| 5424 | WILL_LJMP(lua_error(L)); |
| 5425 | } |
| 5426 | value = lua_tolstring(L, -1, &vlen); |
| 5427 | |
| 5428 | /* Simple Protocol checks. */ |
Christopher Faulet | 545fbba | 2021-09-28 09:36:25 +0200 | [diff] [blame] | 5429 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) { |
| 5430 | int ret; |
| 5431 | |
| 5432 | ret = h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
| 5433 | if (ret < 0) { |
| 5434 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 5435 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
| 5436 | name); |
| 5437 | WILL_LJMP(lua_error(L)); |
| 5438 | } |
| 5439 | else if (ret == 0) |
| 5440 | goto next; /* Skip it */ |
| 5441 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5442 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 5443 | struct ist v = ist2(value, vlen); |
| 5444 | int ret; |
| 5445 | |
| 5446 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 5447 | if (ret < 0) { |
| 5448 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5449 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5450 | name); |
| 5451 | WILL_LJMP(lua_error(L)); |
| 5452 | } |
| 5453 | else if (ret == 0) |
| 5454 | goto next; /* Skip it */ |
| 5455 | } |
| 5456 | |
| 5457 | /* Add a new header */ |
| 5458 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 5459 | 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] | 5460 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5461 | name); |
| 5462 | WILL_LJMP(lua_error(L)); |
| 5463 | } |
| 5464 | next: |
| 5465 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5466 | lua_pop(L, 1); |
| 5467 | } |
| 5468 | |
| 5469 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5470 | lua_pop(L, 1); |
| 5471 | } |
| 5472 | |
| 5473 | if (h1m.flags & H1_MF_CHNK) |
| 5474 | h1m.flags &= ~H1_MF_CLEN; |
| 5475 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5476 | h1m.flags |= H1_MF_XFER_LEN; |
| 5477 | |
| 5478 | /* Uset HTX start-line flags */ |
| 5479 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5480 | flags |= HTX_SL_F_XFER_ENC; |
| 5481 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5482 | flags |= HTX_SL_F_XFER_LEN; |
| 5483 | if (h1m.flags & H1_MF_CHNK) |
| 5484 | flags |= HTX_SL_F_CHNK; |
| 5485 | else if (h1m.flags & H1_MF_CLEN) |
| 5486 | flags |= HTX_SL_F_CLEN; |
| 5487 | if (h1m.body_len == 0) |
| 5488 | flags |= HTX_SL_F_BODYLESS; |
| 5489 | } |
| 5490 | sl->flags |= flags; |
| 5491 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5492 | /* 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] | 5493 | * and the status code implies the presence of a message body, we must |
| 5494 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5495 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 5496 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5497 | */ |
| 5498 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5499 | http_ctx->status >= 200 && http_ctx->status != 204 && http_ctx->status != 304) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5500 | /* Add a new header */ |
| 5501 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 5502 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 5503 | 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] | 5504 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5505 | WILL_LJMP(lua_error(L)); |
| 5506 | } |
| 5507 | } |
| 5508 | |
| 5509 | /* Finalize headers. */ |
| 5510 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 5511 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5512 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5513 | WILL_LJMP(lua_error(L)); |
| 5514 | } |
| 5515 | |
| 5516 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 5517 | b_reset(&res->buf); |
| 5518 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 5519 | WILL_LJMP(lua_error(L)); |
| 5520 | } |
| 5521 | |
| 5522 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5523 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5524 | |
| 5525 | /* Headers sent, set the flag. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5526 | http_ctx->flags |= APPLET_HDR_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5527 | return 0; |
| 5528 | |
| 5529 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5530 | /* We will build the status line and the headers of the HTTP response. |
| 5531 | * We will try send at once if its not possible, we give back the hand |
| 5532 | * waiting for more room. |
| 5533 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5534 | __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] | 5535 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5536 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5537 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5538 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5539 | |
| 5540 | if (co_data(res)) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5541 | sc_need_room(sc); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5542 | 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] | 5543 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5544 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5545 | } |
| 5546 | |
| 5547 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5548 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5549 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5550 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5551 | } |
| 5552 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5553 | /* |
| 5554 | * |
| 5555 | * |
| 5556 | * Class HTTP |
| 5557 | * |
| 5558 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5559 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5560 | |
| 5561 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5562 | * a class stream, otherwise it throws an error. |
| 5563 | */ |
| 5564 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5565 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5566 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 5567 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5568 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5569 | /* This function creates and push in the stack a HTTP object |
| 5570 | * according with a current TXN. |
| 5571 | */ |
| 5572 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5573 | { |
| 5574 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5575 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5576 | /* Check stack size. */ |
| 5577 | if (!lua_checkstack(L, 3)) |
| 5578 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5579 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5580 | /* Create the object: obj[0] = userdata. |
| 5581 | * Note that the base of the Converters object is the |
| 5582 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5583 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5584 | lua_newtable(L); |
| 5585 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5586 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5587 | |
| 5588 | htxn->s = txn->s; |
| 5589 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 5590 | htxn->dir = txn->dir; |
| 5591 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5592 | |
| 5593 | /* Pop a class stream metatable and affect it to the table. */ |
| 5594 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5595 | lua_setmetatable(L, -2); |
| 5596 | |
| 5597 | return 1; |
| 5598 | } |
| 5599 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5600 | /* This function creates and returns an array containing the status-line |
| 5601 | * elements. This function does not fails. |
| 5602 | */ |
| 5603 | __LJMP static int hlua_http_get_stline(lua_State *L, struct htx_sl *sl) |
| 5604 | { |
| 5605 | /* Create the table. */ |
| 5606 | lua_newtable(L); |
| 5607 | |
| 5608 | if (sl->flags & HTX_SL_F_IS_RESP) { |
| 5609 | lua_pushstring(L, "version"); |
| 5610 | lua_pushlstring(L, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); |
| 5611 | lua_settable(L, -3); |
| 5612 | lua_pushstring(L, "code"); |
| 5613 | lua_pushlstring(L, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); |
| 5614 | lua_settable(L, -3); |
| 5615 | lua_pushstring(L, "reason"); |
| 5616 | lua_pushlstring(L, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); |
| 5617 | lua_settable(L, -3); |
| 5618 | } |
| 5619 | else { |
| 5620 | lua_pushstring(L, "method"); |
| 5621 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 5622 | lua_settable(L, -3); |
| 5623 | lua_pushstring(L, "uri"); |
| 5624 | lua_pushlstring(L, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); |
| 5625 | lua_settable(L, -3); |
| 5626 | lua_pushstring(L, "version"); |
| 5627 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 5628 | lua_settable(L, -3); |
| 5629 | } |
| 5630 | return 1; |
| 5631 | } |
| 5632 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5633 | /* This function creates ans returns an array of HTTP headers. |
| 5634 | * This function does not fails. It is used as wrapper with the |
| 5635 | * 2 following functions. |
| 5636 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5637 | __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] | 5638 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5639 | struct htx *htx; |
| 5640 | int32_t pos; |
| 5641 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5642 | /* Create the table. */ |
| 5643 | lua_newtable(L); |
| 5644 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5645 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5646 | htx = htxbuf(&msg->chn->buf); |
| 5647 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5648 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5649 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5650 | struct ist n, v; |
| 5651 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5652 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5653 | if (type == HTX_BLK_HDR) { |
| 5654 | n = htx_get_blk_name(htx,blk); |
| 5655 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5656 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5657 | else if (type == HTX_BLK_EOH) |
| 5658 | break; |
| 5659 | else |
| 5660 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5661 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5662 | /* Check for existing entry: |
| 5663 | * assume that the table is on the top of the stack, and |
| 5664 | * push the key in the stack, the function lua_gettable() |
| 5665 | * perform the lookup. |
| 5666 | */ |
| 5667 | lua_pushlstring(L, n.ptr, n.len); |
| 5668 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5669 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5670 | switch (lua_type(L, -1)) { |
| 5671 | case LUA_TNIL: |
| 5672 | /* Table not found, create it. */ |
| 5673 | lua_pop(L, 1); /* remove the nil value. */ |
| 5674 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5675 | lua_newtable(L); /* create and push empty table. */ |
| 5676 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5677 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5678 | 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] | 5679 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5680 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5681 | case LUA_TTABLE: |
| 5682 | /* Entry found: push the value in the table. */ |
| 5683 | len = lua_rawlen(L, -1); |
| 5684 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5685 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5686 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5687 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5688 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5689 | default: |
| 5690 | /* Other cases are errors. */ |
| 5691 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5692 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5693 | } |
| 5694 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5695 | return 1; |
| 5696 | } |
| 5697 | |
| 5698 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5699 | { |
| 5700 | struct hlua_txn *htxn; |
| 5701 | |
| 5702 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5703 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5704 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5705 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5706 | WILL_LJMP(lua_error(L)); |
| 5707 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5708 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5709 | } |
| 5710 | |
| 5711 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5712 | { |
| 5713 | struct hlua_txn *htxn; |
| 5714 | |
| 5715 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5716 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5717 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5718 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5719 | WILL_LJMP(lua_error(L)); |
| 5720 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5721 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5722 | } |
| 5723 | |
| 5724 | /* This function replace full header, or just a value in |
| 5725 | * the request or in the response. It is a wrapper fir the |
| 5726 | * 4 following functions. |
| 5727 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5728 | __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] | 5729 | { |
| 5730 | size_t name_len; |
| 5731 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5732 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5733 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5734 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5735 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5736 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5737 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5738 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5739 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5740 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5741 | 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] | 5742 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5743 | return 0; |
| 5744 | } |
| 5745 | |
| 5746 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5747 | { |
| 5748 | struct hlua_txn *htxn; |
| 5749 | |
| 5750 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5751 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5752 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5753 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5754 | WILL_LJMP(lua_error(L)); |
| 5755 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5756 | 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] | 5757 | } |
| 5758 | |
| 5759 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5760 | { |
| 5761 | struct hlua_txn *htxn; |
| 5762 | |
| 5763 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5764 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5765 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5766 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5767 | WILL_LJMP(lua_error(L)); |
| 5768 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5769 | 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] | 5770 | } |
| 5771 | |
| 5772 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5773 | { |
| 5774 | struct hlua_txn *htxn; |
| 5775 | |
| 5776 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5777 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5778 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5779 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5780 | WILL_LJMP(lua_error(L)); |
| 5781 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5782 | 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] | 5783 | } |
| 5784 | |
| 5785 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5786 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5787 | struct hlua_txn *htxn; |
| 5788 | |
| 5789 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5790 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5791 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5792 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5793 | WILL_LJMP(lua_error(L)); |
| 5794 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5795 | 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] | 5796 | } |
| 5797 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5798 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5799 | * It is a wrapper for the 2 following functions. |
| 5800 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5801 | __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] | 5802 | { |
| 5803 | size_t len; |
| 5804 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5805 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5806 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5807 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5808 | ctx.blk = NULL; |
| 5809 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5810 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5811 | return 0; |
| 5812 | } |
| 5813 | |
| 5814 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5815 | { |
| 5816 | struct hlua_txn *htxn; |
| 5817 | |
| 5818 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5819 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5820 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5821 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5822 | WILL_LJMP(lua_error(L)); |
| 5823 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5824 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5825 | } |
| 5826 | |
| 5827 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5828 | { |
| 5829 | struct hlua_txn *htxn; |
| 5830 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5831 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5832 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5833 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5834 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5835 | WILL_LJMP(lua_error(L)); |
| 5836 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5837 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5838 | } |
| 5839 | |
| 5840 | /* This function adds an header. It is a wrapper used by |
| 5841 | * the 2 following functions. |
| 5842 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5843 | __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] | 5844 | { |
| 5845 | size_t name_len; |
| 5846 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5847 | size_t value_len; |
| 5848 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5849 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5850 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5851 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5852 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5853 | return 0; |
| 5854 | } |
| 5855 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5856 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5857 | { |
| 5858 | struct hlua_txn *htxn; |
| 5859 | |
| 5860 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5861 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5862 | |
| 5863 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5864 | WILL_LJMP(lua_error(L)); |
| 5865 | |
| 5866 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 5867 | } |
| 5868 | |
| 5869 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5870 | { |
| 5871 | struct hlua_txn *htxn; |
| 5872 | |
| 5873 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5874 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5875 | |
| 5876 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5877 | WILL_LJMP(lua_error(L)); |
| 5878 | |
| 5879 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 5880 | } |
| 5881 | |
| 5882 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5883 | { |
| 5884 | struct hlua_txn *htxn; |
| 5885 | |
| 5886 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5887 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5888 | |
| 5889 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5890 | WILL_LJMP(lua_error(L)); |
| 5891 | |
| 5892 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5893 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 5894 | } |
| 5895 | |
| 5896 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5897 | { |
| 5898 | struct hlua_txn *htxn; |
| 5899 | |
| 5900 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5901 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5902 | |
| 5903 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5904 | WILL_LJMP(lua_error(L)); |
| 5905 | |
| 5906 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5907 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 5908 | } |
| 5909 | |
| 5910 | /* This function set the method. */ |
| 5911 | static int hlua_http_req_set_meth(lua_State *L) |
| 5912 | { |
| 5913 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5914 | size_t name_len; |
| 5915 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5916 | |
| 5917 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5918 | WILL_LJMP(lua_error(L)); |
| 5919 | |
| 5920 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1); |
| 5921 | return 1; |
| 5922 | } |
| 5923 | |
| 5924 | /* This function set the method. */ |
| 5925 | static int hlua_http_req_set_path(lua_State *L) |
| 5926 | { |
| 5927 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5928 | size_t name_len; |
| 5929 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5930 | |
| 5931 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5932 | WILL_LJMP(lua_error(L)); |
| 5933 | |
| 5934 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1); |
| 5935 | return 1; |
| 5936 | } |
| 5937 | |
| 5938 | /* This function set the query-string. */ |
| 5939 | static int hlua_http_req_set_query(lua_State *L) |
| 5940 | { |
| 5941 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5942 | size_t name_len; |
| 5943 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5944 | |
| 5945 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5946 | WILL_LJMP(lua_error(L)); |
| 5947 | |
| 5948 | /* Check length. */ |
| 5949 | if (name_len > trash.size - 1) { |
| 5950 | lua_pushboolean(L, 0); |
| 5951 | return 1; |
| 5952 | } |
| 5953 | |
| 5954 | /* Add the mark question as prefix. */ |
| 5955 | chunk_reset(&trash); |
| 5956 | trash.area[trash.data++] = '?'; |
| 5957 | memcpy(trash.area + trash.data, name, name_len); |
| 5958 | trash.data += name_len; |
| 5959 | |
| 5960 | lua_pushboolean(L, |
| 5961 | http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1); |
| 5962 | return 1; |
| 5963 | } |
| 5964 | |
| 5965 | /* This function set the uri. */ |
| 5966 | static int hlua_http_req_set_uri(lua_State *L) |
| 5967 | { |
| 5968 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5969 | size_t name_len; |
| 5970 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5971 | |
| 5972 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5973 | WILL_LJMP(lua_error(L)); |
| 5974 | |
| 5975 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1); |
| 5976 | return 1; |
| 5977 | } |
| 5978 | |
| 5979 | /* This function set the response code & optionally reason. */ |
| 5980 | static int hlua_http_res_set_status(lua_State *L) |
| 5981 | { |
| 5982 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5983 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5984 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5985 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
| 5986 | |
| 5987 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5988 | WILL_LJMP(lua_error(L)); |
| 5989 | |
| 5990 | http_res_set_status(code, reason, htxn->s); |
| 5991 | return 0; |
| 5992 | } |
| 5993 | |
| 5994 | /* |
| 5995 | * |
| 5996 | * |
| 5997 | * Class HTTPMessage |
| 5998 | * |
| 5999 | * |
| 6000 | */ |
| 6001 | |
| 6002 | /* Returns a struct http_msg if the stack entry "ud" is a class HTTPMessage, |
| 6003 | * otherwise it throws an error. |
| 6004 | */ |
| 6005 | __LJMP static struct http_msg *hlua_checkhttpmsg(lua_State *L, int ud) |
| 6006 | { |
| 6007 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_msg_ref)); |
| 6008 | } |
| 6009 | |
| 6010 | /* Creates and pushes on the stack a HTTP object according with a current TXN. |
| 6011 | */ |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 6012 | 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] | 6013 | { |
| 6014 | /* Check stack size. */ |
| 6015 | if (!lua_checkstack(L, 3)) |
| 6016 | return 0; |
| 6017 | |
| 6018 | lua_newtable(L); |
| 6019 | lua_pushlightuserdata(L, msg); |
| 6020 | lua_rawseti(L, -2, 0); |
| 6021 | |
| 6022 | /* Create the "channel" field that contains the request channel object. */ |
| 6023 | lua_pushstring(L, "channel"); |
| 6024 | if (!hlua_channel_new(L, msg->chn)) |
| 6025 | return 0; |
| 6026 | lua_rawset(L, -3); |
| 6027 | |
| 6028 | /* Pop a class stream metatable and affect it to the table. */ |
| 6029 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_msg_ref); |
| 6030 | lua_setmetatable(L, -2); |
| 6031 | |
| 6032 | return 1; |
| 6033 | } |
| 6034 | |
| 6035 | /* Helper function returning a filter attached to the HTTP message at the |
| 6036 | * 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] | 6037 | * 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] | 6038 | * filled with output and input length respectively. |
| 6039 | */ |
| 6040 | static struct filter *hlua_http_msg_filter(lua_State *L, int ud, struct http_msg *msg, size_t *offset, size_t *len) |
| 6041 | { |
| 6042 | struct channel *chn = msg->chn; |
| 6043 | struct htx *htx = htxbuf(&chn->buf); |
| 6044 | struct filter *filter = NULL; |
| 6045 | |
| 6046 | *offset = co_data(msg->chn); |
| 6047 | *len = htx->data - co_data(msg->chn); |
| 6048 | |
| 6049 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 6050 | filter = lua_touserdata (L, -1); |
| 6051 | if (msg->msg_state >= HTTP_MSG_DATA) { |
| 6052 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6053 | |
| 6054 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 6055 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 6056 | } |
| 6057 | } |
| 6058 | |
| 6059 | lua_pop(L, 1); |
| 6060 | return filter; |
| 6061 | } |
| 6062 | |
| 6063 | /* Returns true if the channel attached to the HTTP message is the response |
| 6064 | * channel. |
| 6065 | */ |
| 6066 | __LJMP static int hlua_http_msg_is_resp(lua_State *L) |
| 6067 | { |
| 6068 | struct http_msg *msg; |
| 6069 | |
| 6070 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 6071 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6072 | |
| 6073 | lua_pushboolean(L, !!(msg->chn->flags & CF_ISRESP)); |
| 6074 | return 1; |
| 6075 | } |
| 6076 | |
| 6077 | /* Returns an array containing the elements status-line of the HTTP message. It relies |
| 6078 | * on hlua_http_get_stline(). |
| 6079 | */ |
| 6080 | __LJMP static int hlua_http_msg_get_stline(lua_State *L) |
| 6081 | { |
| 6082 | struct http_msg *msg; |
| 6083 | struct htx *htx; |
| 6084 | struct htx_sl *sl; |
| 6085 | |
| 6086 | MAY_LJMP(check_args(L, 1, "get_stline")); |
| 6087 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6088 | |
| 6089 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6090 | WILL_LJMP(lua_error(L)); |
| 6091 | |
| 6092 | htx = htxbuf(&msg->chn->buf); |
| 6093 | sl = http_get_stline(htx); |
| 6094 | if (!sl) |
| 6095 | return 0; |
| 6096 | return hlua_http_get_stline(L, sl); |
| 6097 | } |
| 6098 | |
| 6099 | /* Returns an array containing all headers of the HTTP message. it relies on |
| 6100 | * hlua_http_get_headers(). |
| 6101 | */ |
| 6102 | __LJMP static int hlua_http_msg_get_headers(lua_State *L) |
| 6103 | { |
| 6104 | struct http_msg *msg; |
| 6105 | |
| 6106 | MAY_LJMP(check_args(L, 1, "get_headers")); |
| 6107 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6108 | |
| 6109 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6110 | WILL_LJMP(lua_error(L)); |
| 6111 | |
| 6112 | return hlua_http_get_headers(L, msg); |
| 6113 | } |
| 6114 | |
| 6115 | /* Deletes all occurrences of an header in the HTTP message matching on its |
| 6116 | * name. It relies on hlua_http_del_hdr(). |
| 6117 | */ |
| 6118 | __LJMP static int hlua_http_msg_del_hdr(lua_State *L) |
| 6119 | { |
| 6120 | struct http_msg *msg; |
| 6121 | |
| 6122 | MAY_LJMP(check_args(L, 2, "del_header")); |
| 6123 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6124 | |
| 6125 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6126 | WILL_LJMP(lua_error(L)); |
| 6127 | |
| 6128 | return hlua_http_del_hdr(L, msg); |
| 6129 | } |
| 6130 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6131 | /* 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] | 6132 | * message given its name against a regex and replaces it if it matches. It |
| 6133 | * relies on hlua_http_rep_hdr(). |
| 6134 | */ |
| 6135 | __LJMP static int hlua_http_msg_rep_hdr(lua_State *L) |
| 6136 | { |
| 6137 | struct http_msg *msg; |
| 6138 | |
| 6139 | MAY_LJMP(check_args(L, 4, "rep_header")); |
| 6140 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6141 | |
| 6142 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6143 | WILL_LJMP(lua_error(L)); |
| 6144 | |
| 6145 | return hlua_http_rep_hdr(L, msg, 1); |
| 6146 | } |
| 6147 | |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 6148 | /* 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] | 6149 | * message given its name against a regex and replaces it if it matches. It |
| 6150 | * relies on hlua_http_rep_hdr(). |
| 6151 | */ |
| 6152 | __LJMP static int hlua_http_msg_rep_val(lua_State *L) |
| 6153 | { |
| 6154 | struct http_msg *msg; |
| 6155 | |
| 6156 | MAY_LJMP(check_args(L, 4, "rep_value")); |
| 6157 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6158 | |
| 6159 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6160 | WILL_LJMP(lua_error(L)); |
| 6161 | |
| 6162 | return hlua_http_rep_hdr(L, msg, 0); |
| 6163 | } |
| 6164 | |
| 6165 | /* Add an header in the HTTP message. It relies on hlua_http_add_hdr() */ |
| 6166 | __LJMP static int hlua_http_msg_add_hdr(lua_State *L) |
| 6167 | { |
| 6168 | struct http_msg *msg; |
| 6169 | |
| 6170 | MAY_LJMP(check_args(L, 3, "add_header")); |
| 6171 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6172 | |
| 6173 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6174 | WILL_LJMP(lua_error(L)); |
| 6175 | |
| 6176 | return hlua_http_add_hdr(L, msg); |
| 6177 | } |
| 6178 | |
| 6179 | /* Add an header in the HTTP message removing existing headers with the same |
| 6180 | * name. It relies on hlua_http_del_hdr() and hlua_http_add_hdr(). |
| 6181 | */ |
| 6182 | __LJMP static int hlua_http_msg_set_hdr(lua_State *L) |
| 6183 | { |
| 6184 | struct http_msg *msg; |
| 6185 | |
| 6186 | MAY_LJMP(check_args(L, 3, "set_header")); |
| 6187 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6188 | |
| 6189 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6190 | WILL_LJMP(lua_error(L)); |
| 6191 | |
| 6192 | hlua_http_del_hdr(L, msg); |
| 6193 | return hlua_http_add_hdr(L, msg); |
| 6194 | } |
| 6195 | |
| 6196 | /* Rewrites the request method. It relies on http_req_replace_stline(). */ |
| 6197 | __LJMP static int hlua_http_msg_set_meth(lua_State *L) |
| 6198 | { |
| 6199 | struct stream *s; |
| 6200 | struct http_msg *msg; |
| 6201 | const char *name; |
| 6202 | size_t name_len; |
| 6203 | |
| 6204 | MAY_LJMP(check_args(L, 2, "set_method")); |
| 6205 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6206 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6207 | |
| 6208 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6209 | WILL_LJMP(lua_error(L)); |
| 6210 | |
| 6211 | s = chn_strm(msg->chn); |
| 6212 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, s->be, s) != -1); |
| 6213 | return 1; |
| 6214 | } |
| 6215 | |
| 6216 | /* Rewrites the request path. It relies on http_req_replace_stline(). */ |
| 6217 | __LJMP static int hlua_http_msg_set_path(lua_State *L) |
| 6218 | { |
| 6219 | struct stream *s; |
| 6220 | struct http_msg *msg; |
| 6221 | const char *name; |
| 6222 | size_t name_len; |
| 6223 | |
| 6224 | MAY_LJMP(check_args(L, 2, "set_path")); |
| 6225 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6226 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6227 | |
| 6228 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6229 | WILL_LJMP(lua_error(L)); |
| 6230 | |
| 6231 | s = chn_strm(msg->chn); |
| 6232 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, s->be, s) != -1); |
| 6233 | return 1; |
| 6234 | } |
| 6235 | |
| 6236 | /* Rewrites the request query-string. It relies on http_req_replace_stline(). */ |
| 6237 | __LJMP static int hlua_http_msg_set_query(lua_State *L) |
| 6238 | { |
| 6239 | struct stream *s; |
| 6240 | struct http_msg *msg; |
| 6241 | const char *name; |
| 6242 | size_t name_len; |
| 6243 | |
| 6244 | MAY_LJMP(check_args(L, 2, "set_query")); |
| 6245 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6246 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6247 | |
| 6248 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6249 | WILL_LJMP(lua_error(L)); |
| 6250 | |
| 6251 | /* Check length. */ |
| 6252 | if (name_len > trash.size - 1) { |
| 6253 | lua_pushboolean(L, 0); |
| 6254 | return 1; |
| 6255 | } |
| 6256 | |
| 6257 | /* Add the mark question as prefix. */ |
| 6258 | chunk_reset(&trash); |
| 6259 | trash.area[trash.data++] = '?'; |
| 6260 | memcpy(trash.area + trash.data, name, name_len); |
| 6261 | trash.data += name_len; |
| 6262 | |
| 6263 | s = chn_strm(msg->chn); |
| 6264 | lua_pushboolean(L, http_req_replace_stline(2, trash.area, trash.data, s->be, s) != -1); |
| 6265 | return 1; |
| 6266 | } |
| 6267 | |
| 6268 | /* Rewrites the request URI. It relies on http_req_replace_stline(). */ |
| 6269 | __LJMP static int hlua_http_msg_set_uri(lua_State *L) |
| 6270 | { |
| 6271 | struct stream *s; |
| 6272 | struct http_msg *msg; |
| 6273 | const char *name; |
| 6274 | size_t name_len; |
| 6275 | |
| 6276 | MAY_LJMP(check_args(L, 2, "set_uri")); |
| 6277 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6278 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6279 | |
| 6280 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6281 | WILL_LJMP(lua_error(L)); |
| 6282 | |
| 6283 | s = chn_strm(msg->chn); |
| 6284 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, s->be, s) != -1); |
| 6285 | return 1; |
| 6286 | } |
| 6287 | |
| 6288 | /* Rewrites the response status code. It relies on http_res_set_status(). */ |
| 6289 | __LJMP static int hlua_http_msg_set_status(lua_State *L) |
| 6290 | { |
| 6291 | struct http_msg *msg; |
| 6292 | unsigned int code; |
| 6293 | const char *reason; |
| 6294 | size_t reason_len; |
| 6295 | |
| 6296 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6297 | code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6298 | reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, &reason_len)); |
| 6299 | |
| 6300 | if (!(msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6301 | WILL_LJMP(lua_error(L)); |
| 6302 | |
| 6303 | lua_pushboolean(L, http_res_set_status(code, ist2(reason, reason_len), chn_strm(msg->chn)) != -1); |
| 6304 | return 1; |
| 6305 | } |
| 6306 | |
| 6307 | /* Returns true if the HTTP message is full. */ |
| 6308 | __LJMP static int hlua_http_msg_is_full(lua_State *L) |
| 6309 | { |
| 6310 | struct http_msg *msg; |
| 6311 | |
| 6312 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 6313 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6314 | lua_pushboolean(L, channel_full(msg->chn, 0)); |
| 6315 | return 1; |
| 6316 | } |
| 6317 | |
| 6318 | /* Returns true if the HTTP message may still receive data. */ |
| 6319 | __LJMP static int hlua_http_msg_may_recv(lua_State *L) |
| 6320 | { |
| 6321 | struct http_msg *msg; |
| 6322 | struct htx *htx; |
| 6323 | |
| 6324 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6325 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6326 | htx = htxbuf(&msg->chn->buf); |
| 6327 | lua_pushboolean(L, (htx_expect_more(htx) && !channel_input_closed(msg->chn) && channel_may_recv(msg->chn))); |
| 6328 | return 1; |
| 6329 | } |
| 6330 | |
| 6331 | /* Returns true if the HTTP message EOM was received */ |
| 6332 | __LJMP static int hlua_http_msg_is_eom(lua_State *L) |
| 6333 | { |
| 6334 | struct http_msg *msg; |
| 6335 | struct htx *htx; |
| 6336 | |
| 6337 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6338 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6339 | htx = htxbuf(&msg->chn->buf); |
| 6340 | lua_pushboolean(L, !htx_expect_more(htx)); |
| 6341 | return 1; |
| 6342 | } |
| 6343 | |
| 6344 | /* Returns the number of bytes available in the input side of the HTTP |
| 6345 | * message. This function never fails. |
| 6346 | */ |
| 6347 | __LJMP static int hlua_http_msg_get_in_len(lua_State *L) |
| 6348 | { |
| 6349 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6350 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6351 | |
| 6352 | MAY_LJMP(check_args(L, 1, "input")); |
| 6353 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6354 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6355 | lua_pushinteger(L, input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6356 | return 1; |
| 6357 | } |
| 6358 | |
| 6359 | /* Returns the number of bytes available in the output side of the HTTP |
| 6360 | * message. This function never fails. |
| 6361 | */ |
| 6362 | __LJMP static int hlua_http_msg_get_out_len(lua_State *L) |
| 6363 | { |
| 6364 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6365 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6366 | |
| 6367 | MAY_LJMP(check_args(L, 1, "output")); |
| 6368 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6369 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6370 | lua_pushinteger(L, output); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6371 | return 1; |
| 6372 | } |
| 6373 | |
| 6374 | /* Copies at most <len> bytes of DATA blocks from the HTTP message <msg> |
| 6375 | * 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] | 6376 | * 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] | 6377 | * block. This function is called during the payload filtering, so the headers |
| 6378 | * are already scheduled for output (from the filter point of view). |
| 6379 | */ |
| 6380 | static int _hlua_http_msg_dup(struct http_msg *msg, lua_State *L, size_t offset, size_t len) |
| 6381 | { |
| 6382 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 6383 | struct htx_blk *blk; |
| 6384 | struct htx_ret htxret; |
| 6385 | luaL_Buffer b; |
| 6386 | int ret = 0; |
| 6387 | |
| 6388 | luaL_buffinit(L, &b); |
| 6389 | htxret = htx_find_offset(htx, offset); |
| 6390 | for (blk = htxret.blk, offset = htxret.ret; blk && len; blk = htx_get_next_blk(htx, blk)) { |
| 6391 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6392 | struct ist v; |
| 6393 | |
| 6394 | switch (type) { |
| 6395 | case HTX_BLK_UNUSED: |
| 6396 | break; |
| 6397 | |
| 6398 | case HTX_BLK_DATA: |
| 6399 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6400 | v = istadv(v, offset); |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6401 | v = isttrim(v, len); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6402 | |
| 6403 | luaL_addlstring(&b, v.ptr, v.len); |
| 6404 | ret += v.len; |
| 6405 | break; |
| 6406 | |
| 6407 | default: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6408 | if (!ret) |
| 6409 | goto no_data; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6410 | goto end; |
| 6411 | } |
| 6412 | offset = 0; |
| 6413 | } |
| 6414 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6415 | end: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6416 | if (!ret && (htx->flags & HTX_FL_EOM)) |
| 6417 | goto no_data; |
| 6418 | luaL_pushresult(&b); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6419 | return ret; |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6420 | |
| 6421 | no_data: |
| 6422 | /* Remove the empty string and push nil on the stack */ |
| 6423 | lua_pop(L, 1); |
| 6424 | lua_pushnil(L); |
| 6425 | return 0; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6426 | } |
| 6427 | |
| 6428 | /* Copies the string <str> to the HTTP message <msg> at the offset |
| 6429 | * <offset>. This function returns -1 if data cannot be copied. Otherwise, it |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6430 | * returns the amount of data written. This function is responsible to update |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6431 | * the filter context. |
| 6432 | */ |
| 6433 | static int _hlua_http_msg_insert(struct http_msg *msg, struct filter *filter, struct ist str, size_t offset) |
| 6434 | { |
| 6435 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6436 | struct htx_ret htxret; |
| 6437 | int /*max, */ret = 0; |
| 6438 | |
| 6439 | /* Nothing to do, just return */ |
| 6440 | if (unlikely(istlen(str) == 0)) |
| 6441 | goto end; |
| 6442 | |
| 6443 | if (istlen(str) > htx_free_data_space(htx)) { |
| 6444 | ret = -1; |
| 6445 | goto end; |
| 6446 | } |
| 6447 | |
| 6448 | htxret = htx_find_offset(htx, offset); |
| 6449 | if (!htxret.blk || htx_get_blk_type(htxret.blk) != HTX_BLK_DATA) { |
| 6450 | if (!htx_add_last_data(htx, str)) |
| 6451 | goto end; |
| 6452 | } |
| 6453 | else { |
| 6454 | struct ist v = htx_get_blk_value(htx, htxret.blk); |
| 6455 | v.ptr += htxret.ret; |
| 6456 | v.len = 0; |
| 6457 | if (!htx_replace_blk_value(htx, htxret.blk, v, str)) |
| 6458 | goto end; |
| 6459 | } |
| 6460 | ret = str.len; |
| 6461 | if (ret) { |
| 6462 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6463 | flt_update_offsets(filter, msg->chn, ret); |
| 6464 | flt_ctx->cur_len[CHN_IDX(msg->chn)] += ret; |
| 6465 | } |
| 6466 | |
| 6467 | end: |
| 6468 | htx_to_buf(htx, &msg->chn->buf); |
| 6469 | return ret; |
| 6470 | } |
| 6471 | |
| 6472 | /* Helper function removing at most <len> bytes of DATA blocks at the absolute |
| 6473 | * position <offset>. It stops on the first non-DATA HTX block. This function is |
| 6474 | * called during the payload filtering, so the headers are already scheduled for |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6475 | * output (from the filter point of view). This function is responsible to |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6476 | * update the filter context. |
| 6477 | */ |
| 6478 | static void _hlua_http_msg_delete(struct http_msg *msg, struct filter *filter, size_t offset, size_t len) |
| 6479 | { |
| 6480 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6481 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6482 | struct htx_blk *blk; |
| 6483 | struct htx_ret htxret; |
| 6484 | size_t ret = 0; |
| 6485 | |
| 6486 | /* Be sure <len> is always the amount of DATA to remove */ |
| 6487 | if (htx->data == offset+len && htx_get_tail_type(htx) == HTX_BLK_DATA) { |
| 6488 | htx_truncate(htx, offset); |
| 6489 | ret = len; |
| 6490 | goto end; |
| 6491 | } |
| 6492 | |
| 6493 | htxret = htx_find_offset(htx, offset); |
| 6494 | blk = htxret.blk; |
| 6495 | if (htxret.ret) { |
| 6496 | struct ist v; |
| 6497 | |
| 6498 | if (htx_get_blk_type(blk) != HTX_BLK_DATA) |
| 6499 | goto end; |
| 6500 | v = htx_get_blk_value(htx, blk); |
| 6501 | v.ptr += htxret.ret; |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6502 | v = isttrim(v, len); |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6503 | blk = htx_replace_blk_value(htx, blk, v, IST_NULL); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6504 | len -= v.len; |
| 6505 | ret += v.len; |
| 6506 | } |
| 6507 | |
| 6508 | |
| 6509 | while (blk && len) { |
| 6510 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6511 | uint32_t sz = htx_get_blksz(blk); |
| 6512 | |
| 6513 | switch (type) { |
| 6514 | case HTX_BLK_UNUSED: |
| 6515 | break; |
| 6516 | |
| 6517 | case HTX_BLK_DATA: |
| 6518 | if (len < sz) { |
| 6519 | htx_cut_data_blk(htx, blk, len); |
| 6520 | ret += len; |
| 6521 | goto end; |
| 6522 | } |
| 6523 | break; |
| 6524 | |
| 6525 | default: |
| 6526 | goto end; |
| 6527 | } |
| 6528 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6529 | /* Remove all the data block */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6530 | len -= sz; |
| 6531 | ret += sz; |
| 6532 | blk = htx_remove_blk(htx, blk); |
| 6533 | } |
| 6534 | |
| 6535 | end: |
| 6536 | flt_update_offsets(filter, msg->chn, -ret); |
| 6537 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6538 | /* WARNING: we don't call htx_to_buf() on purpose, because we don't want |
| 6539 | * to loose the EOM flag if the message is empty. |
| 6540 | */ |
| 6541 | } |
| 6542 | |
| 6543 | /* Copies input data found in an HTTP message. Unlike the channel function used |
| 6544 | * to duplicate raw data, this one can only be called inside a filter, from |
| 6545 | * http_payload callback. So it cannot yield. An exception is returned if it is |
| 6546 | * called from another callback. If nothing was copied, a nil value is pushed on |
| 6547 | * the stack. |
| 6548 | */ |
| 6549 | __LJMP static int hlua_http_msg_get_body(lua_State *L) |
| 6550 | { |
| 6551 | struct http_msg *msg; |
| 6552 | struct filter *filter; |
| 6553 | size_t output, input; |
| 6554 | int offset, len; |
| 6555 | |
| 6556 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 6557 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 6558 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6559 | |
| 6560 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6561 | WILL_LJMP(lua_error(L)); |
| 6562 | |
| 6563 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6564 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6565 | WILL_LJMP(lua_error(L)); |
| 6566 | |
| 6567 | if (!ci_data(msg->chn) && channel_input_closed(msg->chn)) { |
| 6568 | lua_pushnil(L); |
| 6569 | return 1; |
| 6570 | } |
| 6571 | |
| 6572 | offset = output; |
| 6573 | if (lua_gettop(L) > 1) { |
| 6574 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6575 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6576 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6577 | offset += output; |
| 6578 | if (offset < output || offset > input + output) { |
| 6579 | lua_pushfstring(L, "offset out of range."); |
| 6580 | WILL_LJMP(lua_error(L)); |
| 6581 | } |
| 6582 | } |
| 6583 | len = output + input - offset; |
| 6584 | if (lua_gettop(L) == 3) { |
| 6585 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6586 | if (!len) |
| 6587 | goto dup; |
| 6588 | if (len == -1) |
| 6589 | len = global.tune.bufsize; |
| 6590 | if (len < 0) { |
| 6591 | lua_pushfstring(L, "length out of range."); |
| 6592 | WILL_LJMP(lua_error(L)); |
| 6593 | } |
| 6594 | } |
| 6595 | |
| 6596 | dup: |
| 6597 | _hlua_http_msg_dup(msg, L, offset, len); |
| 6598 | return 1; |
| 6599 | } |
| 6600 | |
| 6601 | /* Appends a string to the HTTP message, after all existing DATA blocks but |
| 6602 | * before the trailers, if any. It returns the amount of data written or -1 if |
| 6603 | * nothing was copied. Unlike the channel function used to append data, this one |
| 6604 | * can only be called inside a filter, from http_payload callback. So it cannot |
| 6605 | * yield. An exception is returned if it is called from another callback. |
| 6606 | */ |
| 6607 | __LJMP static int hlua_http_msg_append(lua_State *L) |
| 6608 | { |
| 6609 | struct http_msg *msg; |
| 6610 | struct filter *filter; |
| 6611 | const char *str; |
| 6612 | size_t offset, len, sz; |
| 6613 | int ret; |
| 6614 | |
| 6615 | MAY_LJMP(check_args(L, 2, "append")); |
| 6616 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6617 | |
| 6618 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6619 | WILL_LJMP(lua_error(L)); |
| 6620 | |
| 6621 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6622 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6623 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6624 | WILL_LJMP(lua_error(L)); |
| 6625 | |
| 6626 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset+len); |
| 6627 | lua_pushinteger(L, ret); |
| 6628 | return 1; |
| 6629 | } |
| 6630 | |
| 6631 | /* Prepends a string to the HTTP message, before all existing DATA blocks. It |
| 6632 | * returns the amount of data written or -1 if nothing was copied. Unlike the |
| 6633 | * channel function used to prepend data, this one can only be called inside a |
| 6634 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6635 | * returned if it is called from another callback. |
| 6636 | */ |
| 6637 | __LJMP static int hlua_http_msg_prepend(lua_State *L) |
| 6638 | { |
| 6639 | struct http_msg *msg; |
| 6640 | struct filter *filter; |
| 6641 | const char *str; |
| 6642 | size_t offset, len, sz; |
| 6643 | int ret; |
| 6644 | |
| 6645 | MAY_LJMP(check_args(L, 2, "prepend")); |
| 6646 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6647 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6648 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6649 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6650 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6651 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6652 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6653 | if (!filter || !hlua_filter_from_payload(filter)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6654 | WILL_LJMP(lua_error(L)); |
| 6655 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6656 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6657 | lua_pushinteger(L, ret); |
| 6658 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6659 | } |
| 6660 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6661 | /* Inserts a string to the HTTP message at a given offset. By default the string |
| 6662 | * is appended at the end of DATA blocks. It returns the amount of data written |
| 6663 | * or -1 if nothing was copied. Unlike the channel function used to insert data, |
| 6664 | * this one can only be called inside a filter, from http_payload callback. So |
| 6665 | * it cannot yield. An exception is returned if it is called from another |
| 6666 | * callback. |
| 6667 | */ |
| 6668 | __LJMP static int hlua_http_msg_insert_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6669 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6670 | struct http_msg *msg; |
| 6671 | struct filter *filter; |
| 6672 | const char *str; |
| 6673 | size_t input, output, sz; |
| 6674 | int offset; |
| 6675 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6676 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6677 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 6678 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
| 6679 | MAY_LJMP(check_args(L, 2, "insert")); |
| 6680 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6681 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6682 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6683 | WILL_LJMP(lua_error(L)); |
| 6684 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6685 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6686 | filter = hlua_http_msg_filter(L, 1, msg, &input, &output); |
| 6687 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6688 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6689 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6690 | offset = input + output; |
| 6691 | if (lua_gettop(L) > 2) { |
| 6692 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6693 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6694 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6695 | offset += output; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6696 | if (offset < output || offset > output + input) { |
| 6697 | lua_pushfstring(L, "offset out of range."); |
| 6698 | WILL_LJMP(lua_error(L)); |
| 6699 | } |
| 6700 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6701 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6702 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6703 | lua_pushinteger(L, ret); |
| 6704 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6705 | } |
| 6706 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6707 | /* Removes a given amount of data from the HTTP message at a given offset. By |
| 6708 | * default all DATA blocks are removed. It returns the amount of data |
| 6709 | * removed. Unlike the channel function used to remove data, this one can only |
| 6710 | * be called inside a filter, from http_payload callback. So it cannot yield. An |
| 6711 | * exception is returned if it is called from another callback. |
| 6712 | */ |
| 6713 | __LJMP static int hlua_http_msg_del_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6714 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6715 | struct http_msg *msg; |
| 6716 | struct filter *filter; |
| 6717 | size_t input, output; |
| 6718 | int offset, len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6719 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6720 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6721 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6722 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6723 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6724 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6725 | WILL_LJMP(lua_error(L)); |
| 6726 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6727 | filter = hlua_http_msg_filter(L, 1, msg, &input, &output); |
| 6728 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6729 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6730 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6731 | offset = input + output; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6732 | if (lua_gettop(L) > 1) { |
| 6733 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6734 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6735 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6736 | offset += output; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6737 | if (offset < output || offset > output + input) { |
| 6738 | lua_pushfstring(L, "offset out of range."); |
| 6739 | WILL_LJMP(lua_error(L)); |
| 6740 | } |
| 6741 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6742 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6743 | len = output + input - offset; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6744 | if (lua_gettop(L) == 3) { |
| 6745 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6746 | if (!len) |
| 6747 | goto end; |
| 6748 | if (len == -1) |
| 6749 | len = output + input - offset; |
| 6750 | if (len < 0 || offset + len > output + input) { |
| 6751 | lua_pushfstring(L, "length out of range."); |
| 6752 | WILL_LJMP(lua_error(L)); |
| 6753 | } |
| 6754 | } |
| 6755 | |
| 6756 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6757 | |
| 6758 | end: |
| 6759 | lua_pushinteger(L, len); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6760 | return 1; |
| 6761 | } |
| 6762 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6763 | /* 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] | 6764 | * all remaining data are removed, accordingly to the filter context. It returns |
| 6765 | * the amount of data written or -1 if nothing was copied. Unlike the channel |
| 6766 | * function used to replace data, this one can only be called inside a filter, |
| 6767 | * from http_payload callback. So it cannot yield. An exception is returned if |
| 6768 | * it is called from another callback. |
| 6769 | */ |
| 6770 | __LJMP static int hlua_http_msg_set_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6771 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6772 | struct http_msg *msg; |
| 6773 | struct filter *filter; |
| 6774 | struct htx *htx; |
| 6775 | const char *str; |
| 6776 | size_t input, output, sz; |
| 6777 | int offset, len; |
| 6778 | int ret; |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 6779 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6780 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 6781 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 6782 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6783 | |
| 6784 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6785 | WILL_LJMP(lua_error(L)); |
| 6786 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6787 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6788 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6789 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6790 | WILL_LJMP(lua_error(L)); |
| 6791 | |
| 6792 | offset = output; |
| 6793 | if (lua_gettop(L) > 2) { |
| 6794 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6795 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6796 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6797 | offset += output; |
| 6798 | if (offset < output || offset > input + output) { |
| 6799 | lua_pushfstring(L, "offset out of range."); |
| 6800 | WILL_LJMP(lua_error(L)); |
| 6801 | } |
| 6802 | } |
| 6803 | |
| 6804 | len = output + input - offset; |
| 6805 | if (lua_gettop(L) == 4) { |
| 6806 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6807 | if (!len) |
| 6808 | goto set; |
| 6809 | if (len == -1) |
| 6810 | len = output + input - offset; |
| 6811 | if (len < 0 || offset + len > output + input) { |
| 6812 | lua_pushfstring(L, "length out of range."); |
| 6813 | WILL_LJMP(lua_error(L)); |
| 6814 | } |
| 6815 | } |
| 6816 | |
| 6817 | set: |
| 6818 | /* Be sure we can copied the string once input data will be removed. */ |
| 6819 | htx = htx_from_buf(&msg->chn->buf); |
| 6820 | if (sz > htx_free_data_space(htx) + len) |
| 6821 | lua_pushinteger(L, -1); |
| 6822 | else { |
| 6823 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6824 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6825 | lua_pushinteger(L, ret); |
| 6826 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6827 | return 1; |
| 6828 | } |
| 6829 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6830 | /* Prepends data into an HTTP message and forward it, from the filter point of |
| 6831 | * view. It returns the amount of data written or -1 if nothing was sent. Unlike |
| 6832 | * the channel function used to send data, this one can only be called inside a |
| 6833 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6834 | * returned if it is called from another callback. |
| 6835 | */ |
| 6836 | __LJMP static int hlua_http_msg_send(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6837 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6838 | struct http_msg *msg; |
| 6839 | struct filter *filter; |
| 6840 | struct htx *htx; |
| 6841 | const char *str; |
| 6842 | size_t offset, len, sz; |
| 6843 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6844 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6845 | MAY_LJMP(check_args(L, 2, "send")); |
| 6846 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6847 | |
| 6848 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6849 | WILL_LJMP(lua_error(L)); |
| 6850 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6851 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6852 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6853 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6854 | WILL_LJMP(lua_error(L)); |
| 6855 | |
| 6856 | /* Return an error if the channel's output is closed */ |
| 6857 | if (unlikely(channel_output_closed(msg->chn))) { |
| 6858 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6859 | return 1; |
| 6860 | } |
| 6861 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6862 | htx = htx_from_buf(&msg->chn->buf); |
| 6863 | if (sz > htx_free_data_space(htx)) { |
| 6864 | lua_pushinteger(L, -1); |
| 6865 | return 1; |
| 6866 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6867 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6868 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6869 | if (ret > 0) { |
| 6870 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6871 | |
| 6872 | FLT_OFF(filter, msg->chn) += ret; |
| 6873 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6874 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 6875 | } |
| 6876 | |
| 6877 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6878 | return 1; |
| 6879 | } |
| 6880 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6881 | /* Forwards a given amount of bytes. It return -1 if the channel's output is |
| 6882 | * closed. Otherwise, it returns the number of bytes forwarded. Unlike the |
| 6883 | * channel function used to forward data, this one can only be called inside a |
| 6884 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6885 | * returned if it is called from another callback. All other functions deal with |
| 6886 | * DATA block, this one not. |
| 6887 | */ |
| 6888 | __LJMP static int hlua_http_msg_forward(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6889 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6890 | struct http_msg *msg; |
| 6891 | struct filter *filter; |
| 6892 | size_t offset, len; |
| 6893 | int fwd, ret = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6894 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6895 | MAY_LJMP(check_args(L, 2, "forward")); |
| 6896 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6897 | |
| 6898 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6899 | WILL_LJMP(lua_error(L)); |
| 6900 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6901 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6902 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6903 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6904 | WILL_LJMP(lua_error(L)); |
| 6905 | |
| 6906 | /* Nothing to do, just return */ |
| 6907 | if (!fwd) |
| 6908 | goto end; |
| 6909 | |
| 6910 | /* Return an error if the channel's output is closed */ |
| 6911 | if (unlikely(channel_output_closed(msg->chn))) { |
| 6912 | ret = -1; |
| 6913 | goto end; |
| 6914 | } |
| 6915 | |
| 6916 | ret = fwd; |
| 6917 | if (ret > len) |
| 6918 | ret = len; |
| 6919 | |
| 6920 | if (ret) { |
| 6921 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6922 | |
| 6923 | FLT_OFF(filter, msg->chn) += ret; |
| 6924 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 6925 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6926 | } |
| 6927 | |
| 6928 | end: |
| 6929 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6930 | return 1; |
| 6931 | } |
| 6932 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6933 | /* Set EOM flag on the HTX message. |
| 6934 | * |
| 6935 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 6936 | * really know how to do without this feature. |
| 6937 | */ |
| 6938 | __LJMP static int hlua_http_msg_set_eom(lua_State *L) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6939 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6940 | struct http_msg *msg; |
| 6941 | struct htx *htx; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6942 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6943 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 6944 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6945 | htx = htxbuf(&msg->chn->buf); |
| 6946 | htx->flags |= HTX_FL_EOM; |
| 6947 | return 0; |
| 6948 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6949 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6950 | /* Unset EOM flag on the HTX message. |
| 6951 | * |
| 6952 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 6953 | * really know how to do without this feature. |
| 6954 | */ |
| 6955 | __LJMP static int hlua_http_msg_unset_eom(lua_State *L) |
| 6956 | { |
| 6957 | struct http_msg *msg; |
| 6958 | struct htx *htx; |
| 6959 | |
| 6960 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 6961 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6962 | htx = htxbuf(&msg->chn->buf); |
| 6963 | htx->flags &= ~HTX_FL_EOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6964 | return 0; |
| 6965 | } |
| 6966 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6967 | /* |
| 6968 | * |
| 6969 | * |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 6970 | * Class HTTPClient |
| 6971 | * |
| 6972 | * |
| 6973 | */ |
| 6974 | __LJMP static struct hlua_httpclient *hlua_checkhttpclient(lua_State *L, int ud) |
| 6975 | { |
| 6976 | return MAY_LJMP(hlua_checkudata(L, ud, class_httpclient_ref)); |
| 6977 | } |
| 6978 | |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 6979 | |
| 6980 | /* stops the httpclient and ask it to kill itself */ |
| 6981 | __LJMP static int hlua_httpclient_gc(lua_State *L) |
| 6982 | { |
| 6983 | struct hlua_httpclient *hlua_hc; |
| 6984 | |
| 6985 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 6986 | |
| 6987 | hlua_hc = MAY_LJMP(hlua_checkhttpclient(L, 1)); |
| 6988 | |
| 6989 | httpclient_stop_and_destroy(hlua_hc->hc); |
| 6990 | |
| 6991 | hlua_hc->hc = NULL; |
| 6992 | |
| 6993 | |
| 6994 | return 0; |
| 6995 | } |
| 6996 | |
| 6997 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 6998 | __LJMP static int hlua_httpclient_new(lua_State *L) |
| 6999 | { |
| 7000 | struct hlua_httpclient *hlua_hc; |
| 7001 | struct hlua *hlua; |
| 7002 | |
| 7003 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 7004 | hlua = hlua_gethlua(L); |
| 7005 | if (!hlua) |
| 7006 | return 0; |
| 7007 | |
| 7008 | /* Check stack size. */ |
| 7009 | if (!lua_checkstack(L, 3)) { |
| 7010 | hlua_pusherror(L, "httpclient: full stack"); |
| 7011 | goto err; |
| 7012 | } |
| 7013 | /* Create the object: obj[0] = userdata. */ |
| 7014 | lua_newtable(L); |
| 7015 | hlua_hc = MAY_LJMP(lua_newuserdata(L, sizeof(*hlua_hc))); |
| 7016 | lua_rawseti(L, -2, 0); |
| 7017 | memset(hlua_hc, 0, sizeof(*hlua_hc)); |
| 7018 | |
| 7019 | hlua_hc->hc = httpclient_new(hlua, 0, IST_NULL); |
| 7020 | if (!hlua_hc->hc) |
| 7021 | goto err; |
| 7022 | |
| 7023 | /* Pop a class stream metatable and affect it to the userdata. */ |
| 7024 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_httpclient_ref); |
| 7025 | lua_setmetatable(L, -2); |
| 7026 | |
| 7027 | return 1; |
| 7028 | |
| 7029 | err: |
| 7030 | WILL_LJMP(lua_error(L)); |
| 7031 | return 0; |
| 7032 | } |
| 7033 | |
| 7034 | |
| 7035 | /* |
| 7036 | * Callback of the httpclient, this callback wakes the lua task up, once the |
| 7037 | * httpclient receives some data |
| 7038 | * |
| 7039 | */ |
| 7040 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7041 | static void hlua_httpclient_cb(struct httpclient *hc) |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7042 | { |
| 7043 | struct hlua *hlua = hc->caller; |
| 7044 | |
| 7045 | if (!hlua || !hlua->task) |
| 7046 | return; |
| 7047 | |
| 7048 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 7049 | } |
| 7050 | |
| 7051 | /* |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 7052 | * Fill the lua stack with headers from the httpclient response |
| 7053 | * This works the same way as the hlua_http_get_headers() function |
| 7054 | */ |
| 7055 | __LJMP static int hlua_httpclient_get_headers(lua_State *L, struct hlua_httpclient *hlua_hc) |
| 7056 | { |
| 7057 | struct http_hdr *hdr; |
| 7058 | |
| 7059 | lua_newtable(L); |
| 7060 | |
William Lallemand | ef574b2 | 2021-10-05 16:19:31 +0200 | [diff] [blame] | 7061 | for (hdr = hlua_hc->hc->res.hdrs; hdr && isttest(hdr->n); hdr++) { |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 7062 | struct ist n, v; |
| 7063 | int len; |
| 7064 | |
| 7065 | n = hdr->n; |
| 7066 | v = hdr->v; |
| 7067 | |
| 7068 | /* Check for existing entry: |
| 7069 | * assume that the table is on the top of the stack, and |
| 7070 | * push the key in the stack, the function lua_gettable() |
| 7071 | * perform the lookup. |
| 7072 | */ |
| 7073 | |
| 7074 | lua_pushlstring(L, n.ptr, n.len); |
| 7075 | lua_gettable(L, -2); |
| 7076 | |
| 7077 | switch (lua_type(L, -1)) { |
| 7078 | case LUA_TNIL: |
| 7079 | /* Table not found, create it. */ |
| 7080 | lua_pop(L, 1); /* remove the nil value. */ |
| 7081 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 7082 | lua_newtable(L); /* create and push empty table. */ |
| 7083 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7084 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 7085 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 7086 | break; |
| 7087 | |
| 7088 | case LUA_TTABLE: |
| 7089 | /* Entry found: push the value in the table. */ |
| 7090 | len = lua_rawlen(L, -1); |
| 7091 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7092 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 7093 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 7094 | break; |
| 7095 | |
| 7096 | default: |
| 7097 | /* Other cases are errors. */ |
| 7098 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 7099 | WILL_LJMP(lua_error(L)); |
| 7100 | } |
| 7101 | } |
| 7102 | return 1; |
| 7103 | } |
| 7104 | |
| 7105 | /* |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7106 | * Allocate and return an array of http_hdr ist extracted from the <headers> lua table |
| 7107 | * |
| 7108 | * Caller must free the result |
| 7109 | */ |
| 7110 | struct http_hdr *hlua_httpclient_table_to_hdrs(lua_State *L) |
| 7111 | { |
| 7112 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
| 7113 | struct http_hdr *result = NULL; |
| 7114 | uint32_t hdr_num = 0; |
| 7115 | |
| 7116 | lua_pushnil(L); |
| 7117 | while (lua_next(L, -2) != 0) { |
| 7118 | struct ist name, value; |
| 7119 | const char *n, *v; |
| 7120 | size_t nlen, vlen; |
| 7121 | |
| 7122 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 7123 | /* Skip element if the key is not a string or if the value is not a table */ |
| 7124 | goto next_hdr; |
| 7125 | } |
| 7126 | |
| 7127 | n = lua_tolstring(L, -2, &nlen); |
| 7128 | name = ist2(n, nlen); |
| 7129 | |
| 7130 | /* Loop on header's values */ |
| 7131 | lua_pushnil(L); |
| 7132 | while (lua_next(L, -2)) { |
| 7133 | if (!lua_isstring(L, -1)) { |
| 7134 | /* Skip the value if it is not a string */ |
| 7135 | goto next_value; |
| 7136 | } |
| 7137 | |
| 7138 | v = lua_tolstring(L, -1, &vlen); |
| 7139 | value = ist2(v, vlen); |
| 7140 | name = ist2(n, nlen); |
| 7141 | |
| 7142 | hdrs[hdr_num].n = istdup(name); |
| 7143 | hdrs[hdr_num].v = istdup(value); |
| 7144 | |
| 7145 | hdr_num++; |
| 7146 | |
| 7147 | next_value: |
| 7148 | lua_pop(L, 1); |
| 7149 | } |
| 7150 | |
| 7151 | next_hdr: |
| 7152 | lua_pop(L, 1); |
| 7153 | |
| 7154 | } |
| 7155 | |
| 7156 | if (hdr_num) { |
| 7157 | /* alloc and copy the headers in the httpclient struct */ |
Tim Duesterhus | 16cc16d | 2021-11-06 15:14:45 +0100 | [diff] [blame] | 7158 | result = calloc((hdr_num + 1), sizeof(*result)); |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7159 | if (!result) |
| 7160 | goto skip_headers; |
| 7161 | memcpy(result, hdrs, sizeof(struct http_hdr) * (hdr_num + 1)); |
| 7162 | |
| 7163 | result[hdr_num].n = IST_NULL; |
| 7164 | result[hdr_num].v = IST_NULL; |
| 7165 | } |
| 7166 | |
| 7167 | skip_headers: |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7168 | |
| 7169 | return result; |
| 7170 | } |
| 7171 | |
| 7172 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7173 | /* |
| 7174 | * For each yield, checks if there is some data in the httpclient and push them |
| 7175 | * in the lua buffer, once the httpclient finished its job, push the result on |
| 7176 | * the stack |
| 7177 | */ |
| 7178 | __LJMP static int hlua_httpclient_rcv_yield(lua_State *L, int status, lua_KContext ctx) |
| 7179 | { |
| 7180 | struct buffer *tr; |
| 7181 | int res; |
| 7182 | struct hlua *hlua = hlua_gethlua(L); |
| 7183 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7184 | |
| 7185 | |
| 7186 | tr = get_trash_chunk(); |
| 7187 | |
| 7188 | res = httpclient_res_xfer(hlua_hc->hc, tr); |
| 7189 | luaL_addlstring(&hlua_hc->b, b_orig(tr), res); |
| 7190 | |
| 7191 | if (!httpclient_data(hlua_hc->hc) && httpclient_ended(hlua_hc->hc)) { |
| 7192 | |
| 7193 | luaL_pushresult(&hlua_hc->b); |
| 7194 | lua_settable(L, -3); |
| 7195 | |
| 7196 | lua_pushstring(L, "status"); |
| 7197 | lua_pushinteger(L, hlua_hc->hc->res.status); |
| 7198 | lua_settable(L, -3); |
| 7199 | |
| 7200 | |
| 7201 | lua_pushstring(L, "reason"); |
| 7202 | lua_pushlstring(L, hlua_hc->hc->res.reason.ptr, hlua_hc->hc->res.reason.len); |
| 7203 | lua_settable(L, -3); |
| 7204 | |
| 7205 | lua_pushstring(L, "headers"); |
| 7206 | hlua_httpclient_get_headers(L, hlua_hc); |
| 7207 | lua_settable(L, -3); |
| 7208 | |
| 7209 | return 1; |
| 7210 | } |
| 7211 | |
| 7212 | if (httpclient_data(hlua_hc->hc)) |
| 7213 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 7214 | |
| 7215 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7216 | |
| 7217 | return 0; |
| 7218 | } |
| 7219 | |
| 7220 | /* |
| 7221 | * Call this when trying to stream a body during a request |
| 7222 | */ |
| 7223 | __LJMP static int hlua_httpclient_snd_yield(lua_State *L, int status, lua_KContext ctx) |
| 7224 | { |
| 7225 | struct hlua *hlua; |
| 7226 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7227 | const char *body_str = NULL; |
| 7228 | int ret; |
| 7229 | int end = 0; |
| 7230 | size_t buf_len; |
| 7231 | size_t to_send = 0; |
| 7232 | |
| 7233 | hlua = hlua_gethlua(L); |
| 7234 | |
| 7235 | if (!hlua || !hlua->task) |
| 7236 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7237 | "'frontend', 'backend' or 'task'")); |
| 7238 | |
| 7239 | ret = lua_getfield(L, -1, "body"); |
| 7240 | if (ret != LUA_TSTRING) |
| 7241 | goto rcv; |
| 7242 | |
| 7243 | body_str = lua_tolstring(L, -1, &buf_len); |
| 7244 | lua_pop(L, 1); |
| 7245 | |
Christopher Faulet | fc59129 | 2022-01-12 14:46:03 +0100 | [diff] [blame] | 7246 | to_send = buf_len - hlua_hc->sent; |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7247 | |
| 7248 | if ((hlua_hc->sent + to_send) >= buf_len) |
| 7249 | end = 1; |
| 7250 | |
| 7251 | /* the end flag is always set since we are using the whole remaining size */ |
| 7252 | hlua_hc->sent += httpclient_req_xfer(hlua_hc->hc, ist2(body_str + hlua_hc->sent, to_send), end); |
| 7253 | |
| 7254 | if (buf_len > hlua_hc->sent) { |
| 7255 | /* still need to process the buffer */ |
| 7256 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_snd_yield, TICK_ETERNITY, 0)); |
| 7257 | } else { |
| 7258 | goto rcv; |
| 7259 | /* we sent the whole request buffer we can recv */ |
| 7260 | } |
| 7261 | return 0; |
| 7262 | |
| 7263 | rcv: |
| 7264 | |
| 7265 | /* we return a "res" object */ |
| 7266 | lua_newtable(L); |
| 7267 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7268 | lua_pushstring(L, "body"); |
William Lallemand | d1187eb | 2021-11-02 10:40:06 +0100 | [diff] [blame] | 7269 | luaL_buffinit(L, &hlua_hc->b); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7270 | |
William Lallemand | 933fe39 | 2021-11-04 09:45:58 +0100 | [diff] [blame] | 7271 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7272 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7273 | |
| 7274 | return 1; |
| 7275 | } |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7276 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7277 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7278 | * Send an HTTP request and wait for a response |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7279 | */ |
| 7280 | |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7281 | __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] | 7282 | { |
| 7283 | struct hlua_httpclient *hlua_hc; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7284 | struct http_hdr *hdrs = NULL; |
| 7285 | struct http_hdr *hdrs_i = NULL; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7286 | struct hlua *hlua; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7287 | const char *url_str = NULL; |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7288 | const char *body_str = NULL; |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7289 | size_t buf_len; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7290 | int ret; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7291 | |
| 7292 | hlua = hlua_gethlua(L); |
| 7293 | |
| 7294 | if (!hlua || !hlua->task) |
| 7295 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7296 | "'frontend', 'backend' or 'task'")); |
| 7297 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7298 | if (lua_gettop(L) != 2 || lua_type(L, -1) != LUA_TTABLE) |
| 7299 | WILL_LJMP(luaL_error(L, "'get' needs a table as argument")); |
| 7300 | |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 7301 | hlua_hc = hlua_checkhttpclient(L, 1); |
| 7302 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7303 | lua_pushnil(L); /* first key */ |
| 7304 | while (lua_next(L, 2)) { |
| 7305 | if (strcmp(lua_tostring(L, -2), "dst") == 0) { |
| 7306 | if (httpclient_set_dst(hlua_hc->hc, lua_tostring(L, -1)) < 0) |
| 7307 | WILL_LJMP(luaL_error(L, "Can't use the 'dst' argument")); |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 7308 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7309 | } else if (strcmp(lua_tostring(L, -2), "url") == 0) { |
| 7310 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7311 | WILL_LJMP(luaL_error(L, "invalid parameter in 'url', must be a string")); |
| 7312 | url_str = lua_tostring(L, -1); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7313 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7314 | } else if (strcmp(lua_tostring(L, -2), "timeout") == 0) { |
| 7315 | if (lua_type(L, -1) != LUA_TNUMBER) |
| 7316 | WILL_LJMP(luaL_error(L, "invalid parameter in 'timeout', must be a number")); |
| 7317 | httpclient_set_timeout(hlua_hc->hc, lua_tointeger(L, -1)); |
William Lallemand | b4a4ef6 | 2022-02-23 14:18:16 +0100 | [diff] [blame] | 7318 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7319 | } else if (strcmp(lua_tostring(L, -2), "headers") == 0) { |
| 7320 | if (lua_type(L, -1) != LUA_TTABLE) |
| 7321 | WILL_LJMP(luaL_error(L, "invalid parameter in 'headers', must be a table")); |
| 7322 | hdrs = hlua_httpclient_table_to_hdrs(L); |
William Lallemand | 79416cb | 2021-09-24 14:51:44 +0200 | [diff] [blame] | 7323 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7324 | } else if (strcmp(lua_tostring(L, -2), "body") == 0) { |
| 7325 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7326 | WILL_LJMP(luaL_error(L, "invalid parameter in 'body', must be a string")); |
| 7327 | body_str = lua_tolstring(L, -1, &buf_len); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7328 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7329 | } else { |
| 7330 | WILL_LJMP(luaL_error(L, "'%s' invalid parameter name", lua_tostring(L, -2))); |
| 7331 | } |
| 7332 | /* removes 'value'; keeps 'key' for next iteration */ |
| 7333 | lua_pop(L, 1); |
| 7334 | } |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7335 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7336 | if (!url_str) { |
| 7337 | WILL_LJMP(luaL_error(L, "'get' need a 'url' argument")); |
| 7338 | return 0; |
| 7339 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7340 | |
William Lallemand | 10a3736 | 2022-03-02 16:18:26 +0100 | [diff] [blame] | 7341 | hlua_hc->sent = 0; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7342 | |
| 7343 | hlua_hc->hc->req.url = istdup(ist(url_str)); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7344 | hlua_hc->hc->req.meth = meth; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7345 | |
| 7346 | /* update the httpclient callbacks */ |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7347 | hlua_hc->hc->ops.res_stline = hlua_httpclient_cb; |
| 7348 | hlua_hc->hc->ops.res_headers = hlua_httpclient_cb; |
| 7349 | hlua_hc->hc->ops.res_payload = hlua_httpclient_cb; |
William Lallemand | 8f170c7 | 2022-03-15 10:52:07 +0100 | [diff] [blame] | 7350 | hlua_hc->hc->ops.res_end = hlua_httpclient_cb; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7351 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7352 | /* a body is available, it will use the request callback */ |
| 7353 | if (body_str) { |
| 7354 | hlua_hc->hc->ops.req_payload = hlua_httpclient_cb; |
| 7355 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7356 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7357 | 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] | 7358 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7359 | /* free the temporary headers array */ |
| 7360 | hdrs_i = hdrs; |
| 7361 | while (hdrs_i && isttest(hdrs_i->n)) { |
| 7362 | istfree(&hdrs_i->n); |
| 7363 | istfree(&hdrs_i->v); |
| 7364 | hdrs_i++; |
| 7365 | } |
| 7366 | ha_free(&hdrs); |
| 7367 | |
| 7368 | |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7369 | if (ret != ERR_NONE) { |
| 7370 | WILL_LJMP(luaL_error(L, "Can't generate the HTTP request")); |
| 7371 | return 0; |
| 7372 | } |
| 7373 | |
William Lallemand | c2d3db4 | 2022-04-26 11:46:13 +0200 | [diff] [blame] | 7374 | if (!httpclient_start(hlua_hc->hc)) |
| 7375 | WILL_LJMP(luaL_error(L, "couldn't start the httpclient")); |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7376 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7377 | 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] | 7378 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7379 | return 0; |
| 7380 | } |
| 7381 | |
| 7382 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7383 | * Sends an HTTP HEAD request and wait for a response |
| 7384 | * |
| 7385 | * httpclient:head(url, headers, payload) |
| 7386 | */ |
| 7387 | __LJMP static int hlua_httpclient_head(lua_State *L) |
| 7388 | { |
| 7389 | return hlua_httpclient_send(L, HTTP_METH_HEAD); |
| 7390 | } |
| 7391 | |
| 7392 | /* |
| 7393 | * Send an HTTP GET request and wait for a response |
| 7394 | * |
| 7395 | * httpclient:get(url, headers, payload) |
| 7396 | */ |
| 7397 | __LJMP static int hlua_httpclient_get(lua_State *L) |
| 7398 | { |
| 7399 | return hlua_httpclient_send(L, HTTP_METH_GET); |
| 7400 | |
| 7401 | } |
| 7402 | |
| 7403 | /* |
| 7404 | * Sends an HTTP PUT request and wait for a response |
| 7405 | * |
| 7406 | * httpclient:put(url, headers, payload) |
| 7407 | */ |
| 7408 | __LJMP static int hlua_httpclient_put(lua_State *L) |
| 7409 | { |
| 7410 | return hlua_httpclient_send(L, HTTP_METH_PUT); |
| 7411 | } |
| 7412 | |
| 7413 | /* |
| 7414 | * Send an HTTP POST request and wait for a response |
| 7415 | * |
| 7416 | * httpclient:post(url, headers, payload) |
| 7417 | */ |
| 7418 | __LJMP static int hlua_httpclient_post(lua_State *L) |
| 7419 | { |
| 7420 | return hlua_httpclient_send(L, HTTP_METH_POST); |
| 7421 | } |
| 7422 | |
| 7423 | |
| 7424 | /* |
| 7425 | * Sends an HTTP DELETE request and wait for a response |
| 7426 | * |
| 7427 | * httpclient:delete(url, headers, payload) |
| 7428 | */ |
| 7429 | __LJMP static int hlua_httpclient_delete(lua_State *L) |
| 7430 | { |
| 7431 | return hlua_httpclient_send(L, HTTP_METH_DELETE); |
| 7432 | } |
| 7433 | |
| 7434 | /* |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7435 | * |
| 7436 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7437 | * Class TXN |
| 7438 | * |
| 7439 | * |
| 7440 | */ |
| 7441 | |
| 7442 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7443 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7444 | */ |
| 7445 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 7446 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7447 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7448 | } |
| 7449 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7450 | __LJMP static int hlua_set_var(lua_State *L) |
| 7451 | { |
| 7452 | struct hlua_txn *htxn; |
| 7453 | const char *name; |
| 7454 | size_t len; |
| 7455 | struct sample smp; |
| 7456 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 7457 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7458 | 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] | 7459 | |
| 7460 | /* It is useles to retrieve the stream, but this function |
| 7461 | * runs only in a stream context. |
| 7462 | */ |
| 7463 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7464 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7465 | |
| 7466 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 7467 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7468 | hlua_lua2smp(L, 3, &smp); |
| 7469 | |
| 7470 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7471 | 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] | 7472 | |
| 7473 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 7474 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 7475 | else |
| 7476 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 7477 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 7478 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7479 | } |
| 7480 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7481 | __LJMP static int hlua_unset_var(lua_State *L) |
| 7482 | { |
| 7483 | struct hlua_txn *htxn; |
| 7484 | const char *name; |
| 7485 | size_t len; |
| 7486 | struct sample smp; |
| 7487 | |
| 7488 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 7489 | |
| 7490 | /* It is useles to retrieve the stream, but this function |
| 7491 | * runs only in a stream context. |
| 7492 | */ |
| 7493 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7494 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7495 | |
| 7496 | /* Unset the variable. */ |
| 7497 | 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] | 7498 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 7499 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7500 | } |
| 7501 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7502 | __LJMP static int hlua_get_var(lua_State *L) |
| 7503 | { |
| 7504 | struct hlua_txn *htxn; |
| 7505 | const char *name; |
| 7506 | size_t len; |
| 7507 | struct sample smp; |
| 7508 | |
| 7509 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 7510 | |
| 7511 | /* It is useles to retrieve the stream, but this function |
| 7512 | * runs only in a stream context. |
| 7513 | */ |
| 7514 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7515 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7516 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7517 | 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] | 7518 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7519 | lua_pushnil(L); |
| 7520 | return 1; |
| 7521 | } |
| 7522 | |
| 7523 | return hlua_smp2lua(L, &smp); |
| 7524 | } |
| 7525 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7526 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7527 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7528 | struct hlua *hlua; |
| 7529 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7530 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 7531 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7532 | /* It is useles to retrieve the stream, but this function |
| 7533 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7534 | */ |
| 7535 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7536 | |
| 7537 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7538 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7539 | if (!hlua) |
| 7540 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7541 | |
| 7542 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 7543 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7544 | |
| 7545 | /* Get and store new value. */ |
| 7546 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 7547 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 7548 | |
| 7549 | return 0; |
| 7550 | } |
| 7551 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7552 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7553 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7554 | struct hlua *hlua; |
| 7555 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7556 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 7557 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7558 | /* It is useles to retrieve the stream, but this function |
| 7559 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7560 | */ |
| 7561 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7562 | |
| 7563 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7564 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7565 | if (!hlua) { |
| 7566 | lua_pushnil(L); |
| 7567 | return 1; |
| 7568 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7569 | |
| 7570 | /* Push configuration index in the stack. */ |
| 7571 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 7572 | |
| 7573 | return 1; |
| 7574 | } |
| 7575 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7576 | /* Create stack entry containing a class TXN. This function |
| 7577 | * return 0 if the stack does not contains free slots, |
| 7578 | * otherwise it returns 1. |
| 7579 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7580 | 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] | 7581 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7582 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7583 | |
| 7584 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7585 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7586 | return 0; |
| 7587 | |
| 7588 | /* NOTE: The allocation never fails. The failure |
| 7589 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7590 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7591 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7592 | /* Create the object: obj[0] = userdata. */ |
| 7593 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7594 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7595 | lua_rawseti(L, -2, 0); |
| 7596 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7597 | htxn->s = s; |
| 7598 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 7599 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7600 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7601 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7602 | /* Create the "f" field that contains a list of fetches. */ |
| 7603 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7604 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7605 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7606 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7607 | |
| 7608 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 7609 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7610 | 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] | 7611 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7612 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7613 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7614 | /* Create the "c" field that contains a list of converters. */ |
| 7615 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7616 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7617 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7618 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7619 | |
| 7620 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 7621 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 7622 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7623 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7624 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7625 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7626 | /* Create the "req" field that contains the request channel object. */ |
| 7627 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7628 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7629 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7630 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7631 | |
| 7632 | /* Create the "res" field that contains the response channel object. */ |
| 7633 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7634 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7635 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7636 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7637 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7638 | /* Creates the HTTP object is the current proxy allows http. */ |
| 7639 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 7640 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7641 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7642 | return 0; |
| 7643 | } |
| 7644 | else |
| 7645 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7646 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7647 | |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 7648 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) { |
| 7649 | /* HTTPMessage object are created when a lua TXN is created from |
| 7650 | * a filter context only |
| 7651 | */ |
| 7652 | |
| 7653 | /* Creates the HTTP-Request object is the current proxy allows http. */ |
| 7654 | lua_pushstring(L, "http_req"); |
| 7655 | if (p->mode == PR_MODE_HTTP) { |
| 7656 | if (!hlua_http_msg_new(L, &s->txn->req)) |
| 7657 | return 0; |
| 7658 | } |
| 7659 | else |
| 7660 | lua_pushnil(L); |
| 7661 | lua_rawset(L, -3); |
| 7662 | |
| 7663 | /* Creates the HTTP-Response object is the current proxy allows http. */ |
| 7664 | lua_pushstring(L, "http_res"); |
| 7665 | if (p->mode == PR_MODE_HTTP) { |
| 7666 | if (!hlua_http_msg_new(L, &s->txn->rsp)) |
| 7667 | return 0; |
| 7668 | } |
| 7669 | else |
| 7670 | lua_pushnil(L); |
| 7671 | lua_rawset(L, -3); |
| 7672 | } |
| 7673 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7674 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 7675 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 7676 | lua_setmetatable(L, -2); |
| 7677 | |
| 7678 | return 1; |
| 7679 | } |
| 7680 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7681 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 7682 | { |
| 7683 | const char *msg; |
| 7684 | struct hlua_txn *htxn; |
| 7685 | |
| 7686 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 7687 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7688 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7689 | |
| 7690 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 7691 | return 0; |
| 7692 | } |
| 7693 | |
| 7694 | __LJMP static int hlua_txn_log(lua_State *L) |
| 7695 | { |
| 7696 | int level; |
| 7697 | const char *msg; |
| 7698 | struct hlua_txn *htxn; |
| 7699 | |
| 7700 | MAY_LJMP(check_args(L, 3, "log")); |
| 7701 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7702 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7703 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 7704 | |
| 7705 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 7706 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 7707 | |
| 7708 | hlua_sendlog(htxn->s->be, level, msg); |
| 7709 | return 0; |
| 7710 | } |
| 7711 | |
| 7712 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 7713 | { |
| 7714 | const char *msg; |
| 7715 | struct hlua_txn *htxn; |
| 7716 | |
| 7717 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 7718 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7719 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7720 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 7721 | return 0; |
| 7722 | } |
| 7723 | |
| 7724 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 7725 | { |
| 7726 | const char *msg; |
| 7727 | struct hlua_txn *htxn; |
| 7728 | |
| 7729 | MAY_LJMP(check_args(L, 2, "Info")); |
| 7730 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7731 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7732 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 7733 | return 0; |
| 7734 | } |
| 7735 | |
| 7736 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 7737 | { |
| 7738 | const char *msg; |
| 7739 | struct hlua_txn *htxn; |
| 7740 | |
| 7741 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 7742 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7743 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7744 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 7745 | return 0; |
| 7746 | } |
| 7747 | |
| 7748 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 7749 | { |
| 7750 | const char *msg; |
| 7751 | struct hlua_txn *htxn; |
| 7752 | |
| 7753 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 7754 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7755 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7756 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 7757 | return 0; |
| 7758 | } |
| 7759 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7760 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 7761 | { |
| 7762 | struct hlua_txn *htxn; |
| 7763 | int ll; |
| 7764 | |
| 7765 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 7766 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7767 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7768 | |
| 7769 | if (ll < 0 || ll > 7) |
| 7770 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 7771 | |
| 7772 | htxn->s->logs.level = ll; |
| 7773 | return 0; |
| 7774 | } |
| 7775 | |
| 7776 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 7777 | { |
| 7778 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7779 | int tos; |
| 7780 | |
| 7781 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 7782 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7783 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7784 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 7785 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7786 | return 0; |
| 7787 | } |
| 7788 | |
| 7789 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 7790 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7791 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7792 | int mark; |
| 7793 | |
| 7794 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 7795 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7796 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7797 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 7798 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7799 | return 0; |
| 7800 | } |
| 7801 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 7802 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 7803 | { |
| 7804 | struct hlua_txn *htxn; |
| 7805 | |
| 7806 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 7807 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7808 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 7809 | return 0; |
| 7810 | } |
| 7811 | |
| 7812 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 7813 | { |
| 7814 | struct hlua_txn *htxn; |
| 7815 | |
| 7816 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 7817 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7818 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 7819 | return 0; |
| 7820 | } |
| 7821 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7822 | /* 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] | 7823 | * 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] | 7824 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 7825 | * error. The Reply must be on top of the stack. |
| 7826 | */ |
| 7827 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 7828 | { |
| 7829 | struct htx *htx; |
| 7830 | struct htx_sl *sl; |
| 7831 | struct h1m h1m; |
| 7832 | const char *status, *reason, *body; |
| 7833 | size_t status_len, reason_len, body_len; |
| 7834 | int ret, code, flags; |
| 7835 | |
| 7836 | code = 200; |
| 7837 | status = "200"; |
| 7838 | status_len = 3; |
| 7839 | ret = lua_getfield(L, -1, "status"); |
| 7840 | if (ret == LUA_TNUMBER) { |
| 7841 | code = lua_tointeger(L, -1); |
| 7842 | status = lua_tolstring(L, -1, &status_len); |
| 7843 | } |
| 7844 | lua_pop(L, 1); |
| 7845 | |
| 7846 | reason = http_get_reason(code); |
| 7847 | reason_len = strlen(reason); |
| 7848 | ret = lua_getfield(L, -1, "reason"); |
| 7849 | if (ret == LUA_TSTRING) |
| 7850 | reason = lua_tolstring(L, -1, &reason_len); |
| 7851 | lua_pop(L, 1); |
| 7852 | |
| 7853 | body = NULL; |
| 7854 | body_len = 0; |
| 7855 | ret = lua_getfield(L, -1, "body"); |
| 7856 | if (ret == LUA_TSTRING) |
| 7857 | body = lua_tolstring(L, -1, &body_len); |
| 7858 | lua_pop(L, 1); |
| 7859 | |
| 7860 | /* Prepare the response before inserting the headers */ |
| 7861 | h1m_init_res(&h1m); |
| 7862 | htx = htx_from_buf(&s->res.buf); |
| 7863 | channel_htx_truncate(&s->res, htx); |
| 7864 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 7865 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 7866 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 7867 | ist2(status, status_len), ist2(reason, reason_len)); |
| 7868 | } |
| 7869 | else { |
| 7870 | flags = HTX_SL_F_IS_RESP; |
| 7871 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 7872 | ist2(status, status_len), ist2(reason, reason_len)); |
| 7873 | } |
| 7874 | if (!sl) |
| 7875 | goto fail; |
| 7876 | sl->info.res.status = code; |
| 7877 | |
| 7878 | /* Push in the stack the "headers" entry. */ |
| 7879 | ret = lua_getfield(L, -1, "headers"); |
| 7880 | if (ret != LUA_TTABLE) |
| 7881 | goto skip_headers; |
| 7882 | |
| 7883 | lua_pushnil(L); |
| 7884 | while (lua_next(L, -2) != 0) { |
| 7885 | struct ist name, value; |
| 7886 | const char *n, *v; |
| 7887 | size_t nlen, vlen; |
| 7888 | |
| 7889 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 7890 | /* Skip element if the key is not a string or if the value is not a table */ |
| 7891 | goto next_hdr; |
| 7892 | } |
| 7893 | |
| 7894 | n = lua_tolstring(L, -2, &nlen); |
| 7895 | name = ist2(n, nlen); |
| 7896 | if (isteqi(name, ist("content-length"))) { |
| 7897 | /* Always skip content-length header. It will be added |
| 7898 | * later with the correct len |
| 7899 | */ |
| 7900 | goto next_hdr; |
| 7901 | } |
| 7902 | |
| 7903 | /* Loop on header's values */ |
| 7904 | lua_pushnil(L); |
| 7905 | while (lua_next(L, -2)) { |
| 7906 | if (!lua_isstring(L, -1)) { |
| 7907 | /* Skip the value if it is not a string */ |
| 7908 | goto next_value; |
| 7909 | } |
| 7910 | |
| 7911 | v = lua_tolstring(L, -1, &vlen); |
| 7912 | value = ist2(v, vlen); |
| 7913 | |
| 7914 | if (isteqi(name, ist("transfer-encoding"))) |
| 7915 | h1_parse_xfer_enc_header(&h1m, value); |
| 7916 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 7917 | goto fail; |
| 7918 | |
| 7919 | next_value: |
| 7920 | lua_pop(L, 1); |
| 7921 | } |
| 7922 | |
| 7923 | next_hdr: |
| 7924 | lua_pop(L, 1); |
| 7925 | } |
| 7926 | skip_headers: |
| 7927 | lua_pop(L, 1); |
| 7928 | |
| 7929 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 7930 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 7931 | const char *clen = ultoa(body_len); |
| 7932 | |
| 7933 | h1m.flags |= H1_MF_CLEN; |
| 7934 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 7935 | goto fail; |
| 7936 | } |
| 7937 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 7938 | h1m.flags |= H1_MF_XFER_LEN; |
| 7939 | |
| 7940 | /* Update HTX start-line flags */ |
| 7941 | if (h1m.flags & H1_MF_XFER_ENC) |
| 7942 | flags |= HTX_SL_F_XFER_ENC; |
| 7943 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 7944 | flags |= HTX_SL_F_XFER_LEN; |
| 7945 | if (h1m.flags & H1_MF_CHNK) |
| 7946 | flags |= HTX_SL_F_CHNK; |
| 7947 | else if (h1m.flags & H1_MF_CLEN) |
| 7948 | flags |= HTX_SL_F_CLEN; |
| 7949 | if (h1m.body_len == 0) |
| 7950 | flags |= HTX_SL_F_BODYLESS; |
| 7951 | } |
| 7952 | sl->flags |= flags; |
| 7953 | |
| 7954 | |
| 7955 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7956 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7957 | goto fail; |
| 7958 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7959 | htx->flags |= HTX_FL_EOM; |
| 7960 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7961 | /* Now, forward the response and terminate the transaction */ |
| 7962 | s->txn->status = code; |
| 7963 | htx_to_buf(htx, &s->res.buf); |
| 7964 | if (!http_forward_proxy_resp(s, 1)) |
| 7965 | goto fail; |
| 7966 | |
| 7967 | return 1; |
| 7968 | |
| 7969 | fail: |
| 7970 | channel_htx_truncate(&s->res, htx); |
| 7971 | return 0; |
| 7972 | } |
| 7973 | |
| 7974 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 7975 | * processing is just aborted. Nothing is returned to the client and all |
| 7976 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 7977 | * is forwarded to the client before terminating the transaction. On success, |
| 7978 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 7979 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 7980 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7981 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7982 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7983 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 7984 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7985 | struct stream *s; |
| 7986 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7987 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 7988 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7989 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7990 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 7991 | * just end the execution of the current lua code. */ |
| 7992 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7993 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7994 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7995 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 7996 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7997 | struct channel *req = &s->req; |
| 7998 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 7999 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8000 | channel_auto_read(req); |
| 8001 | channel_abort(req); |
| 8002 | channel_auto_close(req); |
| 8003 | channel_erase(req); |
| 8004 | |
| 8005 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 8006 | channel_auto_read(res); |
| 8007 | channel_auto_close(res); |
| 8008 | channel_shutr_now(res); |
| 8009 | |
| 8010 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 8011 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8012 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 8013 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8014 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 8015 | /* No reply or invalid reply */ |
| 8016 | s->txn->status = 0; |
| 8017 | http_reply_and_close(s, 0, NULL); |
| 8018 | } |
| 8019 | else { |
| 8020 | /* Remove extra args to have the reply on top of the stack */ |
| 8021 | if (lua_gettop(L) > 2) |
| 8022 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8023 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8024 | if (!hlua_txn_forward_reply(L, s)) { |
| 8025 | if (!(s->flags & SF_ERR_MASK)) |
| 8026 | s->flags |= SF_ERR_PRXCOND; |
| 8027 | lua_pushinteger(L, ACT_RET_ERR); |
| 8028 | WILL_LJMP(hlua_done(L)); |
| 8029 | return 0; /* Never reached */ |
| 8030 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 8031 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8032 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8033 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 8034 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 8035 | /* let's log the request time */ |
| 8036 | s->logs.tv_request = now; |
| 8037 | 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] | 8038 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8039 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8040 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8041 | done: |
| 8042 | if (!(s->flags & SF_ERR_MASK)) |
| 8043 | s->flags |= SF_ERR_LOCAL; |
| 8044 | if (!(s->flags & SF_FINST_MASK)) |
| 8045 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8046 | |
Christopher Faulet | e48d1dc | 2021-08-13 14:11:17 +0200 | [diff] [blame] | 8047 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) |
| 8048 | lua_pushinteger(L, -1); |
| 8049 | else |
| 8050 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8051 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8052 | return 0; |
| 8053 | } |
| 8054 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8055 | /* |
| 8056 | * |
| 8057 | * |
| 8058 | * Class REPLY |
| 8059 | * |
| 8060 | * |
| 8061 | */ |
| 8062 | |
| 8063 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 8064 | * free slots, the function fails and returns 0; |
| 8065 | */ |
| 8066 | static int hlua_txn_reply_new(lua_State *L) |
| 8067 | { |
| 8068 | struct hlua_txn *htxn; |
| 8069 | const char *reason, *body = NULL; |
| 8070 | int ret, status; |
| 8071 | |
| 8072 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8073 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8074 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 8075 | WILL_LJMP(lua_error(L)); |
| 8076 | } |
| 8077 | |
| 8078 | /* Default value */ |
| 8079 | status = 200; |
| 8080 | reason = http_get_reason(status); |
| 8081 | |
| 8082 | if (lua_istable(L, 2)) { |
| 8083 | /* load status and reason from the table argument at index 2 */ |
| 8084 | ret = lua_getfield(L, 2, "status"); |
| 8085 | if (ret == LUA_TNIL) |
| 8086 | goto reason; |
| 8087 | else if (ret != LUA_TNUMBER) { |
| 8088 | /* invalid status: ignore the reason */ |
| 8089 | goto body; |
| 8090 | } |
| 8091 | status = lua_tointeger(L, -1); |
| 8092 | |
| 8093 | reason: |
| 8094 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 8095 | ret = lua_getfield(L, 2, "reason"); |
| 8096 | if (ret == LUA_TSTRING) |
| 8097 | reason = lua_tostring(L, -1); |
| 8098 | |
| 8099 | body: |
| 8100 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 8101 | ret = lua_getfield(L, 2, "body"); |
| 8102 | if (ret == LUA_TSTRING) |
| 8103 | body = lua_tostring(L, -1); |
| 8104 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 8105 | } |
| 8106 | |
| 8107 | /* Create the Reply table */ |
| 8108 | lua_newtable(L); |
| 8109 | |
| 8110 | /* Add status element */ |
| 8111 | lua_pushstring(L, "status"); |
| 8112 | lua_pushinteger(L, status); |
| 8113 | lua_settable(L, -3); |
| 8114 | |
| 8115 | /* Add reason element */ |
| 8116 | reason = http_get_reason(status); |
| 8117 | lua_pushstring(L, "reason"); |
| 8118 | lua_pushstring(L, reason); |
| 8119 | lua_settable(L, -3); |
| 8120 | |
| 8121 | /* Add body element, nil if undefined */ |
| 8122 | lua_pushstring(L, "body"); |
| 8123 | if (body) |
| 8124 | lua_pushstring(L, body); |
| 8125 | else |
| 8126 | lua_pushnil(L); |
| 8127 | lua_settable(L, -3); |
| 8128 | |
| 8129 | /* Add headers element */ |
| 8130 | lua_pushstring(L, "headers"); |
| 8131 | lua_newtable(L); |
| 8132 | |
| 8133 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8134 | if (lua_istable(L, 2)) { |
| 8135 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 8136 | ret = lua_getfield(L, 2, "headers"); |
| 8137 | if (ret == LUA_TTABLE) { |
| 8138 | /* stack: [ ... <headers:table>, <table> ] */ |
| 8139 | lua_pushnil(L); |
| 8140 | while (lua_next(L, -2) != 0) { |
| 8141 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 8142 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 8143 | /* invalid value type, skip it */ |
| 8144 | lua_pop(L, 1); |
| 8145 | continue; |
| 8146 | } |
| 8147 | |
| 8148 | |
| 8149 | /* Duplicate the key and swap it with the value. */ |
| 8150 | lua_pushvalue(L, -2); |
| 8151 | lua_insert(L, -2); |
| 8152 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 8153 | |
| 8154 | lua_newtable(L); |
| 8155 | lua_insert(L, -2); |
| 8156 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 8157 | |
| 8158 | if (lua_isstring(L, -1)) { |
| 8159 | /* push the value in the inner table */ |
| 8160 | lua_rawseti(L, -2, 1); |
| 8161 | } |
| 8162 | else { /* table */ |
| 8163 | lua_pushnil(L); |
| 8164 | while (lua_next(L, -2) != 0) { |
| 8165 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 8166 | if (!lua_isstring(L, -1)) { |
| 8167 | /* invalid value type, skip it*/ |
| 8168 | lua_pop(L, 1); |
| 8169 | continue; |
| 8170 | } |
| 8171 | /* push the value in the inner table */ |
| 8172 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 8173 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 8174 | } |
| 8175 | lua_pop(L, 1); |
| 8176 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 8177 | } |
| 8178 | |
| 8179 | /* push (k,v) on the stack in the headers table: |
| 8180 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 8181 | */ |
| 8182 | lua_settable(L, -5); |
| 8183 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 8184 | } |
| 8185 | } |
| 8186 | lua_pop(L, 1); |
| 8187 | } |
| 8188 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8189 | lua_settable(L, -3); |
| 8190 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 8191 | |
| 8192 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 8193 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 8194 | lua_setmetatable(L, -2); |
| 8195 | return 1; |
| 8196 | } |
| 8197 | |
| 8198 | /* Set the reply status code, and optionally the reason. If no reason is |
| 8199 | * provided, the default one corresponding to the status code is used. |
| 8200 | */ |
| 8201 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 8202 | { |
| 8203 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 8204 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 8205 | |
| 8206 | /* First argument (self) must be a table */ |
| 8207 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8208 | |
| 8209 | if (status < 100 || status > 599) { |
| 8210 | lua_pushboolean(L, 0); |
| 8211 | return 1; |
| 8212 | } |
| 8213 | if (!reason) |
| 8214 | reason = http_get_reason(status); |
| 8215 | |
| 8216 | lua_pushinteger(L, status); |
| 8217 | lua_setfield(L, 1, "status"); |
| 8218 | |
| 8219 | lua_pushstring(L, reason); |
| 8220 | lua_setfield(L, 1, "reason"); |
| 8221 | |
| 8222 | lua_pushboolean(L, 1); |
| 8223 | return 1; |
| 8224 | } |
| 8225 | |
| 8226 | /* Add a header into the reply object. Each header name is associated to an |
| 8227 | * array of values in the "headers" table. If the header name is not found, a |
| 8228 | * new entry is created. |
| 8229 | */ |
| 8230 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 8231 | { |
| 8232 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8233 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 8234 | int ret; |
| 8235 | |
| 8236 | /* First argument (self) must be a table */ |
| 8237 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8238 | |
| 8239 | /* Push in the stack the "headers" entry. */ |
| 8240 | ret = lua_getfield(L, 1, "headers"); |
| 8241 | if (ret != LUA_TTABLE) { |
| 8242 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 8243 | WILL_LJMP(lua_error(L)); |
| 8244 | } |
| 8245 | |
| 8246 | /* check if the header is already registered. If not, register it. */ |
| 8247 | ret = lua_getfield(L, -1, name); |
| 8248 | if (ret == LUA_TNIL) { |
| 8249 | /* Entry not found. */ |
| 8250 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 8251 | |
| 8252 | /* Insert the new header name in the array in the top of the stack. |
| 8253 | * It left the new array in the top of the stack. |
| 8254 | */ |
| 8255 | lua_newtable(L); |
| 8256 | lua_pushstring(L, name); |
| 8257 | lua_pushvalue(L, -2); |
| 8258 | lua_settable(L, -4); |
| 8259 | } |
| 8260 | else if (ret != LUA_TTABLE) { |
| 8261 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 8262 | WILL_LJMP(lua_error(L)); |
| 8263 | } |
| 8264 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8265 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8266 | * the header value as new entry. |
| 8267 | */ |
| 8268 | lua_pushstring(L, value); |
| 8269 | ret = lua_rawlen(L, -2); |
| 8270 | lua_rawseti(L, -2, ret + 1); |
| 8271 | |
| 8272 | lua_pushboolean(L, 1); |
| 8273 | return 1; |
| 8274 | } |
| 8275 | |
| 8276 | /* Remove all occurrences of a given header name. */ |
| 8277 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 8278 | { |
| 8279 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8280 | int ret; |
| 8281 | |
| 8282 | /* First argument (self) must be a table */ |
| 8283 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8284 | |
| 8285 | /* Push in the stack the "headers" entry. */ |
| 8286 | ret = lua_getfield(L, 1, "headers"); |
| 8287 | if (ret != LUA_TTABLE) { |
| 8288 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 8289 | WILL_LJMP(lua_error(L)); |
| 8290 | } |
| 8291 | |
| 8292 | lua_pushstring(L, name); |
| 8293 | lua_pushnil(L); |
| 8294 | lua_settable(L, -3); |
| 8295 | |
| 8296 | lua_pushboolean(L, 1); |
| 8297 | return 1; |
| 8298 | } |
| 8299 | |
| 8300 | /* Set the reply's body. Overwrite any existing entry. */ |
| 8301 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 8302 | { |
| 8303 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8304 | |
| 8305 | /* First argument (self) must be a table */ |
| 8306 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8307 | |
| 8308 | lua_pushstring(L, payload); |
| 8309 | lua_setfield(L, 1, "body"); |
| 8310 | |
| 8311 | lua_pushboolean(L, 1); |
| 8312 | return 1; |
| 8313 | } |
| 8314 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8315 | __LJMP static int hlua_log(lua_State *L) |
| 8316 | { |
| 8317 | int level; |
| 8318 | const char *msg; |
| 8319 | |
| 8320 | MAY_LJMP(check_args(L, 2, "log")); |
| 8321 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 8322 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8323 | |
| 8324 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 8325 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 8326 | |
| 8327 | hlua_sendlog(NULL, level, msg); |
| 8328 | return 0; |
| 8329 | } |
| 8330 | |
| 8331 | __LJMP static int hlua_log_debug(lua_State *L) |
| 8332 | { |
| 8333 | const char *msg; |
| 8334 | |
| 8335 | MAY_LJMP(check_args(L, 1, "debug")); |
| 8336 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8337 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 8338 | return 0; |
| 8339 | } |
| 8340 | |
| 8341 | __LJMP static int hlua_log_info(lua_State *L) |
| 8342 | { |
| 8343 | const char *msg; |
| 8344 | |
| 8345 | MAY_LJMP(check_args(L, 1, "info")); |
| 8346 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8347 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 8348 | return 0; |
| 8349 | } |
| 8350 | |
| 8351 | __LJMP static int hlua_log_warning(lua_State *L) |
| 8352 | { |
| 8353 | const char *msg; |
| 8354 | |
| 8355 | MAY_LJMP(check_args(L, 1, "warning")); |
| 8356 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8357 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 8358 | return 0; |
| 8359 | } |
| 8360 | |
| 8361 | __LJMP static int hlua_log_alert(lua_State *L) |
| 8362 | { |
| 8363 | const char *msg; |
| 8364 | |
| 8365 | MAY_LJMP(check_args(L, 1, "alert")); |
| 8366 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8367 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8368 | return 0; |
| 8369 | } |
| 8370 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8371 | __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] | 8372 | { |
| 8373 | int wakeup_ms = lua_tointeger(L, -1); |
Willy Tarreau | 12c0270 | 2021-09-30 16:12:31 +0200 | [diff] [blame] | 8374 | if (!tick_is_expired(wakeup_ms, now_ms)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8375 | 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] | 8376 | return 0; |
| 8377 | } |
| 8378 | |
| 8379 | __LJMP static int hlua_sleep(lua_State *L) |
| 8380 | { |
| 8381 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8382 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8383 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8384 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8385 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8386 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8387 | wakeup_ms = tick_add(now_ms, delay); |
| 8388 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8389 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8390 | 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] | 8391 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8392 | } |
| 8393 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8394 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8395 | { |
| 8396 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8397 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8398 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8399 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8400 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8401 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8402 | wakeup_ms = tick_add(now_ms, delay); |
| 8403 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8404 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8405 | 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] | 8406 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8407 | } |
| 8408 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8409 | /* This functionis an LUA binding. it permits to give back |
| 8410 | * the hand at the HAProxy scheduler. It is used when the |
| 8411 | * LUA processing consumes a lot of time. |
| 8412 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8413 | __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] | 8414 | { |
| 8415 | return 0; |
| 8416 | } |
| 8417 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8418 | __LJMP static int hlua_yield(lua_State *L) |
| 8419 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8420 | 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] | 8421 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8422 | } |
| 8423 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8424 | /* This function change the nice of the currently executed |
| 8425 | * task. It is used set low or high priority at the current |
| 8426 | * task. |
| 8427 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8428 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8429 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8430 | struct hlua *hlua; |
| 8431 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8432 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8433 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8434 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8435 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8436 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 8437 | hlua = hlua_gethlua(L); |
| 8438 | |
| 8439 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8440 | if (!hlua || !hlua->task) |
| 8441 | return 0; |
| 8442 | |
| 8443 | if (nice < -1024) |
| 8444 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8445 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8446 | nice = 1024; |
| 8447 | |
| 8448 | hlua->task->nice = nice; |
| 8449 | return 0; |
| 8450 | } |
| 8451 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8452 | /* 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] | 8453 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 8454 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 8455 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8456 | * |
| 8457 | * Task wrapper are longjmp safe because the only one Lua code |
| 8458 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8459 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 8460 | 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] | 8461 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 8462 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8463 | enum hlua_exec status; |
| 8464 | |
Willy Tarreau | e35f032 | 2022-06-14 11:00:46 +0200 | [diff] [blame] | 8465 | if (atleast2(task->thread_mask)) |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 8466 | task_set_affinity(task, tid_bit); |
| 8467 | |
Willy Tarreau | 6ef52f4 | 2022-06-15 14:19:48 +0200 | [diff] [blame] | 8468 | if (task->tid < 0) |
| 8469 | task->tid = tid; |
| 8470 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8471 | /* If it is the first call to the task, we must initialize the |
| 8472 | * execution timeouts. |
| 8473 | */ |
| 8474 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 8475 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8476 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8477 | /* Execute the Lua code. */ |
| 8478 | status = hlua_ctx_resume(hlua, 1); |
| 8479 | |
| 8480 | switch (status) { |
| 8481 | /* finished or yield */ |
| 8482 | case HLUA_E_OK: |
| 8483 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8484 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 8485 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8486 | break; |
| 8487 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 8488 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 8489 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 8490 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8491 | break; |
| 8492 | |
| 8493 | /* finished with error. */ |
| 8494 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8495 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8496 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8497 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8498 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8499 | break; |
| 8500 | |
| 8501 | case HLUA_E_ERR: |
| 8502 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8503 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8504 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8505 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8506 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8507 | break; |
| 8508 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8509 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8510 | } |
| 8511 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8512 | /* This function is an LUA binding that register LUA function to be |
| 8513 | * executed after the HAProxy configuration parsing and before the |
| 8514 | * HAProxy scheduler starts. This function expect only one LUA |
| 8515 | * argument that is a function. This function returns nothing, but |
| 8516 | * throws if an error is encountered. |
| 8517 | */ |
| 8518 | __LJMP static int hlua_register_init(lua_State *L) |
| 8519 | { |
| 8520 | struct hlua_init_function *init; |
| 8521 | int ref; |
| 8522 | |
| 8523 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 8524 | |
| 8525 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8526 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8527 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8528 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8529 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8530 | |
| 8531 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 8532 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8533 | return 0; |
| 8534 | } |
| 8535 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8536 | /* This functio is an LUA binding. It permits to register a task |
| 8537 | * executed in parallel of the main HAroxy activity. The task is |
| 8538 | * created and it is set in the HAProxy scheduler. It can be called |
| 8539 | * from the "init" section, "post init" or during the runtime. |
| 8540 | * |
| 8541 | * Lua prototype: |
| 8542 | * |
| 8543 | * <none> core.register_task(<function>) |
| 8544 | */ |
| 8545 | static int hlua_register_task(lua_State *L) |
| 8546 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8547 | struct hlua *hlua = NULL; |
| 8548 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8549 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8550 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8551 | |
| 8552 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 8553 | |
| 8554 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8555 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8556 | /* Get the reference state. If the reference is NULL, L is the master |
| 8557 | * state, otherwise hlua->T is. |
| 8558 | */ |
| 8559 | hlua = hlua_gethlua(L); |
| 8560 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8561 | /* we are in runtime processing */ |
| 8562 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8563 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8564 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8565 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8566 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8567 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8568 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8569 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8570 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8571 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8572 | /* We are in the common lua state, execute the task anywhere, |
| 8573 | * otherwise, inherit the current thread identifier |
| 8574 | */ |
| 8575 | if (state_id == 0) |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8576 | task = task_new_anywhere(); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8577 | else |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8578 | task = task_new_here(); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8579 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8580 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8581 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8582 | task->context = hlua; |
| 8583 | task->process = hlua_process_task; |
| 8584 | |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8585 | if (!hlua_ctx_init(hlua, state_id, task, 1)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8586 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8587 | |
| 8588 | /* Restore the function in the stack. */ |
| 8589 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 8590 | hlua->nargs = 0; |
| 8591 | |
| 8592 | /* Schedule task. */ |
Willy Tarreau | e3957f8 | 2021-09-30 16:17:37 +0200 | [diff] [blame] | 8593 | task_wakeup(task, TASK_WOKEN_INIT); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8594 | |
| 8595 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8596 | |
| 8597 | alloc_error: |
| 8598 | task_destroy(task); |
| 8599 | hlua_ctx_destroy(hlua); |
| 8600 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8601 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8602 | } |
| 8603 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8604 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 8605 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 8606 | * resume converters. |
| 8607 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8608 | 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] | 8609 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8610 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8611 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8612 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8613 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8614 | if (!stream) |
| 8615 | return 0; |
| 8616 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8617 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8618 | * Lua context can be not initialized. This behavior |
| 8619 | * permits to save performances because a systematic |
| 8620 | * Lua initialization cause 5% performances loss. |
| 8621 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8622 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8623 | struct hlua *hlua; |
| 8624 | |
| 8625 | hlua = pool_alloc(pool_head_hlua); |
| 8626 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8627 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8628 | return 0; |
| 8629 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8630 | HLUA_INIT(hlua); |
| 8631 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8632 | 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] | 8633 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8634 | return 0; |
| 8635 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8636 | } |
| 8637 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8638 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8639 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8640 | |
| 8641 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8642 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8643 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8644 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8645 | else |
| 8646 | error = "critical error"; |
| 8647 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8648 | return 0; |
| 8649 | } |
| 8650 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8651 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8652 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8653 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8654 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8655 | return 0; |
| 8656 | } |
| 8657 | |
| 8658 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8659 | 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] | 8660 | |
| 8661 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8662 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8663 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8664 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8665 | return 0; |
| 8666 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8667 | hlua_smp2lua(stream->hlua->T, smp); |
| 8668 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8669 | |
| 8670 | /* push keywords in the stack. */ |
| 8671 | if (arg_p) { |
| 8672 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8673 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8674 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8675 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8676 | return 0; |
| 8677 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8678 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8679 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8680 | } |
| 8681 | } |
| 8682 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8683 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8684 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8685 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8686 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8687 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8688 | } |
| 8689 | |
| 8690 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8691 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8692 | /* finished. */ |
| 8693 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8694 | /* If the stack is empty, the function fails. */ |
| 8695 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8696 | return 0; |
| 8697 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8698 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8699 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8700 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8701 | return 1; |
| 8702 | |
| 8703 | /* yield. */ |
| 8704 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8705 | 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] | 8706 | return 0; |
| 8707 | |
| 8708 | /* finished with error. */ |
| 8709 | case HLUA_E_ERRMSG: |
| 8710 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8711 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8712 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8713 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8714 | return 0; |
| 8715 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8716 | case HLUA_E_ETMOUT: |
| 8717 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 8718 | return 0; |
| 8719 | |
| 8720 | case HLUA_E_NOMEM: |
| 8721 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 8722 | return 0; |
| 8723 | |
| 8724 | case HLUA_E_YIELD: |
| 8725 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 8726 | return 0; |
| 8727 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8728 | case HLUA_E_ERR: |
| 8729 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8730 | 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] | 8731 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8732 | |
| 8733 | default: |
| 8734 | return 0; |
| 8735 | } |
| 8736 | } |
| 8737 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8738 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 8739 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8740 | * resume sample-fetches. This function will be called by the sample |
| 8741 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8742 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8743 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 8744 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8745 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8746 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8747 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8748 | const char *error; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 8749 | unsigned int hflags = HLUA_TXN_NOTERM | HLUA_TXN_SMP_CTX; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8750 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8751 | if (!stream) |
| 8752 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8753 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8754 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8755 | * Lua context can be not initialized. This behavior |
| 8756 | * permits to save performances because a systematic |
| 8757 | * Lua initialization cause 5% performances loss. |
| 8758 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8759 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8760 | struct hlua *hlua; |
| 8761 | |
| 8762 | hlua = pool_alloc(pool_head_hlua); |
| 8763 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8764 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8765 | return 0; |
| 8766 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8767 | hlua->T = NULL; |
| 8768 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8769 | 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] | 8770 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8771 | return 0; |
| 8772 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8773 | } |
| 8774 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8775 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8776 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8777 | |
| 8778 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8779 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8780 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8781 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8782 | else |
| 8783 | error = "critical error"; |
| 8784 | 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] | 8785 | return 0; |
| 8786 | } |
| 8787 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8788 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8789 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8790 | 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] | 8791 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8792 | return 0; |
| 8793 | } |
| 8794 | |
| 8795 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8796 | 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] | 8797 | |
| 8798 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 8799 | 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] | 8800 | 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] | 8801 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8802 | return 0; |
| 8803 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8804 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8805 | |
| 8806 | /* push keywords in the stack. */ |
| 8807 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 8808 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8809 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8810 | 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] | 8811 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8812 | return 0; |
| 8813 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8814 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8815 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8816 | } |
| 8817 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8818 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8819 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8820 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8821 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8822 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8823 | } |
| 8824 | |
| 8825 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8826 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8827 | /* finished. */ |
| 8828 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8829 | /* If the stack is empty, the function fails. */ |
| 8830 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8831 | return 0; |
| 8832 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8833 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8834 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8835 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8836 | |
| 8837 | /* Set the end of execution flag. */ |
| 8838 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 8839 | return 1; |
| 8840 | |
| 8841 | /* yield. */ |
| 8842 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8843 | 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] | 8844 | return 0; |
| 8845 | |
| 8846 | /* finished with error. */ |
| 8847 | case HLUA_E_ERRMSG: |
| 8848 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8849 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8850 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8851 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8852 | return 0; |
| 8853 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8854 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8855 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 8856 | return 0; |
| 8857 | |
| 8858 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8859 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 8860 | return 0; |
| 8861 | |
| 8862 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8863 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 8864 | return 0; |
| 8865 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8866 | case HLUA_E_ERR: |
| 8867 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8868 | 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] | 8869 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8870 | |
| 8871 | default: |
| 8872 | return 0; |
| 8873 | } |
| 8874 | } |
| 8875 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8876 | /* This function is an LUA binding used for registering |
| 8877 | * "sample-conv" functions. It expects a converter name used |
| 8878 | * in the haproxy configuration file, and an LUA function. |
| 8879 | */ |
| 8880 | __LJMP static int hlua_register_converters(lua_State *L) |
| 8881 | { |
| 8882 | struct sample_conv_kw_list *sck; |
| 8883 | const char *name; |
| 8884 | int ref; |
| 8885 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8886 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8887 | struct sample_conv *sc; |
| 8888 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8889 | |
| 8890 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 8891 | |
| 8892 | /* First argument : converter name. */ |
| 8893 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8894 | |
| 8895 | /* Second argument : lua function. */ |
| 8896 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 8897 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8898 | /* Check if the converter is already registered */ |
| 8899 | trash = get_trash_chunk(); |
| 8900 | chunk_printf(trash, "lua.%s", name); |
| 8901 | sc = find_sample_conv(trash->area, trash->data); |
| 8902 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8903 | fcn = sc->private; |
| 8904 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8905 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 8906 | "This will become a hard error in version 2.5.\n", name); |
| 8907 | } |
| 8908 | fcn->function_ref[hlua_state_id] = ref; |
| 8909 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8910 | } |
| 8911 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8912 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8913 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8914 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8915 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8916 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8917 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8918 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8919 | |
| 8920 | /* Fill fcn. */ |
| 8921 | fcn->name = strdup(name); |
| 8922 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8923 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8924 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8925 | |
| 8926 | /* List head */ |
| 8927 | sck->list.n = sck->list.p = NULL; |
| 8928 | |
| 8929 | /* converter keyword. */ |
| 8930 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8931 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8932 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8933 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8934 | |
| 8935 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 8936 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 8937 | 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] | 8938 | sck->kw[0].val_args = NULL; |
| 8939 | sck->kw[0].in_type = SMP_T_STR; |
| 8940 | sck->kw[0].out_type = SMP_T_STR; |
| 8941 | sck->kw[0].private = fcn; |
| 8942 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8943 | /* Register this new converter */ |
| 8944 | sample_register_convs(sck); |
| 8945 | |
| 8946 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8947 | |
| 8948 | alloc_error: |
| 8949 | release_hlua_function(fcn); |
| 8950 | ha_free(&sck); |
| 8951 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8952 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8953 | } |
| 8954 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8955 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8956 | * "sample-fetch" functions. It expects a converter name used |
| 8957 | * in the haproxy configuration file, and an LUA function. |
| 8958 | */ |
| 8959 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 8960 | { |
| 8961 | const char *name; |
| 8962 | int ref; |
| 8963 | int len; |
| 8964 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8965 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8966 | struct sample_fetch *sf; |
| 8967 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8968 | |
| 8969 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 8970 | |
| 8971 | /* First argument : sample-fetch name. */ |
| 8972 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8973 | |
| 8974 | /* Second argument : lua function. */ |
| 8975 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 8976 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8977 | /* Check if the sample-fetch is already registered */ |
| 8978 | trash = get_trash_chunk(); |
| 8979 | chunk_printf(trash, "lua.%s", name); |
| 8980 | sf = find_sample_fetch(trash->area, trash->data); |
| 8981 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8982 | fcn = sf->private; |
| 8983 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8984 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 8985 | "This will become a hard error in version 2.5.\n", name); |
| 8986 | } |
| 8987 | fcn->function_ref[hlua_state_id] = ref; |
| 8988 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8989 | } |
| 8990 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8991 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8992 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8993 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8994 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8995 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8996 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8997 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8998 | |
| 8999 | /* Fill fcn. */ |
| 9000 | fcn->name = strdup(name); |
| 9001 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9002 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9003 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9004 | |
| 9005 | /* List head */ |
| 9006 | sfk->list.n = sfk->list.p = NULL; |
| 9007 | |
| 9008 | /* sample-fetch keyword. */ |
| 9009 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9010 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9011 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9012 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9013 | |
| 9014 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 9015 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 9016 | 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] | 9017 | sfk->kw[0].val_args = NULL; |
| 9018 | sfk->kw[0].out_type = SMP_T_STR; |
| 9019 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 9020 | sfk->kw[0].val = 0; |
| 9021 | sfk->kw[0].private = fcn; |
| 9022 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9023 | /* Register this new fetch. */ |
| 9024 | sample_register_fetches(sfk); |
| 9025 | |
| 9026 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9027 | |
| 9028 | alloc_error: |
| 9029 | release_hlua_function(fcn); |
| 9030 | ha_free(&sfk); |
| 9031 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9032 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9033 | } |
| 9034 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 9035 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9036 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 9037 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9038 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9039 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9040 | unsigned int delay; |
| 9041 | unsigned int wakeup_ms; |
| 9042 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9043 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 9044 | hlua = hlua_gethlua(L); |
| 9045 | if (!hlua) { |
| 9046 | return 0; |
| 9047 | } |
| 9048 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9049 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 9050 | |
| 9051 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 9052 | wakeup_ms = tick_add(now_ms, delay); |
| 9053 | hlua->wake_time = wakeup_ms; |
| 9054 | return 0; |
| 9055 | } |
| 9056 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9057 | /* This function is a wrapper to execute each LUA function declared as an action |
| 9058 | * wrapper during the initialisation period. This function may return any |
| 9059 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 9060 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 9061 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9062 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9063 | 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] | 9064 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9065 | { |
| 9066 | char **arg; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 9067 | unsigned int hflags = HLUA_TXN_ACT_CTX; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9068 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9069 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9070 | |
| 9071 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 9072 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 9073 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 9074 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 9075 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9076 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9077 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9078 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9079 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9080 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9081 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9082 | * Lua context can be not initialized. This behavior |
| 9083 | * permits to save performances because a systematic |
| 9084 | * Lua initialization cause 5% performances loss. |
| 9085 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9086 | if (!s->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9087 | struct hlua *hlua; |
| 9088 | |
| 9089 | hlua = pool_alloc(pool_head_hlua); |
| 9090 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9091 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9092 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9093 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9094 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9095 | HLUA_INIT(hlua); |
| 9096 | s->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9097 | 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] | 9098 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9099 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9100 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9101 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9102 | } |
| 9103 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9104 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9105 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9106 | |
| 9107 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9108 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9109 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 9110 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9111 | else |
| 9112 | error = "critical error"; |
| 9113 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9114 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9115 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9116 | } |
| 9117 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9118 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9119 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9120 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9121 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9122 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9123 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9124 | } |
| 9125 | |
| 9126 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9127 | 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] | 9128 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9129 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 9130 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9131 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9132 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9133 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9134 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9135 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9136 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9137 | |
| 9138 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9139 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9140 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9141 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9142 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9143 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9144 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9145 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9146 | lua_pushstring(s->hlua->T, *arg); |
| 9147 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9148 | } |
| 9149 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9150 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9151 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9152 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9153 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9154 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9155 | } |
| 9156 | |
| 9157 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 9158 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9159 | /* finished. */ |
| 9160 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9161 | /* Catch the return value */ |
| 9162 | if (lua_gettop(s->hlua->T) > 0) |
| 9163 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9164 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9165 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 9166 | if (act_ret == ACT_RET_YIELD) { |
| 9167 | if (flags & ACT_OPT_FINAL) |
| 9168 | goto err_yield; |
| 9169 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9170 | if (dir == SMP_OPT_DIR_REQ) |
| 9171 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 9172 | s->hlua->wake_time); |
| 9173 | else |
| 9174 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 9175 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9176 | } |
| 9177 | goto end; |
| 9178 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9179 | /* yield. */ |
| 9180 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 9181 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9182 | if (dir == SMP_OPT_DIR_REQ) |
| 9183 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 9184 | s->hlua->wake_time); |
| 9185 | else |
| 9186 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 9187 | s->hlua->wake_time); |
| 9188 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9189 | /* Some actions can be wake up when a "write" event |
| 9190 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9191 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9192 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 9193 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9194 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9195 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9196 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9197 | act_ret = ACT_RET_YIELD; |
| 9198 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9199 | |
| 9200 | /* finished with error. */ |
| 9201 | case HLUA_E_ERRMSG: |
| 9202 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9203 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9204 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9205 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9206 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9207 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9208 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9209 | 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] | 9210 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9211 | |
| 9212 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9213 | 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] | 9214 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9215 | |
| 9216 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 9217 | err_yield: |
| 9218 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9219 | 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] | 9220 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9221 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9222 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9223 | case HLUA_E_ERR: |
| 9224 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9225 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9226 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9227 | |
| 9228 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9229 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9230 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9231 | |
| 9232 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 9233 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9234 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9235 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9236 | } |
| 9237 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 9238 | 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] | 9239 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 9240 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9241 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9242 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 9243 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 9244 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9245 | } |
| 9246 | |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9247 | static int hlua_applet_tcp_init(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9248 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9249 | struct hlua_tcp_ctx *tcp_ctx = applet_reserve_svcctx(ctx, sizeof(*tcp_ctx)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 9250 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9251 | struct stream *strm = __sc_strm(sc); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9252 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9253 | struct task *task; |
| 9254 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9255 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9256 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9257 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9258 | if (!hlua) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9259 | SEND_ERR(strm->be, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9260 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9261 | return -1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9262 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9263 | HLUA_INIT(hlua); |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9264 | tcp_ctx->hlua = hlua; |
| 9265 | tcp_ctx->flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9266 | |
| 9267 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 9268 | task = task_new_here(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9269 | if (!task) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9270 | SEND_ERR(strm->be, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9271 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9272 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9273 | } |
| 9274 | task->nice = 0; |
| 9275 | task->context = ctx; |
| 9276 | task->process = hlua_applet_wakeup; |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9277 | tcp_ctx->task = task; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9278 | |
| 9279 | /* In the execution wrappers linked with a stream, the |
| 9280 | * Lua context can be not initialized. This behavior |
| 9281 | * permits to save performances because a systematic |
| 9282 | * Lua initialization cause 5% performances loss. |
| 9283 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9284 | 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] | 9285 | 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] | 9286 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9287 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9288 | } |
| 9289 | |
| 9290 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 9291 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9292 | |
| 9293 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9294 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9295 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9296 | error = lua_tostring(hlua->T, -1); |
| 9297 | else |
| 9298 | error = "critical error"; |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9299 | SEND_ERR(strm->be, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9300 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9301 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9302 | } |
| 9303 | |
| 9304 | /* Check stack available size. */ |
| 9305 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9306 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9307 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9308 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9309 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9310 | } |
| 9311 | |
| 9312 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9313 | 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] | 9314 | |
| 9315 | /* Create and and push object stream in the stack. */ |
| 9316 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9317 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9318 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9319 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9320 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9321 | } |
| 9322 | hlua->nargs = 1; |
| 9323 | |
| 9324 | /* push keywords in the stack. */ |
| 9325 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 9326 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9327 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9328 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9329 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9330 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9331 | } |
| 9332 | lua_pushstring(hlua->T, *arg); |
| 9333 | hlua->nargs++; |
| 9334 | } |
| 9335 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9336 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9337 | |
| 9338 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 9339 | applet_need_more_data(ctx); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 9340 | applet_have_more_data(ctx); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9341 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9342 | return 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9343 | } |
| 9344 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 9345 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9346 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9347 | struct hlua_tcp_ctx *tcp_ctx = ctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 9348 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9349 | struct stream *strm = __sc_strm(sc); |
| 9350 | struct channel *res = sc_ic(sc); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9351 | struct act_rule *rule = ctx->rule; |
| 9352 | struct proxy *px = strm->be; |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9353 | struct hlua *hlua = tcp_ctx->hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9354 | |
| 9355 | /* The applet execution is already done. */ |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9356 | if (tcp_ctx->flags & APPLET_DONE) { |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 9357 | /* eat the whole request */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9358 | co_skip(sc_oc(sc), co_data(sc_oc(sc))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9359 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 9360 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9361 | |
| 9362 | /* If the stream is disconnect or closed, ldo nothing. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9363 | if (unlikely(sc->state == SC_ST_DIS || sc->state == SC_ST_CLO)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9364 | return; |
| 9365 | |
| 9366 | /* Execute the function. */ |
| 9367 | switch (hlua_ctx_resume(hlua, 1)) { |
| 9368 | /* finished. */ |
| 9369 | case HLUA_E_OK: |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9370 | tcp_ctx->flags |= APPLET_DONE; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9371 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9372 | /* eat the whole request */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9373 | co_skip(sc_oc(sc), co_data(sc_oc(sc))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9374 | res->flags |= CF_READ_NULL; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9375 | sc_shutr(sc); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9376 | return; |
| 9377 | |
| 9378 | /* yield. */ |
| 9379 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 9380 | if (hlua->wake_time != TICK_ETERNITY) |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9381 | task_schedule(tcp_ctx->task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9382 | return; |
| 9383 | |
| 9384 | /* finished with error. */ |
| 9385 | case HLUA_E_ERRMSG: |
| 9386 | /* Display log. */ |
| 9387 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9388 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9389 | lua_pop(hlua->T, 1); |
| 9390 | goto error; |
| 9391 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9392 | case HLUA_E_ETMOUT: |
| 9393 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9394 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9395 | goto error; |
| 9396 | |
| 9397 | case HLUA_E_NOMEM: |
| 9398 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9399 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9400 | goto error; |
| 9401 | |
| 9402 | case HLUA_E_YIELD: /* unexpected */ |
| 9403 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9404 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9405 | goto error; |
| 9406 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9407 | case HLUA_E_ERR: |
| 9408 | /* Display log. */ |
| 9409 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9410 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9411 | goto error; |
| 9412 | |
| 9413 | default: |
| 9414 | goto error; |
| 9415 | } |
| 9416 | |
| 9417 | error: |
| 9418 | |
| 9419 | /* For all other cases, just close the stream. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9420 | sc_shutw(sc); |
| 9421 | sc_shutr(sc); |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9422 | tcp_ctx->flags |= APPLET_DONE; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9423 | } |
| 9424 | |
| 9425 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 9426 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9427 | struct hlua_tcp_ctx *tcp_ctx = ctx->svcctx; |
| 9428 | |
| 9429 | task_destroy(tcp_ctx->task); |
| 9430 | tcp_ctx->task = NULL; |
| 9431 | hlua_ctx_destroy(tcp_ctx->hlua); |
| 9432 | tcp_ctx->hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9433 | } |
| 9434 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9435 | /* The function returns 0 if the initialisation is complete or -1 if |
| 9436 | * an errors occurs. It also reserves the appctx for an hlua_http_ctx. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9437 | */ |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9438 | static int hlua_applet_http_init(struct appctx *ctx) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9439 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9440 | struct hlua_http_ctx *http_ctx = applet_reserve_svcctx(ctx, sizeof(*http_ctx)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 9441 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9442 | struct stream *strm = __sc_strm(sc); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9443 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9444 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9445 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9446 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9447 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9448 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9449 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9450 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9451 | if (!hlua) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9452 | SEND_ERR(strm->be, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9453 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9454 | return -1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9455 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9456 | HLUA_INIT(hlua); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9457 | http_ctx->hlua = hlua; |
| 9458 | http_ctx->left_bytes = -1; |
| 9459 | http_ctx->flags = 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9460 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 9461 | if (txn->req.flags & HTTP_MSGF_VER_11) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9462 | http_ctx->flags |= APPLET_HTTP11; |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 9463 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9464 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 9465 | task = task_new_here(); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9466 | if (!task) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9467 | SEND_ERR(strm->be, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9468 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9469 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9470 | } |
| 9471 | task->nice = 0; |
| 9472 | task->context = ctx; |
| 9473 | task->process = hlua_applet_wakeup; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9474 | http_ctx->task = task; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9475 | |
| 9476 | /* In the execution wrappers linked with a stream, the |
| 9477 | * Lua context can be not initialized. This behavior |
| 9478 | * permits to save performances because a systematic |
| 9479 | * Lua initialization cause 5% performances loss. |
| 9480 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9481 | 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] | 9482 | 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] | 9483 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9484 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9485 | } |
| 9486 | |
| 9487 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 9488 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9489 | |
| 9490 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9491 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9492 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9493 | error = lua_tostring(hlua->T, -1); |
| 9494 | else |
| 9495 | error = "critical error"; |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9496 | SEND_ERR(strm->be, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9497 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9498 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9499 | } |
| 9500 | |
| 9501 | /* Check stack available size. */ |
| 9502 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9503 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9504 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9505 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9506 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9507 | } |
| 9508 | |
| 9509 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9510 | 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] | 9511 | |
| 9512 | /* Create and and push object stream in the stack. */ |
| 9513 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9514 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9515 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9516 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9517 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9518 | } |
| 9519 | hlua->nargs = 1; |
| 9520 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9521 | /* push keywords in the stack. */ |
| 9522 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 9523 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9524 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9525 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9526 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9527 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9528 | } |
| 9529 | lua_pushstring(hlua->T, *arg); |
| 9530 | hlua->nargs++; |
| 9531 | } |
| 9532 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9533 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9534 | |
| 9535 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 9536 | applet_need_more_data(ctx); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9537 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9538 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9539 | } |
| 9540 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 9541 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9542 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9543 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 9544 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9545 | struct stream *strm = __sc_strm(sc); |
| 9546 | struct channel *req = sc_oc(sc); |
| 9547 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9548 | struct act_rule *rule = ctx->rule; |
| 9549 | struct proxy *px = strm->be; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9550 | struct hlua *hlua = http_ctx->hlua; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9551 | struct htx *req_htx, *res_htx; |
| 9552 | |
| 9553 | res_htx = htx_from_buf(&res->buf); |
| 9554 | |
| 9555 | /* If the stream is disconnect or closed, ldo nothing. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9556 | if (unlikely(sc->state == SC_ST_DIS || sc->state == SC_ST_CLO)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9557 | goto out; |
| 9558 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9559 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9560 | if (!b_size(&res->buf)) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9561 | sc_need_room(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9562 | goto out; |
| 9563 | } |
| 9564 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9565 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9566 | http_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9567 | |
| 9568 | /* Set the currently running flag. */ |
| 9569 | if (!HLUA_IS_RUNNING(hlua) && |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9570 | !(http_ctx->flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 9571 | if (!co_data(req)) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 9572 | applet_need_more_data(ctx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9573 | goto out; |
| 9574 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9575 | } |
| 9576 | |
| 9577 | /* Executes The applet if it is not done. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9578 | if (!(http_ctx->flags & APPLET_DONE)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9579 | |
| 9580 | /* Execute the function. */ |
| 9581 | switch (hlua_ctx_resume(hlua, 1)) { |
| 9582 | /* finished. */ |
| 9583 | case HLUA_E_OK: |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9584 | http_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9585 | break; |
| 9586 | |
| 9587 | /* yield. */ |
| 9588 | case HLUA_E_AGAIN: |
| 9589 | if (hlua->wake_time != TICK_ETERNITY) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9590 | task_schedule(http_ctx->task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9591 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9592 | |
| 9593 | /* finished with error. */ |
| 9594 | case HLUA_E_ERRMSG: |
| 9595 | /* Display log. */ |
| 9596 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9597 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9598 | lua_pop(hlua->T, 1); |
| 9599 | goto error; |
| 9600 | |
| 9601 | case HLUA_E_ETMOUT: |
| 9602 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9603 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9604 | goto error; |
| 9605 | |
| 9606 | case HLUA_E_NOMEM: |
| 9607 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9608 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9609 | goto error; |
| 9610 | |
| 9611 | case HLUA_E_YIELD: /* unexpected */ |
| 9612 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9613 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9614 | goto error; |
| 9615 | |
| 9616 | case HLUA_E_ERR: |
| 9617 | /* Display log. */ |
| 9618 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9619 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9620 | goto error; |
| 9621 | |
| 9622 | default: |
| 9623 | goto error; |
| 9624 | } |
| 9625 | } |
| 9626 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9627 | if (http_ctx->flags & APPLET_DONE) { |
| 9628 | if (http_ctx->flags & APPLET_RSP_SENT) |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9629 | goto done; |
| 9630 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9631 | if (!(http_ctx->flags & APPLET_HDR_SENT)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9632 | goto error; |
| 9633 | |
Christopher Faulet | f89af9c | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 9634 | /* no more data are expected. If the response buffer is empty |
| 9635 | * for a chunked message, be sure to add something (EOT block in |
| 9636 | * this case) to have something to send. It is important to be |
| 9637 | * sure the EOM flags will be handled by the endpoint. |
| 9638 | */ |
| 9639 | if (htx_is_empty(res_htx) && (strm->txn->rsp.flags & (HTTP_MSGF_XFER_LEN|HTTP_MSGF_CNT_LEN)) == HTTP_MSGF_XFER_LEN) { |
| 9640 | if (!htx_add_endof(res_htx, HTX_BLK_EOT)) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9641 | sc_need_room(sc); |
Christopher Faulet | f89af9c | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 9642 | goto out; |
| 9643 | } |
| 9644 | channel_add_input(res, 1); |
| 9645 | } |
| 9646 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 9647 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | d8d2708 | 2022-03-07 15:50:54 +0100 | [diff] [blame] | 9648 | res->flags |= CF_EOI; |
Willy Tarreau | d869e13 | 2022-05-17 18:05:31 +0200 | [diff] [blame] | 9649 | se_fl_set(ctx->sedesc, SE_FL_EOI); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9650 | strm->txn->status = http_ctx->status; |
| 9651 | http_ctx->flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9652 | } |
| 9653 | |
| 9654 | done: |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9655 | if (http_ctx->flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9656 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9657 | res->flags |= CF_READ_NULL; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9658 | sc_shutr(sc); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9659 | } |
| 9660 | |
| 9661 | /* eat the whole request */ |
| 9662 | if (co_data(req)) { |
| 9663 | req_htx = htx_from_buf(&req->buf); |
| 9664 | co_htx_skip(req, req_htx, co_data(req)); |
| 9665 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9666 | } |
| 9667 | } |
| 9668 | |
| 9669 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9670 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9671 | return; |
| 9672 | |
| 9673 | error: |
| 9674 | |
| 9675 | /* If we are in HTTP mode, and we are not send any |
| 9676 | * data, return a 500 server error in best effort: |
| 9677 | * if there is no room available in the buffer, |
| 9678 | * just close the connection. |
| 9679 | */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9680 | if (!(http_ctx->flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 9681 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9682 | |
| 9683 | channel_erase(res); |
| 9684 | res->buf.data = b_data(err); |
| 9685 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 9686 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 9687 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9688 | } |
| 9689 | if (!(strm->flags & SF_ERR_MASK)) |
| 9690 | strm->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9691 | http_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9692 | goto done; |
| 9693 | } |
| 9694 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9695 | static void hlua_applet_http_release(struct appctx *ctx) |
| 9696 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9697 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
| 9698 | |
| 9699 | task_destroy(http_ctx->task); |
| 9700 | http_ctx->task = NULL; |
| 9701 | hlua_ctx_destroy(http_ctx->hlua); |
| 9702 | http_ctx->hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9703 | } |
| 9704 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9705 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9706 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9707 | * |
| 9708 | * This function can fail with an abort() due to an Lua critical error. |
| 9709 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 9710 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9711 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9712 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 9713 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9714 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9715 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9716 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9717 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9718 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9719 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9720 | if (!rule->arg.hlua_rule) { |
| 9721 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9722 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9723 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9724 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9725 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 9726 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 9727 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9728 | if (!rule->arg.hlua_rule->args) { |
| 9729 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9730 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9731 | } |
| 9732 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9733 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9734 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9735 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9736 | /* Expect some arguments */ |
| 9737 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9738 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9739 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9740 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9741 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9742 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9743 | if (!rule->arg.hlua_rule->args[i]) { |
| 9744 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9745 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9746 | } |
| 9747 | (*cur_arg)++; |
| 9748 | } |
| 9749 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9750 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 9751 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9752 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9753 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9754 | |
| 9755 | error: |
| 9756 | if (rule->arg.hlua_rule) { |
| 9757 | if (rule->arg.hlua_rule->args) { |
| 9758 | for (i = 0; i < fcn->nargs; i++) |
| 9759 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 9760 | ha_free(&rule->arg.hlua_rule->args); |
| 9761 | } |
| 9762 | ha_free(&rule->arg.hlua_rule); |
| 9763 | } |
| 9764 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9765 | } |
| 9766 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9767 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 9768 | struct act_rule *rule, char **err) |
| 9769 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9770 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9771 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9772 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9773 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9774 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9775 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9776 | * the call of this analyzer. |
| 9777 | */ |
| 9778 | if (rule->from != ACT_F_HTTP_REQ) { |
| 9779 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 9780 | return ACT_RET_PRS_ERR; |
| 9781 | } |
| 9782 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9783 | /* Memory for the rule. */ |
| 9784 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 9785 | if (!rule->arg.hlua_rule) { |
| 9786 | memprintf(err, "out of memory error"); |
| 9787 | return ACT_RET_PRS_ERR; |
| 9788 | } |
| 9789 | |
| 9790 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9791 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9792 | |
| 9793 | /* TODO: later accept arguments. */ |
| 9794 | rule->arg.hlua_rule->args = NULL; |
| 9795 | |
| 9796 | /* Add applet pointer in the rule. */ |
| 9797 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 9798 | rule->applet.name = fcn->name; |
| 9799 | rule->applet.init = hlua_applet_http_init; |
| 9800 | rule->applet.fct = hlua_applet_http_fct; |
| 9801 | rule->applet.release = hlua_applet_http_release; |
| 9802 | rule->applet.timeout = hlua_timeout_applet; |
| 9803 | |
| 9804 | return ACT_RET_PRS_OK; |
| 9805 | } |
| 9806 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9807 | /* This function is an LUA binding used for registering |
| 9808 | * "sample-conv" functions. It expects a converter name used |
| 9809 | * in the haproxy configuration file, and an LUA function. |
| 9810 | */ |
| 9811 | __LJMP static int hlua_register_action(lua_State *L) |
| 9812 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9813 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9814 | const char *name; |
| 9815 | int ref; |
| 9816 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9817 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9818 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9819 | struct buffer *trash; |
| 9820 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9821 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9822 | /* Initialise the number of expected arguments at 0. */ |
| 9823 | nargs = 0; |
| 9824 | |
| 9825 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 9826 | 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] | 9827 | |
| 9828 | /* First argument : converter name. */ |
| 9829 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9830 | |
| 9831 | /* Second argument : environment. */ |
| 9832 | if (lua_type(L, 2) != LUA_TTABLE) |
| 9833 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9834 | |
| 9835 | /* Third argument : lua function. */ |
| 9836 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 9837 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9838 | /* 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] | 9839 | if (lua_gettop(L) >= 4) |
| 9840 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 9841 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9842 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9843 | lua_pushnil(L); |
| 9844 | while (lua_next(L, 2) != 0) { |
| 9845 | if (lua_type(L, -1) != LUA_TSTRING) |
| 9846 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9847 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9848 | /* Check if action exists */ |
| 9849 | trash = get_trash_chunk(); |
| 9850 | chunk_printf(trash, "lua.%s", name); |
| 9851 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 9852 | akw = tcp_req_cont_action(trash->area); |
| 9853 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 9854 | akw = tcp_res_cont_action(trash->area); |
| 9855 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 9856 | akw = action_http_req_custom(trash->area); |
| 9857 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 9858 | akw = action_http_res_custom(trash->area); |
| 9859 | } else { |
| 9860 | akw = NULL; |
| 9861 | } |
| 9862 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9863 | fcn = akw->private; |
| 9864 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9865 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 9866 | "This will become a hard error in version 2.5.\n", name); |
| 9867 | } |
| 9868 | fcn->function_ref[hlua_state_id] = ref; |
| 9869 | |
| 9870 | /* pop the environment string. */ |
| 9871 | lua_pop(L, 1); |
| 9872 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9873 | } |
| 9874 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9875 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 9876 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9877 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9878 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9879 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9880 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9881 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9882 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9883 | |
| 9884 | /* Fill fcn. */ |
| 9885 | fcn->name = strdup(name); |
| 9886 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9887 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9888 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9889 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 9890 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9891 | fcn->nargs = nargs; |
| 9892 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9893 | /* List head */ |
| 9894 | akl->list.n = akl->list.p = NULL; |
| 9895 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9896 | /* action keyword. */ |
| 9897 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9898 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9899 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9900 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9901 | |
| 9902 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 9903 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 9904 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9905 | akl->kw[0].private = fcn; |
| 9906 | akl->kw[0].parse = action_register_lua; |
| 9907 | |
| 9908 | /* select the action registering point. */ |
| 9909 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 9910 | tcp_req_cont_keywords_register(akl); |
| 9911 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 9912 | tcp_res_cont_keywords_register(akl); |
| 9913 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 9914 | http_req_keywords_register(akl); |
| 9915 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 9916 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9917 | else { |
| 9918 | release_hlua_function(fcn); |
| 9919 | if (akl) |
| 9920 | ha_free((char **)&(akl->kw[0].kw)); |
| 9921 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 9922 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9923 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 9924 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9925 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9926 | |
| 9927 | /* pop the environment string. */ |
| 9928 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9929 | |
| 9930 | /* reset for next loop */ |
| 9931 | akl = NULL; |
| 9932 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9933 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9934 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9935 | |
| 9936 | alloc_error: |
| 9937 | release_hlua_function(fcn); |
| 9938 | ha_free(&akl); |
| 9939 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9940 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9941 | } |
| 9942 | |
| 9943 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 9944 | struct act_rule *rule, char **err) |
| 9945 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9946 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9947 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 9948 | if (px->mode == PR_MODE_HTTP) { |
| 9949 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 9950 | return ACT_RET_PRS_ERR; |
| 9951 | } |
| 9952 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9953 | /* Memory for the rule. */ |
| 9954 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 9955 | if (!rule->arg.hlua_rule) { |
| 9956 | memprintf(err, "out of memory error"); |
| 9957 | return ACT_RET_PRS_ERR; |
| 9958 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9959 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9960 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9961 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9962 | |
| 9963 | /* TODO: later accept arguments. */ |
| 9964 | rule->arg.hlua_rule->args = NULL; |
| 9965 | |
| 9966 | /* Add applet pointer in the rule. */ |
| 9967 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 9968 | rule->applet.name = fcn->name; |
| 9969 | rule->applet.init = hlua_applet_tcp_init; |
| 9970 | rule->applet.fct = hlua_applet_tcp_fct; |
| 9971 | rule->applet.release = hlua_applet_tcp_release; |
| 9972 | rule->applet.timeout = hlua_timeout_applet; |
| 9973 | |
| 9974 | return 0; |
| 9975 | } |
| 9976 | |
| 9977 | /* This function is an LUA binding used for registering |
| 9978 | * "sample-conv" functions. It expects a converter name used |
| 9979 | * in the haproxy configuration file, and an LUA function. |
| 9980 | */ |
| 9981 | __LJMP static int hlua_register_service(lua_State *L) |
| 9982 | { |
| 9983 | struct action_kw_list *akl; |
| 9984 | const char *name; |
| 9985 | const char *env; |
| 9986 | int ref; |
| 9987 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9988 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9989 | struct buffer *trash; |
| 9990 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9991 | |
| 9992 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 9993 | |
| 9994 | /* First argument : converter name. */ |
| 9995 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9996 | |
| 9997 | /* Second argument : environment. */ |
| 9998 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 9999 | |
| 10000 | /* Third argument : lua function. */ |
| 10001 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 10002 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10003 | /* Check for service already registered */ |
| 10004 | trash = get_trash_chunk(); |
| 10005 | chunk_printf(trash, "lua.%s", name); |
| 10006 | akw = service_find(trash->area); |
| 10007 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10008 | fcn = akw->private; |
| 10009 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 10010 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 10011 | "This will become a hard error in version 2.5.\n", name); |
| 10012 | } |
| 10013 | fcn->function_ref[hlua_state_id] = ref; |
| 10014 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10015 | } |
| 10016 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10017 | /* Allocate and fill the sample fetch keyword struct. */ |
| 10018 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 10019 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10020 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10021 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10022 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10023 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10024 | |
| 10025 | /* Fill fcn. */ |
| 10026 | len = strlen("<lua.>") + strlen(name) + 1; |
| 10027 | fcn->name = calloc(1, len); |
| 10028 | if (!fcn->name) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10029 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10030 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10031 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10032 | |
| 10033 | /* List head */ |
| 10034 | akl->list.n = akl->list.p = NULL; |
| 10035 | |
| 10036 | /* converter keyword. */ |
| 10037 | len = strlen("lua.") + strlen(name) + 1; |
| 10038 | akl->kw[0].kw = calloc(1, len); |
| 10039 | if (!akl->kw[0].kw) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10040 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10041 | |
| 10042 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 10043 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 10044 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10045 | if (strcmp(env, "tcp") == 0) |
| 10046 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10047 | else if (strcmp(env, "http") == 0) |
| 10048 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10049 | else { |
| 10050 | release_hlua_function(fcn); |
| 10051 | if (akl) |
| 10052 | ha_free((char **)&(akl->kw[0].kw)); |
| 10053 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 10054 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 10055 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10056 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10057 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 10058 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10059 | akl->kw[0].private = fcn; |
| 10060 | |
| 10061 | /* End of array. */ |
| 10062 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 10063 | |
| 10064 | /* Register this new converter */ |
| 10065 | service_keywords_register(akl); |
| 10066 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10067 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10068 | |
| 10069 | alloc_error: |
| 10070 | release_hlua_function(fcn); |
| 10071 | ha_free(&akl); |
| 10072 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 10073 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10074 | } |
| 10075 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10076 | /* This function initialises Lua cli handler. It copies the |
| 10077 | * arguments in the Lua stack and create channel IO objects. |
| 10078 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 10079 | 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] | 10080 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10081 | struct hlua_cli_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10082 | struct hlua *hlua; |
| 10083 | struct hlua_function *fcn; |
| 10084 | int i; |
| 10085 | const char *error; |
| 10086 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10087 | fcn = private; |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10088 | ctx->fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10089 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10090 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10091 | if (!hlua) { |
| 10092 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10093 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10094 | } |
| 10095 | HLUA_INIT(hlua); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10096 | ctx->hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10097 | |
| 10098 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 10099 | * 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] | 10100 | * applet_http. It is absolutely compatible. |
| 10101 | */ |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10102 | ctx->task = task_new_here(); |
| 10103 | if (!ctx->task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 10104 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10105 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10106 | } |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10107 | ctx->task->nice = 0; |
| 10108 | ctx->task->context = appctx; |
| 10109 | ctx->task->process = hlua_applet_wakeup; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10110 | |
| 10111 | /* Initialises the Lua context */ |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10112 | if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(fcn), ctx->task, 0)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10113 | 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] | 10114 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10115 | } |
| 10116 | |
| 10117 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10118 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10119 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 10120 | error = lua_tostring(hlua->T, -1); |
| 10121 | else |
| 10122 | error = "critical error"; |
| 10123 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 10124 | goto error; |
| 10125 | } |
| 10126 | |
| 10127 | /* Check stack available size. */ |
| 10128 | if (!lua_checkstack(hlua->T, 2)) { |
| 10129 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10130 | goto error; |
| 10131 | } |
| 10132 | |
| 10133 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10134 | 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] | 10135 | |
| 10136 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 10137 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10138 | */ |
| 10139 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 10140 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10141 | goto error; |
| 10142 | } |
| 10143 | hlua->nargs = 1; |
| 10144 | |
| 10145 | /* push keywords in the stack. */ |
| 10146 | for (i = 0; *args[i]; i++) { |
| 10147 | /* Check stack available size. */ |
| 10148 | if (!lua_checkstack(hlua->T, 1)) { |
| 10149 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10150 | goto error; |
| 10151 | } |
| 10152 | lua_pushstring(hlua->T, args[i]); |
| 10153 | hlua->nargs++; |
| 10154 | } |
| 10155 | |
| 10156 | /* We must initialize the execution timeouts. */ |
| 10157 | hlua->max_time = hlua_timeout_session; |
| 10158 | |
| 10159 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10160 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10161 | |
| 10162 | /* It's ok */ |
| 10163 | return 0; |
| 10164 | |
| 10165 | /* It's not ok. */ |
| 10166 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10167 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10168 | hlua_ctx_destroy(hlua); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10169 | ctx->hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10170 | return 1; |
| 10171 | } |
| 10172 | |
| 10173 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 10174 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10175 | struct hlua_cli_ctx *ctx = appctx->svcctx; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10176 | struct hlua *hlua; |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 10177 | struct stconn *sc; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10178 | struct hlua_function *fcn; |
| 10179 | |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10180 | hlua = ctx->hlua; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 10181 | sc = appctx_sc(appctx); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10182 | fcn = ctx->fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10183 | |
| 10184 | /* If the stream is disconnect or closed, ldo nothing. */ |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 10185 | if (unlikely(sc->state == SC_ST_DIS || sc->state == SC_ST_CLO)) |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10186 | return 1; |
| 10187 | |
| 10188 | /* Execute the function. */ |
| 10189 | switch (hlua_ctx_resume(hlua, 1)) { |
| 10190 | |
| 10191 | /* finished. */ |
| 10192 | case HLUA_E_OK: |
| 10193 | return 1; |
| 10194 | |
| 10195 | /* yield. */ |
| 10196 | case HLUA_E_AGAIN: |
| 10197 | /* We want write. */ |
| 10198 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 10199 | sc_need_room(sc); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10200 | /* Set the timeout. */ |
| 10201 | if (hlua->wake_time != TICK_ETERNITY) |
| 10202 | task_schedule(hlua->task, hlua->wake_time); |
| 10203 | return 0; |
| 10204 | |
| 10205 | /* finished with error. */ |
| 10206 | case HLUA_E_ERRMSG: |
| 10207 | /* Display log. */ |
| 10208 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 10209 | fcn->name, lua_tostring(hlua->T, -1)); |
| 10210 | lua_pop(hlua->T, 1); |
| 10211 | return 1; |
| 10212 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10213 | case HLUA_E_ETMOUT: |
| 10214 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 10215 | fcn->name); |
| 10216 | return 1; |
| 10217 | |
| 10218 | case HLUA_E_NOMEM: |
| 10219 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 10220 | fcn->name); |
| 10221 | return 1; |
| 10222 | |
| 10223 | case HLUA_E_YIELD: /* unexpected */ |
| 10224 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 10225 | fcn->name); |
| 10226 | return 1; |
| 10227 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10228 | case HLUA_E_ERR: |
| 10229 | /* Display log. */ |
| 10230 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 10231 | fcn->name); |
| 10232 | return 1; |
| 10233 | |
| 10234 | default: |
| 10235 | return 1; |
| 10236 | } |
| 10237 | |
| 10238 | return 1; |
| 10239 | } |
| 10240 | |
| 10241 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 10242 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10243 | struct hlua_cli_ctx *ctx = appctx->svcctx; |
| 10244 | |
| 10245 | hlua_ctx_destroy(ctx->hlua); |
| 10246 | ctx->hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10247 | } |
| 10248 | |
| 10249 | /* This function is an LUA binding used for registering |
| 10250 | * new keywords in the cli. It expects a list of keywords |
| 10251 | * which are the "path". It is limited to 5 keywords. A |
| 10252 | * description of the command, a function to be executed |
| 10253 | * for the parsing and a function for io handlers. |
| 10254 | */ |
| 10255 | __LJMP static int hlua_register_cli(lua_State *L) |
| 10256 | { |
| 10257 | struct cli_kw_list *cli_kws; |
| 10258 | const char *message; |
| 10259 | int ref_io; |
| 10260 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10261 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10262 | int index; |
| 10263 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10264 | struct buffer *trash; |
| 10265 | const char *kw[5]; |
| 10266 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10267 | const char *errmsg; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10268 | |
| 10269 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 10270 | |
| 10271 | /* First argument : an array of maximum 5 keywords. */ |
| 10272 | if (!lua_istable(L, 1)) |
| 10273 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 10274 | |
| 10275 | /* Second argument : string with contextual message. */ |
| 10276 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 10277 | |
| 10278 | /* Third and fourth argument : lua function. */ |
| 10279 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 10280 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10281 | /* Check for CLI service already registered */ |
| 10282 | trash = get_trash_chunk(); |
| 10283 | index = 0; |
| 10284 | lua_pushnil(L); |
| 10285 | memset(kw, 0, sizeof(kw)); |
| 10286 | while (lua_next(L, 1) != 0) { |
| 10287 | if (index >= CLI_PREFIX_KW_NB) |
| 10288 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 10289 | if (lua_type(L, -1) != LUA_TSTRING) |
| 10290 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 10291 | kw[index] = lua_tostring(L, -1); |
| 10292 | if (index == 0) |
| 10293 | chunk_printf(trash, "%s", kw[index]); |
| 10294 | else |
| 10295 | chunk_appendf(trash, " %s", kw[index]); |
| 10296 | index++; |
| 10297 | lua_pop(L, 1); |
| 10298 | } |
| 10299 | cli_kw = cli_find_kw_exact((char **)kw); |
| 10300 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10301 | fcn = cli_kw->private; |
| 10302 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 10303 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 10304 | "This will become a hard error in version 2.5.\n", trash->area); |
| 10305 | } |
| 10306 | fcn->function_ref[hlua_state_id] = ref_io; |
| 10307 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10308 | } |
| 10309 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10310 | /* Allocate and fill the sample fetch keyword struct. */ |
| 10311 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10312 | if (!cli_kws) { |
| 10313 | errmsg = "Lua out of memory error."; |
| 10314 | goto error; |
| 10315 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10316 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10317 | if (!fcn) { |
| 10318 | errmsg = "Lua out of memory error."; |
| 10319 | goto error; |
| 10320 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10321 | |
| 10322 | /* Fill path. */ |
| 10323 | index = 0; |
| 10324 | lua_pushnil(L); |
| 10325 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10326 | if (index >= 5) { |
| 10327 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 10328 | goto error; |
| 10329 | } |
| 10330 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 10331 | errmsg = "1st argument must be a table filled with strings"; |
| 10332 | goto error; |
| 10333 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10334 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10335 | if (!cli_kws->kw[0].str_kw[index]) { |
| 10336 | errmsg = "Lua out of memory error."; |
| 10337 | goto error; |
| 10338 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10339 | index++; |
| 10340 | lua_pop(L, 1); |
| 10341 | } |
| 10342 | |
| 10343 | /* Copy help message. */ |
| 10344 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10345 | if (!cli_kws->kw[0].usage) { |
| 10346 | errmsg = "Lua out of memory error."; |
| 10347 | goto error; |
| 10348 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10349 | |
| 10350 | /* Fill fcn io handler. */ |
| 10351 | len = strlen("<lua.cli>") + 1; |
| 10352 | for (i = 0; i < index; i++) |
| 10353 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 10354 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10355 | if (!fcn->name) { |
| 10356 | errmsg = "Lua out of memory error."; |
| 10357 | goto error; |
| 10358 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10359 | strncat((char *)fcn->name, "<lua.cli", len); |
| 10360 | for (i = 0; i < index; i++) { |
| 10361 | strncat((char *)fcn->name, ".", len); |
| 10362 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 10363 | } |
| 10364 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10365 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10366 | |
| 10367 | /* Fill last entries. */ |
| 10368 | cli_kws->kw[0].private = fcn; |
| 10369 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 10370 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 10371 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 10372 | |
| 10373 | /* Register this new converter */ |
| 10374 | cli_register_kw(cli_kws); |
| 10375 | |
| 10376 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10377 | |
| 10378 | error: |
| 10379 | release_hlua_function(fcn); |
| 10380 | if (cli_kws) { |
| 10381 | for (i = 0; i < index; i++) |
| 10382 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 10383 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 10384 | } |
| 10385 | ha_free(&cli_kws); |
| 10386 | WILL_LJMP(luaL_error(L, errmsg)); |
| 10387 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10388 | } |
| 10389 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10390 | static int hlua_filter_init_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 10391 | { |
| 10392 | struct hlua_flt_config *conf = fconf->conf; |
| 10393 | lua_State *L; |
| 10394 | int error, pos, state_id, flt_ref; |
| 10395 | |
| 10396 | state_id = reg_flt_to_stack_id(conf->reg); |
| 10397 | L = hlua_states[state_id]; |
| 10398 | pos = lua_gettop(L); |
| 10399 | |
| 10400 | /* The filter parsing function */ |
| 10401 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->fun_ref[state_id]); |
| 10402 | |
| 10403 | /* Push the filter class on the stack and resolve all callbacks */ |
| 10404 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->flt_ref[state_id]); |
| 10405 | |
| 10406 | /* Duplicate the filter class so each filter will have its own copy */ |
| 10407 | lua_newtable(L); |
| 10408 | lua_pushnil(L); |
| 10409 | |
| 10410 | while (lua_next(L, pos+2)) { |
| 10411 | lua_pushvalue(L, -2); |
| 10412 | lua_insert(L, -2); |
| 10413 | lua_settable(L, -4); |
| 10414 | } |
| 10415 | flt_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
| 10416 | |
| 10417 | /* Remove the original lua filter class from the stack */ |
| 10418 | lua_pop(L, 1); |
| 10419 | |
| 10420 | /* Push the copy on the stack */ |
| 10421 | lua_rawgeti(L, LUA_REGISTRYINDEX, flt_ref); |
| 10422 | |
| 10423 | /* extra args are pushed in a table */ |
| 10424 | lua_newtable(L); |
| 10425 | for (pos = 0; conf->args[pos]; pos++) { |
| 10426 | /* Check stack available size. */ |
| 10427 | if (!lua_checkstack(L, 1)) { |
| 10428 | ha_alert("Lua filter '%s' : Lua error : full stack.", conf->reg->name); |
| 10429 | goto error; |
| 10430 | } |
| 10431 | lua_pushstring(L, conf->args[pos]); |
| 10432 | lua_rawseti(L, -2, lua_rawlen(L, -2) + 1); |
| 10433 | } |
| 10434 | |
| 10435 | error = lua_pcall(L, 2, LUA_MULTRET, 0); |
| 10436 | switch (error) { |
| 10437 | case LUA_OK: |
| 10438 | /* replace the filter ref */ |
| 10439 | conf->ref[state_id] = flt_ref; |
| 10440 | break; |
| 10441 | case LUA_ERRRUN: |
| 10442 | ha_alert("Lua filter '%s' : runtime error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10443 | goto error; |
| 10444 | case LUA_ERRMEM: |
| 10445 | ha_alert("Lua filter '%s' : out of memory error", conf->reg->name); |
| 10446 | goto error; |
| 10447 | case LUA_ERRERR: |
| 10448 | ha_alert("Lua filter '%s' : message handler error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10449 | goto error; |
| 10450 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
| 10451 | case LUA_ERRGCMM: |
| 10452 | ha_alert("Lua filter '%s' : garbage collector error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10453 | goto error; |
| 10454 | #endif |
| 10455 | default: |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 10456 | 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] | 10457 | goto error; |
| 10458 | } |
| 10459 | |
| 10460 | lua_settop(L, 0); |
| 10461 | return 0; |
| 10462 | |
| 10463 | error: |
| 10464 | lua_settop(L, 0); |
| 10465 | return -1; |
| 10466 | } |
| 10467 | |
| 10468 | static void hlua_filter_deinit_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 10469 | { |
| 10470 | struct hlua_flt_config *conf = fconf->conf; |
| 10471 | lua_State *L; |
| 10472 | int state_id; |
| 10473 | |
| 10474 | if (!conf) |
| 10475 | return; |
| 10476 | |
| 10477 | state_id = reg_flt_to_stack_id(conf->reg); |
| 10478 | L = hlua_states[state_id]; |
| 10479 | luaL_unref(L, LUA_REGISTRYINDEX, conf->ref[state_id]); |
| 10480 | } |
| 10481 | |
| 10482 | static int hlua_filter_init(struct proxy *px, struct flt_conf *fconf) |
| 10483 | { |
| 10484 | struct hlua_flt_config *conf = fconf->conf; |
| 10485 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 10486 | |
| 10487 | /* Rely on per-thread init for global scripts */ |
| 10488 | if (!state_id) |
| 10489 | return hlua_filter_init_per_thread(px, fconf); |
| 10490 | return 0; |
| 10491 | } |
| 10492 | |
| 10493 | static void hlua_filter_deinit(struct proxy *px, struct flt_conf *fconf) |
| 10494 | { |
| 10495 | |
| 10496 | if (fconf->conf) { |
| 10497 | struct hlua_flt_config *conf = fconf->conf; |
| 10498 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 10499 | int pos; |
| 10500 | |
| 10501 | /* Rely on per-thread deinit for global scripts */ |
| 10502 | if (!state_id) |
| 10503 | hlua_filter_deinit_per_thread(px, fconf); |
| 10504 | |
| 10505 | for (pos = 0; conf->args[pos]; pos++) |
| 10506 | free(conf->args[pos]); |
| 10507 | free(conf->args); |
| 10508 | } |
| 10509 | ha_free(&fconf->conf); |
| 10510 | ha_free((char **)&fconf->id); |
| 10511 | ha_free(&fconf->ops); |
| 10512 | } |
| 10513 | |
| 10514 | static int hlua_filter_new(struct stream *s, struct filter *filter) |
| 10515 | { |
| 10516 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 10517 | struct hlua_flt_ctx *flt_ctx = NULL; |
| 10518 | int ret = 1; |
| 10519 | |
| 10520 | /* In the execution wrappers linked with a stream, the |
| 10521 | * Lua context can be not initialized. This behavior |
| 10522 | * permits to save performances because a systematic |
| 10523 | * Lua initialization cause 5% performances loss. |
| 10524 | */ |
| 10525 | if (!s->hlua) { |
| 10526 | struct hlua *hlua; |
| 10527 | |
| 10528 | hlua = pool_alloc(pool_head_hlua); |
| 10529 | if (!hlua) { |
| 10530 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 10531 | conf->reg->name); |
| 10532 | ret = 0; |
| 10533 | goto end; |
| 10534 | } |
| 10535 | HLUA_INIT(hlua); |
| 10536 | s->hlua = hlua; |
| 10537 | if (!hlua_ctx_init(s->hlua, reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 10538 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 10539 | conf->reg->name); |
| 10540 | ret = 0; |
| 10541 | goto end; |
| 10542 | } |
| 10543 | } |
| 10544 | |
| 10545 | flt_ctx = pool_zalloc(pool_head_hlua_flt_ctx); |
| 10546 | if (!flt_ctx) { |
| 10547 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10548 | conf->reg->name); |
| 10549 | ret = 0; |
| 10550 | goto end; |
| 10551 | } |
| 10552 | flt_ctx->hlua[0] = pool_alloc(pool_head_hlua); |
| 10553 | flt_ctx->hlua[1] = pool_alloc(pool_head_hlua); |
| 10554 | if (!flt_ctx->hlua[0] || !flt_ctx->hlua[1]) { |
| 10555 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10556 | conf->reg->name); |
| 10557 | ret = 0; |
| 10558 | goto end; |
| 10559 | } |
| 10560 | HLUA_INIT(flt_ctx->hlua[0]); |
| 10561 | HLUA_INIT(flt_ctx->hlua[1]); |
| 10562 | if (!hlua_ctx_init(flt_ctx->hlua[0], reg_flt_to_stack_id(conf->reg), s->task, 0) || |
| 10563 | !hlua_ctx_init(flt_ctx->hlua[1], reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 10564 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10565 | conf->reg->name); |
| 10566 | ret = 0; |
| 10567 | goto end; |
| 10568 | } |
| 10569 | |
| 10570 | if (!HLUA_IS_RUNNING(s->hlua)) { |
| 10571 | /* The following Lua calls can fail. */ |
| 10572 | if (!SET_SAFE_LJMP(s->hlua)) { |
| 10573 | const char *error; |
| 10574 | |
| 10575 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 10576 | error = lua_tostring(s->hlua->T, -1); |
| 10577 | else |
| 10578 | error = "critical error"; |
| 10579 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 10580 | ret = 0; |
| 10581 | goto end; |
| 10582 | } |
| 10583 | |
| 10584 | /* Check stack size. */ |
| 10585 | if (!lua_checkstack(s->hlua->T, 1)) { |
| 10586 | 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] | 10587 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10588 | ret = 0; |
| 10589 | goto end; |
| 10590 | } |
| 10591 | |
| 10592 | lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, conf->ref[s->hlua->state_id]); |
| 10593 | if (lua_getfield(s->hlua->T, -1, "new") != LUA_TFUNCTION) { |
| 10594 | SEND_ERR(s->be, "Lua filter '%s': 'new' field is not a function.\n", |
| 10595 | conf->reg->name); |
| 10596 | RESET_SAFE_LJMP(s->hlua); |
| 10597 | ret = 0; |
| 10598 | goto end; |
| 10599 | } |
| 10600 | lua_insert(s->hlua->T, -2); |
| 10601 | |
| 10602 | /* Push the copy on the stack */ |
| 10603 | s->hlua->nargs = 1; |
| 10604 | |
| 10605 | /* We must initialize the execution timeouts. */ |
| 10606 | s->hlua->max_time = hlua_timeout_session; |
| 10607 | |
| 10608 | /* At this point the execution is safe. */ |
| 10609 | RESET_SAFE_LJMP(s->hlua); |
| 10610 | } |
| 10611 | |
| 10612 | switch (hlua_ctx_resume(s->hlua, 0)) { |
| 10613 | case HLUA_E_OK: |
| 10614 | /* Nothing returned or not a table, ignore the filter for current stream */ |
| 10615 | if (!lua_gettop(s->hlua->T) || !lua_istable(s->hlua->T, 1)) { |
| 10616 | ret = 0; |
| 10617 | goto end; |
| 10618 | } |
| 10619 | |
| 10620 | /* Attached the filter pointer to the ctx */ |
| 10621 | lua_pushstring(s->hlua->T, "__filter"); |
| 10622 | lua_pushlightuserdata(s->hlua->T, filter); |
| 10623 | lua_settable(s->hlua->T, -3); |
| 10624 | |
| 10625 | /* Save a ref on the filter ctx */ |
| 10626 | lua_pushvalue(s->hlua->T, 1); |
| 10627 | flt_ctx->ref = luaL_ref(s->hlua->T, LUA_REGISTRYINDEX); |
| 10628 | filter->ctx = flt_ctx; |
| 10629 | break; |
| 10630 | case HLUA_E_ERRMSG: |
| 10631 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(s->hlua->T, -1)); |
| 10632 | ret = -1; |
| 10633 | goto end; |
| 10634 | case HLUA_E_ETMOUT: |
| 10635 | SEND_ERR(s->be, "Lua filter '%s' : 'new' execution timeout.\n", conf->reg->name); |
| 10636 | ret = 0; |
| 10637 | goto end; |
| 10638 | case HLUA_E_NOMEM: |
| 10639 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10640 | ret = 0; |
| 10641 | goto end; |
| 10642 | case HLUA_E_AGAIN: |
| 10643 | case HLUA_E_YIELD: |
| 10644 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10645 | " are not allowed from 'new' function.\n", conf->reg->name); |
| 10646 | ret = 0; |
| 10647 | goto end; |
| 10648 | case HLUA_E_ERR: |
| 10649 | SEND_ERR(s->be, "Lua filter '%s': 'new' returns an unknown error.\n", conf->reg->name); |
| 10650 | ret = 0; |
| 10651 | goto end; |
| 10652 | default: |
| 10653 | ret = 0; |
| 10654 | goto end; |
| 10655 | } |
| 10656 | |
| 10657 | end: |
| 10658 | if (s->hlua) |
| 10659 | lua_settop(s->hlua->T, 0); |
| 10660 | if (ret <= 0) { |
| 10661 | if (flt_ctx) { |
| 10662 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10663 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10664 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10665 | } |
| 10666 | } |
| 10667 | return ret; |
| 10668 | } |
| 10669 | |
| 10670 | static void hlua_filter_delete(struct stream *s, struct filter *filter) |
| 10671 | { |
| 10672 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10673 | |
| 10674 | luaL_unref(s->hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 10675 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10676 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10677 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10678 | filter->ctx = NULL; |
| 10679 | } |
| 10680 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 10681 | static int hlua_filter_from_payload(struct filter *filter) |
| 10682 | { |
| 10683 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10684 | |
| 10685 | return (flt_ctx && !!(flt_ctx->flags & HLUA_FLT_CTX_FL_PAYLOAD)); |
| 10686 | } |
| 10687 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10688 | static int hlua_filter_callback(struct stream *s, struct filter *filter, const char *fun, |
| 10689 | int dir, unsigned int flags) |
| 10690 | { |
| 10691 | struct hlua *flt_hlua; |
| 10692 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 10693 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10694 | unsigned int hflags = HLUA_TXN_FLT_CTX; |
| 10695 | int ret = 1; |
| 10696 | |
| 10697 | flt_hlua = flt_ctx->hlua[(dir == SMP_OPT_DIR_REQ ? 0 : 1)]; |
| 10698 | if (!flt_hlua) |
| 10699 | goto end; |
| 10700 | |
| 10701 | if (!HLUA_IS_RUNNING(flt_hlua)) { |
| 10702 | int extra_idx = lua_gettop(flt_hlua->T); |
| 10703 | |
| 10704 | /* The following Lua calls can fail. */ |
| 10705 | if (!SET_SAFE_LJMP(flt_hlua)) { |
| 10706 | const char *error; |
| 10707 | |
| 10708 | if (lua_type(flt_hlua->T, -1) == LUA_TSTRING) |
| 10709 | error = lua_tostring(flt_hlua->T, -1); |
| 10710 | else |
| 10711 | error = "critical error"; |
| 10712 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 10713 | goto end; |
| 10714 | } |
| 10715 | |
| 10716 | /* Check stack size. */ |
| 10717 | if (!lua_checkstack(flt_hlua->T, 3)) { |
| 10718 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10719 | RESET_SAFE_LJMP(flt_hlua); |
| 10720 | goto end; |
| 10721 | } |
| 10722 | |
| 10723 | lua_rawgeti(flt_hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 10724 | if (lua_getfield(flt_hlua->T, -1, fun) != LUA_TFUNCTION) { |
| 10725 | RESET_SAFE_LJMP(flt_hlua); |
| 10726 | goto end; |
| 10727 | } |
| 10728 | lua_insert(flt_hlua->T, -2); |
| 10729 | |
| 10730 | if (!hlua_txn_new(flt_hlua->T, s, s->be, dir, hflags)) { |
| 10731 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10732 | RESET_SAFE_LJMP(flt_hlua); |
| 10733 | goto end; |
| 10734 | } |
| 10735 | flt_hlua->nargs = 2; |
| 10736 | |
| 10737 | if (flags & HLUA_FLT_CB_ARG_CHN) { |
| 10738 | if (dir == SMP_OPT_DIR_REQ) |
| 10739 | lua_getfield(flt_hlua->T, -1, "req"); |
| 10740 | else |
| 10741 | lua_getfield(flt_hlua->T, -1, "res"); |
| 10742 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10743 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10744 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10745 | lua_settable(flt_hlua->T, -3); |
| 10746 | } |
| 10747 | flt_hlua->nargs++; |
| 10748 | } |
| 10749 | else if (flags & HLUA_FLT_CB_ARG_HTTP_MSG) { |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10750 | if (dir == SMP_OPT_DIR_REQ) |
| 10751 | lua_getfield(flt_hlua->T, -1, "http_req"); |
| 10752 | else |
| 10753 | lua_getfield(flt_hlua->T, -1, "http_res"); |
| 10754 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10755 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10756 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10757 | lua_settable(flt_hlua->T, -3); |
| 10758 | } |
| 10759 | flt_hlua->nargs++; |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10760 | } |
| 10761 | |
| 10762 | /* Check stack size. */ |
| 10763 | if (!lua_checkstack(flt_hlua->T, 1)) { |
| 10764 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10765 | RESET_SAFE_LJMP(flt_hlua); |
| 10766 | goto end; |
| 10767 | } |
| 10768 | |
| 10769 | while (extra_idx--) { |
| 10770 | lua_pushvalue(flt_hlua->T, 1); |
| 10771 | lua_remove(flt_hlua->T, 1); |
| 10772 | flt_hlua->nargs++; |
| 10773 | } |
| 10774 | |
| 10775 | /* We must initialize the execution timeouts. */ |
| 10776 | flt_hlua->max_time = hlua_timeout_session; |
| 10777 | |
| 10778 | /* At this point the execution is safe. */ |
| 10779 | RESET_SAFE_LJMP(flt_hlua); |
| 10780 | } |
| 10781 | |
| 10782 | switch (hlua_ctx_resume(flt_hlua, !(flags & HLUA_FLT_CB_FINAL))) { |
| 10783 | case HLUA_E_OK: |
| 10784 | /* Catch the return value if it required */ |
| 10785 | if ((flags & HLUA_FLT_CB_RETVAL) && lua_gettop(flt_hlua->T) > 0) { |
| 10786 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10787 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10788 | } |
| 10789 | |
| 10790 | /* Set timeout in the required channel. */ |
| 10791 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10792 | if (dir == SMP_OPT_DIR_REQ) |
| 10793 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10794 | else |
| 10795 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10796 | } |
| 10797 | break; |
| 10798 | case HLUA_E_AGAIN: |
| 10799 | /* Set timeout in the required channel. */ |
| 10800 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10801 | if (dir == SMP_OPT_DIR_REQ) |
| 10802 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10803 | else |
| 10804 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10805 | } |
| 10806 | /* Some actions can be wake up when a "write" event |
| 10807 | * is detected on a response channel. This is useful |
| 10808 | * only for actions targeted on the requests. |
| 10809 | */ |
| 10810 | if (HLUA_IS_WAKERESWR(flt_hlua)) |
| 10811 | s->res.flags |= CF_WAKE_WRITE; |
| 10812 | if (HLUA_IS_WAKEREQWR(flt_hlua)) |
| 10813 | s->req.flags |= CF_WAKE_WRITE; |
| 10814 | ret = 0; |
| 10815 | goto end; |
| 10816 | case HLUA_E_ERRMSG: |
| 10817 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(flt_hlua->T, -1)); |
| 10818 | ret = -1; |
| 10819 | goto end; |
| 10820 | case HLUA_E_ETMOUT: |
| 10821 | SEND_ERR(s->be, "Lua filter '%s' : '%s' callback execution timeout.\n", conf->reg->name, fun); |
| 10822 | goto end; |
| 10823 | case HLUA_E_NOMEM: |
| 10824 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10825 | goto end; |
| 10826 | case HLUA_E_YIELD: |
| 10827 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10828 | " are not allowed from '%s' callback.\n", conf->reg->name, fun); |
| 10829 | goto end; |
| 10830 | case HLUA_E_ERR: |
| 10831 | SEND_ERR(s->be, "Lua filter '%s': '%s' returns an unknown error.\n", conf->reg->name, fun); |
| 10832 | goto end; |
| 10833 | default: |
| 10834 | goto end; |
| 10835 | } |
| 10836 | |
| 10837 | |
| 10838 | end: |
| 10839 | return ret; |
| 10840 | } |
| 10841 | |
| 10842 | static int hlua_filter_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 10843 | { |
| 10844 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10845 | |
| 10846 | flt_ctx->flags = 0; |
| 10847 | return hlua_filter_callback(s, filter, "start_analyze", |
| 10848 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10849 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 10850 | } |
| 10851 | |
| 10852 | static int hlua_filter_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 10853 | { |
| 10854 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10855 | |
| 10856 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10857 | return hlua_filter_callback(s, filter, "end_analyze", |
| 10858 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10859 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 10860 | } |
| 10861 | |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10862 | static int hlua_filter_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 10863 | { |
| 10864 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10865 | |
| 10866 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10867 | return hlua_filter_callback(s, filter, "http_headers", |
| 10868 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10869 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10870 | } |
| 10871 | |
| 10872 | static int hlua_filter_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg, |
| 10873 | unsigned int offset, unsigned int len) |
| 10874 | { |
| 10875 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10876 | struct hlua *flt_hlua; |
| 10877 | int dir = (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 10878 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 10879 | int ret; |
| 10880 | |
| 10881 | flt_hlua = flt_ctx->hlua[idx]; |
| 10882 | flt_ctx->cur_off[idx] = offset; |
| 10883 | flt_ctx->cur_len[idx] = len; |
| 10884 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 10885 | ret = hlua_filter_callback(s, filter, "http_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10886 | if (ret != -1) { |
| 10887 | ret = flt_ctx->cur_len[idx]; |
| 10888 | if (lua_gettop(flt_hlua->T) > 0) { |
| 10889 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10890 | if (ret > flt_ctx->cur_len[idx]) |
| 10891 | ret = flt_ctx->cur_len[idx]; |
| 10892 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10893 | } |
| 10894 | } |
| 10895 | return ret; |
| 10896 | } |
| 10897 | |
| 10898 | static int hlua_filter_http_end(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 10899 | { |
| 10900 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10901 | |
| 10902 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10903 | return hlua_filter_callback(s, filter, "http_end", |
| 10904 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10905 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10906 | } |
| 10907 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10908 | static int hlua_filter_tcp_payload(struct stream *s, struct filter *filter, struct channel *chn, |
| 10909 | unsigned int offset, unsigned int len) |
| 10910 | { |
| 10911 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10912 | struct hlua *flt_hlua; |
| 10913 | int dir = (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 10914 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 10915 | int ret; |
| 10916 | |
| 10917 | flt_hlua = flt_ctx->hlua[idx]; |
| 10918 | flt_ctx->cur_off[idx] = offset; |
| 10919 | flt_ctx->cur_len[idx] = len; |
| 10920 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 10921 | ret = hlua_filter_callback(s, filter, "tcp_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_CHN)); |
| 10922 | if (ret != -1) { |
| 10923 | ret = flt_ctx->cur_len[idx]; |
| 10924 | if (lua_gettop(flt_hlua->T) > 0) { |
| 10925 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10926 | if (ret > flt_ctx->cur_len[idx]) |
| 10927 | ret = flt_ctx->cur_len[idx]; |
| 10928 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10929 | } |
| 10930 | } |
| 10931 | return ret; |
| 10932 | } |
| 10933 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10934 | static int hlua_filter_parse_fct(char **args, int *cur_arg, struct proxy *px, |
| 10935 | struct flt_conf *fconf, char **err, void *private) |
| 10936 | { |
| 10937 | struct hlua_reg_filter *reg_flt = private; |
| 10938 | lua_State *L; |
| 10939 | struct hlua_flt_config *conf = NULL; |
| 10940 | const char *flt_id = NULL; |
| 10941 | int state_id, pos, flt_flags = 0; |
| 10942 | struct flt_ops *hlua_flt_ops = NULL; |
| 10943 | |
| 10944 | state_id = reg_flt_to_stack_id(reg_flt); |
| 10945 | L = hlua_states[state_id]; |
| 10946 | |
| 10947 | /* Initialize the filter ops with default callbacks */ |
| 10948 | hlua_flt_ops = calloc(1, sizeof(*hlua_flt_ops)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10949 | if (!hlua_flt_ops) |
| 10950 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10951 | hlua_flt_ops->init = hlua_filter_init; |
| 10952 | hlua_flt_ops->deinit = hlua_filter_deinit; |
| 10953 | if (state_id) { |
| 10954 | /* Set per-thread callback if script is loaded per-thread */ |
| 10955 | hlua_flt_ops->init_per_thread = hlua_filter_init_per_thread; |
| 10956 | hlua_flt_ops->deinit_per_thread = hlua_filter_deinit_per_thread; |
| 10957 | } |
| 10958 | hlua_flt_ops->attach = hlua_filter_new; |
| 10959 | hlua_flt_ops->detach = hlua_filter_delete; |
| 10960 | |
| 10961 | /* Push the filter class on the stack and resolve all callbacks */ |
| 10962 | lua_rawgeti(L, LUA_REGISTRYINDEX, reg_flt->flt_ref[state_id]); |
| 10963 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10964 | if (lua_getfield(L, -1, "start_analyze") == LUA_TFUNCTION) |
| 10965 | hlua_flt_ops->channel_start_analyze = hlua_filter_start_analyze; |
| 10966 | lua_pop(L, 1); |
| 10967 | if (lua_getfield(L, -1, "end_analyze") == LUA_TFUNCTION) |
| 10968 | hlua_flt_ops->channel_end_analyze = hlua_filter_end_analyze; |
| 10969 | lua_pop(L, 1); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10970 | if (lua_getfield(L, -1, "http_headers") == LUA_TFUNCTION) |
| 10971 | hlua_flt_ops->http_headers = hlua_filter_http_headers; |
| 10972 | lua_pop(L, 1); |
| 10973 | if (lua_getfield(L, -1, "http_payload") == LUA_TFUNCTION) |
| 10974 | hlua_flt_ops->http_payload = hlua_filter_http_payload; |
| 10975 | lua_pop(L, 1); |
| 10976 | if (lua_getfield(L, -1, "http_end") == LUA_TFUNCTION) |
| 10977 | hlua_flt_ops->http_end = hlua_filter_http_end; |
| 10978 | lua_pop(L, 1); |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10979 | if (lua_getfield(L, -1, "tcp_payload") == LUA_TFUNCTION) |
| 10980 | hlua_flt_ops->tcp_payload = hlua_filter_tcp_payload; |
| 10981 | lua_pop(L, 1); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10982 | |
| 10983 | /* Get id and flags of the filter class */ |
| 10984 | if (lua_getfield(L, -1, "id") == LUA_TSTRING) |
| 10985 | flt_id = lua_tostring(L, -1); |
| 10986 | lua_pop(L, 1); |
| 10987 | if (lua_getfield(L, -1, "flags") == LUA_TNUMBER) |
| 10988 | flt_flags = lua_tointeger(L, -1); |
| 10989 | lua_pop(L, 1); |
| 10990 | |
| 10991 | /* Create the filter config */ |
| 10992 | conf = calloc(1, sizeof(*conf)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10993 | if (!conf) |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10994 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10995 | conf->reg = reg_flt; |
| 10996 | |
| 10997 | /* duplicate args */ |
| 10998 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++); |
| 10999 | conf->args = calloc(pos + 1, sizeof(*conf->args)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11000 | if (!conf->args) |
| 11001 | goto error; |
| 11002 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++) { |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11003 | conf->args[pos] = strdup(args[*cur_arg + 1 + pos]); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11004 | if (!conf->args[pos]) |
| 11005 | goto error; |
| 11006 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11007 | conf->args[pos] = NULL; |
| 11008 | *cur_arg += pos + 1; |
| 11009 | |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11010 | if (flt_id) { |
| 11011 | fconf->id = strdup(flt_id); |
| 11012 | if (!fconf->id) |
| 11013 | goto error; |
| 11014 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11015 | fconf->flags = flt_flags; |
| 11016 | fconf->conf = conf; |
| 11017 | fconf->ops = hlua_flt_ops; |
| 11018 | |
| 11019 | lua_settop(L, 0); |
| 11020 | return 0; |
| 11021 | |
| 11022 | error: |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11023 | 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] | 11024 | free(hlua_flt_ops); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11025 | if (conf && conf->args) { |
| 11026 | for (pos = 0; conf->args[pos]; pos++) |
| 11027 | free(conf->args[pos]); |
| 11028 | free(conf->args); |
| 11029 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11030 | free(conf); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11031 | free((char *)fconf->id); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11032 | lua_settop(L, 0); |
| 11033 | return -1; |
| 11034 | } |
| 11035 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11036 | __LJMP static int hlua_register_data_filter(lua_State *L) |
| 11037 | { |
| 11038 | struct filter *filter; |
| 11039 | struct channel *chn; |
| 11040 | |
| 11041 | MAY_LJMP(check_args(L, 2, "register_data_filter")); |
| 11042 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 11043 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 11044 | |
| 11045 | lua_getfield(L, 1, "__filter"); |
| 11046 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 11047 | filter = lua_touserdata (L, -1); |
| 11048 | lua_pop(L, 1); |
| 11049 | |
| 11050 | register_data_filter(chn_strm(chn), chn, filter); |
| 11051 | return 1; |
| 11052 | } |
| 11053 | |
| 11054 | __LJMP static int hlua_unregister_data_filter(lua_State *L) |
| 11055 | { |
| 11056 | struct filter *filter; |
| 11057 | struct channel *chn; |
| 11058 | |
| 11059 | MAY_LJMP(check_args(L, 2, "unregister_data_filter")); |
| 11060 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 11061 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 11062 | |
| 11063 | lua_getfield(L, 1, "__filter"); |
| 11064 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 11065 | filter = lua_touserdata (L, -1); |
| 11066 | lua_pop(L, 1); |
| 11067 | |
| 11068 | unregister_data_filter(chn_strm(chn), chn, filter); |
| 11069 | return 1; |
| 11070 | } |
| 11071 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11072 | /* 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] | 11073 | * 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] | 11074 | * parse configuration arguments. |
| 11075 | */ |
| 11076 | __LJMP static int hlua_register_filter(lua_State *L) |
| 11077 | { |
| 11078 | struct buffer *trash; |
| 11079 | struct flt_kw_list *fkl; |
| 11080 | struct flt_kw *fkw; |
| 11081 | const char *name; |
| 11082 | struct hlua_reg_filter *reg_flt= NULL; |
| 11083 | int flt_ref, fun_ref; |
| 11084 | int len; |
| 11085 | |
| 11086 | MAY_LJMP(check_args(L, 3, "register_filter")); |
| 11087 | |
| 11088 | /* First argument : filter name. */ |
| 11089 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 11090 | |
| 11091 | /* Second argument : The filter class */ |
| 11092 | flt_ref = MAY_LJMP(hlua_checktable(L, 2)); |
| 11093 | |
| 11094 | /* Third argument : lua function. */ |
| 11095 | fun_ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 11096 | |
| 11097 | trash = get_trash_chunk(); |
| 11098 | chunk_printf(trash, "lua.%s", name); |
| 11099 | fkw = flt_find_kw(trash->area); |
| 11100 | if (fkw != NULL) { |
| 11101 | reg_flt = fkw->private; |
| 11102 | if (reg_flt->flt_ref[hlua_state_id] != -1 || reg_flt->fun_ref[hlua_state_id] != -1) { |
| 11103 | ha_warning("Trying to register filter 'lua.%s' more than once. " |
| 11104 | "This will become a hard error in version 2.5.\n", name); |
| 11105 | } |
| 11106 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 11107 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 11108 | return 0; |
| 11109 | } |
| 11110 | |
| 11111 | fkl = calloc(1, sizeof(*fkl) + sizeof(struct flt_kw) * 2); |
| 11112 | if (!fkl) |
| 11113 | goto alloc_error; |
| 11114 | fkl->scope = "HLUA"; |
| 11115 | |
| 11116 | reg_flt = new_hlua_reg_filter(name); |
| 11117 | if (!reg_flt) |
| 11118 | goto alloc_error; |
| 11119 | |
| 11120 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 11121 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 11122 | |
| 11123 | /* The filter keyword */ |
| 11124 | len = strlen("lua.") + strlen(name) + 1; |
| 11125 | fkl->kw[0].kw = calloc(1, len); |
| 11126 | if (!fkl->kw[0].kw) |
| 11127 | goto alloc_error; |
| 11128 | |
| 11129 | snprintf((char *)fkl->kw[0].kw, len, "lua.%s", name); |
| 11130 | |
| 11131 | fkl->kw[0].parse = hlua_filter_parse_fct; |
| 11132 | fkl->kw[0].private = reg_flt; |
| 11133 | memset(&fkl->kw[1], 0, sizeof(*fkl->kw)); |
| 11134 | |
| 11135 | /* Register this new filter */ |
| 11136 | flt_register_keywords(fkl); |
| 11137 | |
| 11138 | return 0; |
| 11139 | |
| 11140 | alloc_error: |
| 11141 | release_hlua_reg_filter(reg_flt); |
| 11142 | ha_free(&fkl); |
| 11143 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 11144 | return 0; /* Never reached */ |
| 11145 | } |
| 11146 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11147 | 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] | 11148 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11149 | char **err, unsigned int *timeout) |
| 11150 | { |
| 11151 | const char *error; |
| 11152 | |
| 11153 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 11154 | if (error == PARSE_TIME_OVER) { |
| 11155 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 11156 | args[1], args[0]); |
| 11157 | return -1; |
| 11158 | } |
| 11159 | else if (error == PARSE_TIME_UNDER) { |
| 11160 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 11161 | args[1], args[0]); |
| 11162 | return -1; |
| 11163 | } |
| 11164 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11165 | memprintf(err, "%s: invalid timeout", args[0]); |
| 11166 | return -1; |
| 11167 | } |
| 11168 | return 0; |
| 11169 | } |
| 11170 | |
| 11171 | 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] | 11172 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11173 | char **err) |
| 11174 | { |
| 11175 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11176 | file, line, err, &hlua_timeout_session); |
| 11177 | } |
| 11178 | |
| 11179 | 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] | 11180 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11181 | char **err) |
| 11182 | { |
| 11183 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11184 | file, line, err, &hlua_timeout_task); |
| 11185 | } |
| 11186 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11187 | 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] | 11188 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11189 | char **err) |
| 11190 | { |
| 11191 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11192 | file, line, err, &hlua_timeout_applet); |
| 11193 | } |
| 11194 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11195 | 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] | 11196 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11197 | char **err) |
| 11198 | { |
| 11199 | char *error; |
| 11200 | |
| 11201 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 11202 | if (*error != '\0') { |
| 11203 | memprintf(err, "%s: invalid number", args[0]); |
| 11204 | return -1; |
| 11205 | } |
| 11206 | return 0; |
| 11207 | } |
| 11208 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11209 | 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] | 11210 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11211 | char **err) |
| 11212 | { |
| 11213 | char *error; |
| 11214 | |
| 11215 | if (*(args[1]) == 0) { |
| 11216 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 11217 | return -1; |
| 11218 | } |
| 11219 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 11220 | if (*error != '\0') { |
| 11221 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 11222 | return -1; |
| 11223 | } |
| 11224 | return 0; |
| 11225 | } |
| 11226 | |
| 11227 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11228 | /* This function is called by the main configuration key "lua-load". It loads and |
| 11229 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 11230 | * the main lua entry point. |
| 11231 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 11232 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 11233 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11234 | * |
| 11235 | * In some error case, LUA set an error message in top of the stack. This function |
| 11236 | * 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] | 11237 | * |
| 11238 | * This function can fail with an abort() due to an Lua critical error. |
| 11239 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 11240 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11241 | */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11242 | static int hlua_load_state(char *filename, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11243 | { |
| 11244 | int error; |
| 11245 | |
| 11246 | /* Just load and compile the file. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11247 | error = luaL_loadfile(L, filename); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11248 | if (error) { |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11249 | memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1)); |
| 11250 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11251 | return -1; |
| 11252 | } |
| 11253 | |
| 11254 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11255 | error = lua_pcall(L, 0, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11256 | switch (error) { |
| 11257 | case LUA_OK: |
| 11258 | break; |
| 11259 | case LUA_ERRRUN: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11260 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1)); |
| 11261 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11262 | return -1; |
| 11263 | case LUA_ERRMEM: |
Thierry Fournier | de6145f | 2020-11-29 00:55:53 +0100 | [diff] [blame] | 11264 | memprintf(err, "Lua out of memory error\n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11265 | return -1; |
| 11266 | case LUA_ERRERR: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11267 | memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1)); |
| 11268 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11269 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 11270 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11271 | case LUA_ERRGCMM: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11272 | memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1)); |
| 11273 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11274 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 11275 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11276 | default: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11277 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1)); |
| 11278 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11279 | return -1; |
| 11280 | } |
| 11281 | |
| 11282 | return 0; |
| 11283 | } |
| 11284 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11285 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 11286 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11287 | char **err) |
| 11288 | { |
| 11289 | if (*(args[1]) == 0) { |
| 11290 | memprintf(err, "'%s' expects a file name as parameter.\n", args[0]); |
| 11291 | return -1; |
| 11292 | } |
| 11293 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11294 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11295 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11296 | ha_set_thread(NULL); |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 11297 | return hlua_load_state(args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11298 | } |
| 11299 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11300 | 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] | 11301 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11302 | char **err) |
| 11303 | { |
| 11304 | int len; |
| 11305 | |
| 11306 | if (*(args[1]) == 0) { |
| 11307 | memprintf(err, "'%s' expects a file as parameter.\n", args[0]); |
| 11308 | return -1; |
| 11309 | } |
| 11310 | |
| 11311 | if (per_thread_load == NULL) { |
| 11312 | /* allocate the first entry large enough to store the final NULL */ |
| 11313 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 11314 | if (per_thread_load == NULL) { |
| 11315 | memprintf(err, "out of memory error"); |
| 11316 | return -1; |
| 11317 | } |
| 11318 | } |
| 11319 | |
| 11320 | /* count used entries */ |
| 11321 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 11322 | ; |
| 11323 | |
| 11324 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 11325 | if (per_thread_load == NULL) { |
| 11326 | memprintf(err, "out of memory error"); |
| 11327 | return -1; |
| 11328 | } |
| 11329 | |
| 11330 | per_thread_load[len] = strdup(args[1]); |
| 11331 | per_thread_load[len + 1] = NULL; |
| 11332 | |
| 11333 | if (per_thread_load[len] == NULL) { |
| 11334 | memprintf(err, "out of memory error"); |
| 11335 | return -1; |
| 11336 | } |
| 11337 | |
| 11338 | /* loading for thread 1 only */ |
| 11339 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11340 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11341 | return hlua_load_state(args[1], hlua_states[1], err); |
| 11342 | } |
| 11343 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11344 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 11345 | * in the given <ctx>. |
| 11346 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 11347 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11348 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 11349 | lua_getglobal(L, "package"); /* push package variable */ |
| 11350 | lua_pushstring(L, path); /* push given path */ |
| 11351 | lua_pushstring(L, ";"); /* push semicolon */ |
| 11352 | lua_getfield(L, -3, type); /* push old path */ |
| 11353 | lua_concat(L, 3); /* concatenate to new path */ |
| 11354 | lua_setfield(L, -2, type); /* store new path */ |
| 11355 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11356 | |
| 11357 | return 0; |
| 11358 | } |
| 11359 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11360 | 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] | 11361 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11362 | char **err) |
| 11363 | { |
| 11364 | char *path; |
| 11365 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11366 | struct prepend_path *p = NULL; |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 11367 | size_t i; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11368 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11369 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11370 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11371 | } |
| 11372 | |
| 11373 | if (!(*args[1])) { |
| 11374 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11375 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11376 | } |
| 11377 | path = args[1]; |
| 11378 | |
| 11379 | if (*args[2]) { |
| 11380 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 11381 | 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] | 11382 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11383 | } |
| 11384 | type = args[2]; |
| 11385 | } |
| 11386 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11387 | p = calloc(1, sizeof(*p)); |
| 11388 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11389 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11390 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11391 | } |
| 11392 | p->path = strdup(path); |
| 11393 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11394 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11395 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11396 | } |
| 11397 | p->type = strdup(type); |
| 11398 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11399 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11400 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11401 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 11402 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11403 | |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 11404 | /* Handle the global state and the per-thread state for the first |
| 11405 | * thread. The remaining threads will be initialized based on |
| 11406 | * prepend_path_list. |
| 11407 | */ |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 11408 | for (i = 0; i < 2; i++) { |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 11409 | lua_State *L = hlua_states[i]; |
| 11410 | const char *error; |
| 11411 | |
| 11412 | if (setjmp(safe_ljmp_env) != 0) { |
| 11413 | lua_atpanic(L, hlua_panic_safe); |
| 11414 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11415 | error = lua_tostring(L, -1); |
| 11416 | else |
| 11417 | error = "critical error"; |
| 11418 | fprintf(stderr, "lua-prepend-path: %s.\n", error); |
| 11419 | exit(1); |
| 11420 | } else { |
| 11421 | lua_atpanic(L, hlua_panic_ljmp); |
| 11422 | } |
| 11423 | |
| 11424 | hlua_prepend_path(L, type, path); |
| 11425 | |
| 11426 | lua_atpanic(L, hlua_panic_safe); |
| 11427 | } |
| 11428 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11429 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11430 | |
| 11431 | err2: |
| 11432 | free(p->type); |
| 11433 | free(p->path); |
| 11434 | err: |
| 11435 | free(p); |
| 11436 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11437 | } |
| 11438 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11439 | /* configuration keywords declaration */ |
| 11440 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11441 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11442 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11443 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11444 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 11445 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 11446 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11447 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11448 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11449 | { 0, NULL, NULL }, |
| 11450 | }}; |
| 11451 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 11452 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 11453 | |
William Lallemand | 5213918 | 2022-03-30 15:05:42 +0200 | [diff] [blame] | 11454 | #ifdef USE_OPENSSL |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 11455 | |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11456 | /* |
| 11457 | * This function replace a ckch_store by another one, and rebuild the ckch_inst and all its dependencies. |
| 11458 | * It does the sam as "cli_io_handler_commit_cert" but for lua, the major |
| 11459 | * difference is that the yield in lua and for the CLI is not handled the same |
| 11460 | * way. |
| 11461 | */ |
| 11462 | __LJMP static int hlua_ckch_commit_yield(lua_State *L, int status, lua_KContext ctx) |
| 11463 | { |
| 11464 | struct ckch_inst **lua_ckchi = lua_touserdata(L, -1); |
| 11465 | struct ckch_store **lua_ckchs = lua_touserdata(L, -2); |
| 11466 | struct ckch_inst *ckchi = *lua_ckchi; |
| 11467 | struct ckch_store *old_ckchs = lua_ckchs[0]; |
| 11468 | struct ckch_store *new_ckchs = lua_ckchs[1]; |
| 11469 | struct hlua *hlua; |
| 11470 | char *err = NULL; |
| 11471 | int y = 1; |
| 11472 | |
| 11473 | hlua = hlua_gethlua(L); |
| 11474 | |
| 11475 | /* get the first ckchi to copy */ |
| 11476 | if (ckchi == NULL) |
| 11477 | ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs); |
| 11478 | |
| 11479 | /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */ |
| 11480 | list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) { |
| 11481 | struct ckch_inst *new_inst; |
| 11482 | |
| 11483 | /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */ |
| 11484 | if (y % 10 == 0) { |
| 11485 | |
| 11486 | *lua_ckchi = ckchi; |
| 11487 | |
| 11488 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 11489 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_ckch_commit_yield, TICK_ETERNITY, 0)); |
| 11490 | } |
| 11491 | |
| 11492 | if (ckch_inst_rebuild(new_ckchs, ckchi, &new_inst, &err)) |
| 11493 | goto error; |
| 11494 | |
| 11495 | /* link the new ckch_inst to the duplicate */ |
| 11496 | LIST_APPEND(&new_ckchs->ckch_inst, &new_inst->by_ckchs); |
| 11497 | y++; |
| 11498 | } |
| 11499 | |
| 11500 | /* The generation is finished, we can insert everything */ |
| 11501 | ckch_store_replace(old_ckchs, new_ckchs); |
| 11502 | |
| 11503 | lua_pop(L, 2); /* pop the lua_ckchs and ckchi */ |
| 11504 | |
| 11505 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11506 | |
| 11507 | return 0; |
| 11508 | |
| 11509 | error: |
| 11510 | ckch_store_free(new_ckchs); |
| 11511 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11512 | WILL_LJMP(luaL_error(L, "%s", err)); |
| 11513 | free(err); |
| 11514 | |
| 11515 | return 0; |
| 11516 | } |
| 11517 | |
| 11518 | /* |
| 11519 | * Replace a ckch_store <filename> in the ckchs_tree with a ckch_store created |
| 11520 | * from the table in parameter. |
| 11521 | * |
| 11522 | * This is equivalent to "set ssl cert" + "commit ssl cert" over the CLI, which |
| 11523 | * means it does not need to have a transaction since everything is done in the |
| 11524 | * same function. |
| 11525 | * |
| 11526 | * CertCache.set{filename="", crt="", key="", sctl="", ocsp="", issuer=""} |
| 11527 | * |
| 11528 | */ |
| 11529 | __LJMP static int hlua_ckch_set(lua_State *L) |
| 11530 | { |
| 11531 | struct hlua *hlua; |
| 11532 | struct ckch_inst **lua_ckchi; |
| 11533 | struct ckch_store **lua_ckchs; |
| 11534 | struct ckch_store *old_ckchs = NULL; |
| 11535 | struct ckch_store *new_ckchs = NULL; |
| 11536 | int errcode = 0; |
| 11537 | char *err = NULL; |
| 11538 | struct cert_exts *cert_ext = NULL; |
| 11539 | char *filename; |
| 11540 | struct cert_key_and_chain *ckch; |
| 11541 | int ret; |
| 11542 | |
| 11543 | if (lua_type(L, -1) != LUA_TTABLE) |
| 11544 | WILL_LJMP(luaL_error(L, "'CertCache.set' needs a table as argument")); |
| 11545 | |
| 11546 | hlua = hlua_gethlua(L); |
| 11547 | |
| 11548 | /* FIXME: this should not return an error but should come back later */ |
| 11549 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 11550 | WILL_LJMP(luaL_error(L, "CertCache already under lock")); |
| 11551 | |
| 11552 | ret = lua_getfield(L, -1, "filename"); |
| 11553 | if (ret != LUA_TSTRING) { |
| 11554 | memprintf(&err, "%sNo filename specified!\n", err ? err : ""); |
| 11555 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11556 | goto end; |
| 11557 | } |
| 11558 | filename = (char *)lua_tostring(L, -1); |
| 11559 | |
| 11560 | |
| 11561 | /* look for the filename in the tree */ |
| 11562 | old_ckchs = ckchs_lookup(filename); |
| 11563 | if (!old_ckchs) { |
| 11564 | memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n", err ? err : ""); |
| 11565 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11566 | goto end; |
| 11567 | } |
| 11568 | /* TODO: handle extra_files_noext */ |
| 11569 | |
| 11570 | new_ckchs = ckchs_dup(old_ckchs); |
| 11571 | if (!new_ckchs) { |
| 11572 | memprintf(&err, "%sCannot allocate memory!\n", err ? err : ""); |
| 11573 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11574 | goto end; |
| 11575 | } |
| 11576 | |
| 11577 | ckch = new_ckchs->ckch; |
| 11578 | |
| 11579 | /* loop on the field in the table, which have the same name as the |
| 11580 | * possible extensions of files */ |
| 11581 | lua_pushnil(L); |
| 11582 | while (lua_next(L, 1)) { |
| 11583 | int i; |
| 11584 | const char *field = lua_tostring(L, -2); |
| 11585 | char *payload = (char *)lua_tostring(L, -1); |
| 11586 | |
| 11587 | if (!field || strcmp(field, "filename") == 0) { |
| 11588 | lua_pop(L, 1); |
| 11589 | continue; |
| 11590 | } |
| 11591 | |
| 11592 | for (i = 0; field && cert_exts[i].ext != NULL; i++) { |
| 11593 | if (strcmp(field, cert_exts[i].ext) == 0) { |
| 11594 | cert_ext = &cert_exts[i]; |
| 11595 | break; |
| 11596 | } |
| 11597 | } |
| 11598 | |
| 11599 | /* this is the default type, the field is not supported */ |
| 11600 | if (cert_ext == NULL) { |
| 11601 | memprintf(&err, "%sUnsupported field '%s'\n", err ? err : "", field); |
| 11602 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11603 | goto end; |
| 11604 | } |
| 11605 | |
| 11606 | /* appply the change on the duplicate */ |
William Lallemand | d8c195a | 2022-05-26 11:20:13 +0200 | [diff] [blame] | 11607 | if (cert_ext->load(filename, payload, ckch, &err) != 0) { |
| 11608 | memprintf(&err, "%sCan't load the payload for '%s'\n", err ? err : "", cert_ext->ext); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11609 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11610 | goto end; |
| 11611 | } |
| 11612 | lua_pop(L, 1); |
| 11613 | } |
| 11614 | |
| 11615 | /* store the pointers on the lua stack */ |
| 11616 | lua_ckchs = lua_newuserdata(L, sizeof(struct ckch_store *) * 2); |
| 11617 | lua_ckchs[0] = old_ckchs; |
| 11618 | lua_ckchs[1] = new_ckchs; |
| 11619 | lua_ckchi = lua_newuserdata(L, sizeof(struct ckch_inst *)); |
| 11620 | *lua_ckchi = NULL; |
| 11621 | |
| 11622 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 11623 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_ckch_commit_yield, TICK_ETERNITY, 0)); |
| 11624 | |
| 11625 | end: |
| 11626 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11627 | |
| 11628 | if (errcode & ERR_CODE) { |
| 11629 | ckch_store_free(new_ckchs); |
| 11630 | WILL_LJMP(luaL_error(L, "%s", err)); |
| 11631 | } |
| 11632 | free(err); |
| 11633 | |
| 11634 | return 0; |
| 11635 | } |
| 11636 | |
William Lallemand | 5213918 | 2022-03-30 15:05:42 +0200 | [diff] [blame] | 11637 | #else |
| 11638 | |
| 11639 | __LJMP static int hlua_ckch_set(lua_State *L) |
| 11640 | { |
| 11641 | WILL_LJMP(luaL_error(L, "'CertCache.set' needs an HAProxy built with OpenSSL")); |
| 11642 | |
| 11643 | return 0; |
| 11644 | } |
| 11645 | #endif /* ! USE_OPENSSL */ |
| 11646 | |
| 11647 | |
| 11648 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 11649 | /* This function can fail with an abort() due to an Lua critical error. |
| 11650 | * We are in the initialisation process of HAProxy, this abort() is |
| 11651 | * tolerated. |
| 11652 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11653 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11654 | { |
| 11655 | struct hlua_init_function *init; |
| 11656 | const char *msg; |
| 11657 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 11658 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11659 | const char *kind; |
| 11660 | const char *trace; |
| 11661 | int return_status = 1; |
| 11662 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 11663 | int nres; |
| 11664 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11665 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11666 | /* disable memory limit checks if limit is not set */ |
| 11667 | if (!hlua_global_allocator.limit) |
| 11668 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 11669 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 11670 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11671 | if (setjmp(safe_ljmp_env) != 0) { |
| 11672 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11673 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11674 | error = lua_tostring(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11675 | else |
| 11676 | error = "critical error"; |
| 11677 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 11678 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11679 | } else { |
| 11680 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11681 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 11682 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11683 | hlua_fcn_post_init(L); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11684 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11685 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11686 | lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11687 | |
| 11688 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11689 | ret = lua_resume(L, L, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11690 | #else |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11691 | ret = lua_resume(L, L, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11692 | #endif |
| 11693 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11694 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11695 | |
| 11696 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11697 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 11698 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11699 | |
| 11700 | case LUA_ERRERR: |
| 11701 | kind = "message handler error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11702 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11703 | case LUA_ERRRUN: |
| 11704 | if (!kind) |
| 11705 | kind = "runtime error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11706 | msg = lua_tostring(L, -1); |
| 11707 | lua_settop(L, 0); /* Empty the stack. */ |
| 11708 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 11709 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11710 | if (msg) |
| 11711 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 11712 | else |
| 11713 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
| 11714 | return_status = 0; |
| 11715 | break; |
| 11716 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11717 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11718 | /* Unknown error */ |
| 11719 | kind = "Unknown error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11720 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11721 | case LUA_YIELD: |
| 11722 | /* yield is not configured at this step, this state doesn't happen */ |
| 11723 | if (!kind) |
| 11724 | kind = "yield not allowed"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11725 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11726 | case LUA_ERRMEM: |
| 11727 | if (!kind) |
| 11728 | kind = "out of memory error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11729 | lua_settop(L, 0); |
| 11730 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 11731 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11732 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 11733 | return_status = 0; |
| 11734 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11735 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11736 | if (!return_status) |
| 11737 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11738 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11739 | |
| 11740 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11741 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11742 | } |
| 11743 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11744 | int hlua_post_init() |
| 11745 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11746 | int ret; |
| 11747 | int i; |
| 11748 | int errors; |
| 11749 | char *err = NULL; |
| 11750 | struct hlua_function *fcn; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11751 | struct hlua_reg_filter *reg_flt; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11752 | |
Willy Tarreau | b131049 | 2021-08-30 09:35:18 +0200 | [diff] [blame] | 11753 | #if defined(USE_OPENSSL) |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11754 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11755 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11756 | int saved_used_backed = global.ssl_used_backend; |
| 11757 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11758 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11759 | global.ssl_used_backend = saved_used_backed; |
| 11760 | } |
| 11761 | #endif |
| 11762 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11763 | /* Perform post init of common thread */ |
| 11764 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11765 | ha_set_thread(&ha_thread_info[0]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11766 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 11767 | if (ret == 0) |
| 11768 | return 0; |
| 11769 | |
| 11770 | /* init remaining lua states and load files */ |
| 11771 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 11772 | |
| 11773 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11774 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11775 | |
| 11776 | /* Init lua state */ |
| 11777 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 11778 | |
| 11779 | /* Load lua files */ |
| 11780 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 11781 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 11782 | if (ret != 0) { |
| 11783 | ha_alert("Lua init: %s\n", err); |
| 11784 | return 0; |
| 11785 | } |
| 11786 | } |
| 11787 | } |
| 11788 | |
| 11789 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11790 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11791 | |
| 11792 | /* Execute post init for all states */ |
| 11793 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 11794 | |
| 11795 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11796 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11797 | |
| 11798 | /* run post init */ |
| 11799 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 11800 | if (ret == 0) |
| 11801 | return 0; |
| 11802 | } |
| 11803 | |
| 11804 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11805 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11806 | |
| 11807 | /* control functions registering. Each function must have: |
| 11808 | * - only the function_ref[0] set positive and all other to -1 |
| 11809 | * - only the function_ref[0] set to -1 and all other positive |
| 11810 | * This ensure a same reference is not used both in shared |
| 11811 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11812 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11813 | * complicated to found for the end user. |
| 11814 | */ |
| 11815 | errors = 0; |
| 11816 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 11817 | ret = 0; |
| 11818 | for (i = 1; i < global.nbthread + 1; i++) { |
| 11819 | if (fcn->function_ref[i] == -1) |
| 11820 | ret--; |
| 11821 | else |
| 11822 | ret++; |
| 11823 | } |
| 11824 | if (abs(ret) != global.nbthread) { |
| 11825 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 11826 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 11827 | errors++; |
| 11828 | continue; |
| 11829 | } |
| 11830 | |
| 11831 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11832 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 11833 | "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] | 11834 | "exclusive.\n", fcn->name); |
| 11835 | errors++; |
| 11836 | } |
| 11837 | } |
| 11838 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11839 | /* Do the same with registered filters */ |
| 11840 | list_for_each_entry(reg_flt, &referenced_filters, l) { |
| 11841 | ret = 0; |
| 11842 | for (i = 1; i < global.nbthread + 1; i++) { |
| 11843 | if (reg_flt->flt_ref[i] == -1) |
| 11844 | ret--; |
| 11845 | else |
| 11846 | ret++; |
| 11847 | } |
| 11848 | if (abs(ret) != global.nbthread) { |
| 11849 | ha_alert("Lua filter '%s' is not referenced in all thread. " |
| 11850 | "Expect function in all thread or in none thread.\n", reg_flt->name); |
| 11851 | errors++; |
| 11852 | continue; |
| 11853 | } |
| 11854 | |
| 11855 | if ((reg_flt->flt_ref[0] == -1) == (ret < 0)) { |
| 11856 | ha_alert("Lua filter '%s' is referenced both ins shared Lua context (through lua-load) " |
| 11857 | "and per-thread Lua context (through lua-load-per-thread). these two context " |
| 11858 | "exclusive.\n", fcn->name); |
| 11859 | errors++; |
| 11860 | } |
| 11861 | } |
| 11862 | |
| 11863 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11864 | if (errors > 0) |
| 11865 | return 0; |
| 11866 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11867 | /* 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] | 11868 | * -1 in order to have probably a segfault if someone use it |
| 11869 | */ |
| 11870 | hlua_state_id = -1; |
| 11871 | |
| 11872 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11873 | } |
| 11874 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11875 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 11876 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 11877 | * 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] | 11878 | * allocation. <nsize> is the requested new size. A new allocation is |
| 11879 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11880 | * zero. This one verifies that the limits are respected but is optimized |
| 11881 | * 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] | 11882 | * |
| 11883 | * Warning: while this API ressembles glibc's realloc() a lot, glibc surpasses |
| 11884 | * POSIX by making realloc(ptr,0) an effective free(), but others do not do |
| 11885 | * that and will simply allocate zero as if it were the result of malloc(0), |
| 11886 | * so mapping this onto realloc() will lead to memory leaks on non-glibc |
| 11887 | * systems. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11888 | */ |
| 11889 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 11890 | { |
| 11891 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11892 | size_t limit, old, new; |
| 11893 | |
| 11894 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 11895 | * for accounting anymore. |
| 11896 | */ |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 11897 | if (likely(~zone->limit == 0)) { |
| 11898 | if (!nsize) |
| 11899 | ha_free(&ptr); |
| 11900 | else |
| 11901 | ptr = realloc(ptr, nsize); |
| 11902 | return ptr; |
| 11903 | } |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11904 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 11905 | if (!ptr) |
| 11906 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11907 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11908 | /* enforce strict limits across all threads */ |
| 11909 | limit = zone->limit; |
| 11910 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 11911 | do { |
| 11912 | new = old + nsize - osize; |
| 11913 | if (unlikely(nsize && limit && new > limit)) |
| 11914 | return NULL; |
| 11915 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11916 | |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 11917 | if (!nsize) |
| 11918 | ha_free(&ptr); |
| 11919 | else |
| 11920 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11921 | |
| 11922 | if (unlikely(!ptr && nsize)) // failed |
| 11923 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 11924 | |
| 11925 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11926 | return ptr; |
| 11927 | } |
| 11928 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11929 | /* 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] | 11930 | * We are in the initialisation process of HAProxy, this abort() is |
| 11931 | * tolerated. |
| 11932 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11933 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 11934 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11935 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11936 | int idx; |
| 11937 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11938 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11939 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 11940 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11941 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11942 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11943 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11944 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11945 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11946 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11947 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11948 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11949 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11950 | *context = NULL; |
| 11951 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 11952 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 11953 | * the Lua function can fail with an abort. We are in the initialisation |
| 11954 | * process of HAProxy, this abort() is tolerated. |
| 11955 | */ |
| 11956 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11957 | /* Call post initialisation function in safe environment. */ |
| 11958 | if (setjmp(safe_ljmp_env) != 0) { |
| 11959 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11960 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11961 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 11962 | else |
| 11963 | error_msg = "critical error"; |
| 11964 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 11965 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11966 | } else { |
| 11967 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 11968 | } |
| 11969 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11970 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11971 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 11972 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 11973 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 11974 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11975 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 11976 | #endif |
| 11977 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11978 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 11979 | #endif |
| 11980 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 11981 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11982 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11983 | /* Apply configured prepend path */ |
| 11984 | list_for_each_entry(pp, &prepend_path_list, l) |
| 11985 | hlua_prepend_path(L, pp->type, pp->path); |
| 11986 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11987 | /* |
| 11988 | * |
| 11989 | * Create "core" object. |
| 11990 | * |
| 11991 | */ |
| 11992 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 11993 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11994 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11995 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11996 | /* set the thread id */ |
| 11997 | hlua_class_const_int(L, "thread", thread_num); |
| 11998 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11999 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 12000 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12001 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12002 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12003 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12004 | hlua_class_function(L, "register_init", hlua_register_init); |
| 12005 | hlua_class_function(L, "register_task", hlua_register_task); |
| 12006 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 12007 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 12008 | hlua_class_function(L, "register_action", hlua_register_action); |
| 12009 | hlua_class_function(L, "register_service", hlua_register_service); |
| 12010 | hlua_class_function(L, "register_cli", hlua_register_cli); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12011 | hlua_class_function(L, "register_filter", hlua_register_filter); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12012 | hlua_class_function(L, "yield", hlua_yield); |
| 12013 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 12014 | hlua_class_function(L, "sleep", hlua_sleep); |
| 12015 | hlua_class_function(L, "msleep", hlua_msleep); |
| 12016 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 12017 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 12018 | hlua_class_function(L, "set_map", hlua_set_map); |
| 12019 | hlua_class_function(L, "del_map", hlua_del_map); |
| 12020 | hlua_class_function(L, "tcp", hlua_socket_new); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12021 | hlua_class_function(L, "httpclient", hlua_httpclient_new); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12022 | hlua_class_function(L, "log", hlua_log); |
| 12023 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 12024 | hlua_class_function(L, "Info", hlua_log_info); |
| 12025 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 12026 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 12027 | hlua_class_function(L, "done", hlua_done); |
| 12028 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12029 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12030 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12031 | |
| 12032 | /* |
| 12033 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12034 | * Create "act" object. |
| 12035 | * |
| 12036 | */ |
| 12037 | |
| 12038 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12039 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12040 | |
| 12041 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12042 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 12043 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 12044 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 12045 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 12046 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 12047 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 12048 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 12049 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12050 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12051 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 12052 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12053 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12054 | |
| 12055 | /* |
| 12056 | * |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12057 | * Create "Filter" object. |
| 12058 | * |
| 12059 | */ |
| 12060 | |
| 12061 | /* This table entry is the object "filter" base. */ |
| 12062 | lua_newtable(L); |
| 12063 | |
| 12064 | /* push flags and constants */ |
| 12065 | hlua_class_const_int(L, "CONTINUE", 1); |
| 12066 | hlua_class_const_int(L, "WAIT", 0); |
| 12067 | hlua_class_const_int(L, "ERROR", -1); |
| 12068 | |
| 12069 | hlua_class_const_int(L, "FLT_CFG_FL_HTX", FLT_CFG_FL_HTX); |
| 12070 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 12071 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
| 12072 | hlua_class_function(L, "register_data_filter", hlua_register_data_filter); |
| 12073 | hlua_class_function(L, "unregister_data_filter", hlua_unregister_data_filter); |
| 12074 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12075 | lua_setglobal(L, "filter"); |
| 12076 | |
| 12077 | /* |
| 12078 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12079 | * Register class Map |
| 12080 | * |
| 12081 | */ |
| 12082 | |
| 12083 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12084 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12085 | |
| 12086 | /* register pattern types. */ |
| 12087 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12088 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 12089 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12090 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12091 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 12092 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12093 | |
| 12094 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12095 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 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 | 3def393 | 2015-04-07 11:27:54 +0200 | [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 | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12103 | |
| 12104 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12105 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 12106 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12107 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12108 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12109 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 12110 | /* Register previous table in the registry with reference and named entry. |
| 12111 | * The function hlua_register_metatable() pops the stack, so we |
| 12112 | * previously create a copy of the table. |
| 12113 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12114 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 12115 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12116 | |
| 12117 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12118 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12119 | |
| 12120 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12121 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12122 | |
| 12123 | /* |
| 12124 | * |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12125 | * Register "CertCache" class |
| 12126 | * |
| 12127 | */ |
| 12128 | |
| 12129 | /* Create and fill the metatable. */ |
| 12130 | lua_newtable(L); |
| 12131 | /* Register */ |
| 12132 | hlua_class_function(L, "set", hlua_ckch_set); |
| 12133 | lua_setglobal(L, CLASS_CERTCACHE); /* Create global object called Regex */ |
| 12134 | |
| 12135 | /* |
| 12136 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12137 | * Register class Channel |
| 12138 | * |
| 12139 | */ |
| 12140 | |
| 12141 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12142 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12143 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12144 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12145 | lua_pushstring(L, "__index"); |
| 12146 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12147 | |
| 12148 | /* Register . */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12149 | hlua_class_function(L, "data", hlua_channel_get_data); |
| 12150 | hlua_class_function(L, "line", hlua_channel_get_line); |
| 12151 | hlua_class_function(L, "set", hlua_channel_set_data); |
| 12152 | hlua_class_function(L, "remove", hlua_channel_del_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12153 | hlua_class_function(L, "append", hlua_channel_append); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12154 | hlua_class_function(L, "prepend", hlua_channel_prepend); |
| 12155 | hlua_class_function(L, "insert", hlua_channel_insert_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12156 | hlua_class_function(L, "send", hlua_channel_send); |
| 12157 | hlua_class_function(L, "forward", hlua_channel_forward); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12158 | hlua_class_function(L, "input", hlua_channel_get_in_len); |
| 12159 | hlua_class_function(L, "output", hlua_channel_get_out_len); |
| 12160 | hlua_class_function(L, "may_recv", hlua_channel_may_recv); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12161 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 12162 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12163 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12164 | /* Deprecated API */ |
| 12165 | hlua_class_function(L, "get", hlua_channel_get); |
| 12166 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 12167 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 12168 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 12169 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 12170 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12171 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12172 | |
| 12173 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12174 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12175 | |
| 12176 | /* |
| 12177 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12178 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12179 | * |
| 12180 | */ |
| 12181 | |
| 12182 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12183 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12184 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12185 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12186 | lua_pushstring(L, "__index"); |
| 12187 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12188 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12189 | /* Browse existing fetches and create the associated |
| 12190 | * object method. |
| 12191 | */ |
| 12192 | sf = NULL; |
| 12193 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12194 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 12195 | * by an underscore. |
| 12196 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 12197 | strlcpy2(trash.area, sf->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12198 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12199 | if (*p == '.' || *p == '-' || *p == '+') |
| 12200 | *p = '_'; |
| 12201 | |
| 12202 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12203 | lua_pushstring(L, trash.area); |
| 12204 | lua_pushlightuserdata(L, sf); |
| 12205 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 12206 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12207 | } |
| 12208 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12209 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12210 | |
| 12211 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12212 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12213 | |
| 12214 | /* |
| 12215 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12216 | * Register class Converters |
| 12217 | * |
| 12218 | */ |
| 12219 | |
| 12220 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12221 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12222 | |
| 12223 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12224 | lua_pushstring(L, "__index"); |
| 12225 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12226 | |
| 12227 | /* Browse existing converters and create the associated |
| 12228 | * object method. |
| 12229 | */ |
| 12230 | sc = NULL; |
| 12231 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12232 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 12233 | * by an underscore. |
| 12234 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 12235 | strlcpy2(trash.area, sc->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12236 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12237 | if (*p == '.' || *p == '-' || *p == '+') |
| 12238 | *p = '_'; |
| 12239 | |
| 12240 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12241 | lua_pushstring(L, trash.area); |
| 12242 | lua_pushlightuserdata(L, sc); |
| 12243 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 12244 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12245 | } |
| 12246 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12247 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12248 | |
| 12249 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12250 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12251 | |
| 12252 | /* |
| 12253 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12254 | * Register class HTTP |
| 12255 | * |
| 12256 | */ |
| 12257 | |
| 12258 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12259 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12260 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12261 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12262 | lua_pushstring(L, "__index"); |
| 12263 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12264 | |
| 12265 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12266 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 12267 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 12268 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 12269 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 12270 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 12271 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 12272 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 12273 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 12274 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 12275 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12276 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12277 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 12278 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 12279 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 12280 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 12281 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 12282 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 12283 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12284 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12285 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12286 | |
| 12287 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12288 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12289 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 12290 | /* |
| 12291 | * |
| 12292 | * Register class HTTPMessage |
| 12293 | * |
| 12294 | */ |
| 12295 | |
| 12296 | /* Create and fill the metatable. */ |
| 12297 | lua_newtable(L); |
| 12298 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 12299 | /* Create and fill the __index entry. */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 12300 | lua_pushstring(L, "__index"); |
| 12301 | lua_newtable(L); |
| 12302 | |
| 12303 | /* Register Lua functions. */ |
| 12304 | hlua_class_function(L, "is_resp", hlua_http_msg_is_resp); |
| 12305 | hlua_class_function(L, "get_stline", hlua_http_msg_get_stline); |
| 12306 | hlua_class_function(L, "get_headers", hlua_http_msg_get_headers); |
| 12307 | hlua_class_function(L, "del_header", hlua_http_msg_del_hdr); |
| 12308 | hlua_class_function(L, "rep_header", hlua_http_msg_rep_hdr); |
| 12309 | hlua_class_function(L, "rep_value", hlua_http_msg_rep_val); |
| 12310 | hlua_class_function(L, "add_header", hlua_http_msg_add_hdr); |
| 12311 | hlua_class_function(L, "set_header", hlua_http_msg_set_hdr); |
| 12312 | hlua_class_function(L, "set_method", hlua_http_msg_set_meth); |
| 12313 | hlua_class_function(L, "set_path", hlua_http_msg_set_path); |
| 12314 | hlua_class_function(L, "set_query", hlua_http_msg_set_query); |
| 12315 | hlua_class_function(L, "set_uri", hlua_http_msg_set_uri); |
| 12316 | hlua_class_function(L, "set_status", hlua_http_msg_set_status); |
| 12317 | hlua_class_function(L, "is_full", hlua_http_msg_is_full); |
| 12318 | hlua_class_function(L, "may_recv", hlua_http_msg_may_recv); |
| 12319 | hlua_class_function(L, "eom", hlua_http_msg_is_eom); |
| 12320 | hlua_class_function(L, "input", hlua_http_msg_get_in_len); |
| 12321 | hlua_class_function(L, "output", hlua_http_msg_get_out_len); |
| 12322 | |
| 12323 | hlua_class_function(L, "body", hlua_http_msg_get_body); |
| 12324 | hlua_class_function(L, "set", hlua_http_msg_set_data); |
| 12325 | hlua_class_function(L, "remove", hlua_http_msg_del_data); |
| 12326 | hlua_class_function(L, "append", hlua_http_msg_append); |
| 12327 | hlua_class_function(L, "prepend", hlua_http_msg_prepend); |
| 12328 | hlua_class_function(L, "insert", hlua_http_msg_insert_data); |
| 12329 | hlua_class_function(L, "set_eom", hlua_http_msg_set_eom); |
| 12330 | hlua_class_function(L, "unset_eom", hlua_http_msg_unset_eom); |
| 12331 | |
| 12332 | hlua_class_function(L, "send", hlua_http_msg_send); |
| 12333 | hlua_class_function(L, "forward", hlua_http_msg_forward); |
| 12334 | |
| 12335 | lua_rawset(L, -3); |
| 12336 | |
| 12337 | /* Register previous table in the registry with reference and named entry. */ |
| 12338 | class_http_msg_ref = hlua_register_metatable(L, CLASS_HTTP_MSG); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12339 | |
| 12340 | /* |
| 12341 | * |
| 12342 | * Register class HTTPClient |
| 12343 | * |
| 12344 | */ |
| 12345 | |
| 12346 | /* Create and fill the metatable. */ |
| 12347 | lua_newtable(L); |
| 12348 | lua_pushstring(L, "__index"); |
| 12349 | lua_newtable(L); |
| 12350 | hlua_class_function(L, "get", hlua_httpclient_get); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 12351 | hlua_class_function(L, "head", hlua_httpclient_head); |
| 12352 | hlua_class_function(L, "put", hlua_httpclient_put); |
| 12353 | hlua_class_function(L, "post", hlua_httpclient_post); |
| 12354 | hlua_class_function(L, "delete", hlua_httpclient_delete); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12355 | lua_settable(L, -3); /* Sets the __index entry. */ |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 12356 | /* Register the garbage collector entry. */ |
| 12357 | lua_pushstring(L, "__gc"); |
| 12358 | lua_pushcclosure(L, hlua_httpclient_gc, 0); |
| 12359 | lua_settable(L, -3); /* Push the last 2 entries in the table at index -3 */ |
| 12360 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12361 | |
| 12362 | |
| 12363 | class_httpclient_ref = hlua_register_metatable(L, CLASS_HTTPCLIENT); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12364 | /* |
| 12365 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12366 | * Register class AppletTCP |
| 12367 | * |
| 12368 | */ |
| 12369 | |
| 12370 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12371 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12372 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12373 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12374 | lua_pushstring(L, "__index"); |
| 12375 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12376 | |
| 12377 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12378 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 12379 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 12380 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 12381 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 12382 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 12383 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 12384 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 12385 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12386 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12387 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12388 | |
| 12389 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12390 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12391 | |
| 12392 | /* |
| 12393 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12394 | * Register class AppletHTTP |
| 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 | a30b5db | 2015-09-18 09:04:27 +0200 | [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 | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12404 | |
| 12405 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12406 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 12407 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 12408 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 12409 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 12410 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 12411 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 12412 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 12413 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 12414 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 12415 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 12416 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12417 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12418 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12419 | |
| 12420 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12421 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12422 | |
| 12423 | /* |
| 12424 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12425 | * Register class TXN |
| 12426 | * |
| 12427 | */ |
| 12428 | |
| 12429 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12430 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12431 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12432 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12433 | lua_pushstring(L, "__index"); |
| 12434 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12435 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 12436 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12437 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 12438 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 12439 | hlua_class_function(L, "set_var", hlua_set_var); |
| 12440 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 12441 | hlua_class_function(L, "get_var", hlua_get_var); |
| 12442 | hlua_class_function(L, "done", hlua_txn_done); |
| 12443 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 12444 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 12445 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 12446 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 12447 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 12448 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 12449 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 12450 | hlua_class_function(L, "log", hlua_txn_log); |
| 12451 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 12452 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 12453 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 12454 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 12455 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12456 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12457 | |
| 12458 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12459 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12460 | |
| 12461 | /* |
| 12462 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 12463 | * Register class reply |
| 12464 | * |
| 12465 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12466 | lua_newtable(L); |
| 12467 | lua_pushstring(L, "__index"); |
| 12468 | lua_newtable(L); |
| 12469 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 12470 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 12471 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 12472 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 12473 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 12474 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 12475 | |
| 12476 | |
| 12477 | /* |
| 12478 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12479 | * Register class Socket |
| 12480 | * |
| 12481 | */ |
| 12482 | |
| 12483 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12484 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12485 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12486 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12487 | lua_pushstring(L, "__index"); |
| 12488 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12489 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 12490 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12491 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 12492 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12493 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 12494 | hlua_class_function(L, "send", hlua_socket_send); |
| 12495 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 12496 | hlua_class_function(L, "close", hlua_socket_close); |
| 12497 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 12498 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 12499 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 12500 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12501 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12502 | 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] | 12503 | |
| 12504 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12505 | lua_pushstring(L, "__gc"); |
| 12506 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 12507 | 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] | 12508 | |
| 12509 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12510 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12511 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12512 | lua_atpanic(L, hlua_panic_safe); |
| 12513 | |
| 12514 | return L; |
| 12515 | } |
| 12516 | |
| 12517 | void hlua_init(void) { |
| 12518 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 12519 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12520 | #ifdef USE_OPENSSL |
| 12521 | struct srv_kw *kw; |
| 12522 | int tmp_error; |
| 12523 | char *error; |
| 12524 | char *args[] = { /* SSL client configuration. */ |
| 12525 | "ssl", |
| 12526 | "verify", |
| 12527 | "none", |
| 12528 | NULL |
| 12529 | }; |
| 12530 | #endif |
| 12531 | |
| 12532 | /* Init post init function list head */ |
| 12533 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 12534 | LIST_INIT(&hlua_init_functions[i]); |
| 12535 | |
| 12536 | /* Init state for common/shared lua parts */ |
| 12537 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12538 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12539 | hlua_states[0] = hlua_init_state(0); |
| 12540 | |
| 12541 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 12542 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12543 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12544 | hlua_states[1] = hlua_init_state(1); |
| 12545 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12546 | /* Proxy and server configuration initialisation. */ |
William Lallemand | 6bb77b9 | 2021-07-28 15:48:16 +0200 | [diff] [blame] | 12547 | 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] | 12548 | if (!socket_proxy) { |
| 12549 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 12550 | exit(1); |
| 12551 | } |
| 12552 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12553 | |
| 12554 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12555 | socket_tcp = new_server(socket_proxy); |
| 12556 | if (!socket_tcp) { |
| 12557 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 12558 | exit(1); |
| 12559 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12560 | |
| 12561 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12562 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12563 | socket_ssl = new_server(socket_proxy); |
| 12564 | if (!socket_ssl) { |
| 12565 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 12566 | exit(1); |
| 12567 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12568 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12569 | socket_ssl->use_ssl = 1; |
| 12570 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12571 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 12572 | for (i = 0; args[i] != NULL; i++) { |
| 12573 | 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] | 12574 | /* |
| 12575 | * |
| 12576 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 12577 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12578 | * features like client certificates and ssl_verify. |
| 12579 | * |
| 12580 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12581 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12582 | if (tmp_error != 0) { |
| 12583 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 12584 | abort(); /* This must be never arrives because the command line |
| 12585 | not editable by the user. */ |
| 12586 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 12587 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12588 | } |
| 12589 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12590 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 12591 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 12592 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12593 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 12594 | static void hlua_deinit() |
| 12595 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 12596 | int thr; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12597 | struct hlua_reg_filter *reg_flt, *reg_flt_bck; |
| 12598 | |
| 12599 | list_for_each_entry_safe(reg_flt, reg_flt_bck, &referenced_filters, l) |
| 12600 | release_hlua_reg_filter(reg_flt); |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 12601 | |
| 12602 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 12603 | if (hlua_states[thr]) |
| 12604 | lua_close(hlua_states[thr]); |
| 12605 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 12606 | |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 12607 | srv_drop(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 12608 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 12609 | #ifdef USE_OPENSSL |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 12610 | srv_drop(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 12611 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 12612 | |
| 12613 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 12614 | } |
| 12615 | |
| 12616 | REGISTER_POST_DEINIT(hlua_deinit); |
| 12617 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12618 | static void hlua_register_build_options(void) |
| 12619 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12620 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12621 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12622 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 12623 | hap_register_build_opts(ptr, 1); |
| 12624 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12625 | |
| 12626 | INITCALL0(STG_REGISTER, hlua_register_build_options); |