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 */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 148 | static char ***per_thread_load = NULL; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 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: |
Olivier Houchard | 14f6268 | 2022-09-13 00:35:53 +0200 | [diff] [blame] | 863 | if (!p->table) { |
| 864 | msg = "Mandatory argument expected"; |
| 865 | goto error; |
| 866 | } |
| 867 | argp[idx].data.t = p->table; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 868 | argp[idx].type = ARGT_TAB; |
| 869 | argp[idx+1].type = ARGT_STOP; |
| 870 | break; |
| 871 | |
| 872 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 873 | msg = "Mandatory argument expected"; |
| 874 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 875 | break; |
| 876 | } |
| 877 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 878 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 879 | } |
| 880 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 881 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 882 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 883 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 884 | msg = "Last argument expected"; |
| 885 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 889 | argp[idx].type == ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 890 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 891 | } |
| 892 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 893 | /* Convert some argument types. All string in argp[] are for not |
| 894 | * duplicated yet. |
| 895 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 896 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 897 | case ARGT_SINT: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 898 | if (argp[idx].type != ARGT_SINT) { |
| 899 | msg = "integer expected"; |
| 900 | goto error; |
| 901 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 902 | argp[idx].type = ARGT_SINT; |
| 903 | break; |
| 904 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 905 | case ARGT_TIME: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 906 | if (argp[idx].type != ARGT_SINT) { |
| 907 | msg = "integer expected"; |
| 908 | goto error; |
| 909 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 910 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 911 | break; |
| 912 | |
| 913 | case ARGT_SIZE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 914 | if (argp[idx].type != ARGT_SINT) { |
| 915 | msg = "integer expected"; |
| 916 | goto error; |
| 917 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 918 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 919 | break; |
| 920 | |
| 921 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 922 | if (argp[idx].type != ARGT_STR) { |
| 923 | msg = "string expected"; |
| 924 | goto error; |
| 925 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 926 | 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] | 927 | if (!argp[idx].data.prx) { |
| 928 | msg = "frontend doesn't exist"; |
| 929 | goto error; |
| 930 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 931 | argp[idx].type = ARGT_FE; |
| 932 | break; |
| 933 | |
| 934 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 935 | if (argp[idx].type != ARGT_STR) { |
| 936 | msg = "string expected"; |
| 937 | goto error; |
| 938 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 939 | 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] | 940 | if (!argp[idx].data.prx) { |
| 941 | msg = "backend doesn't exist"; |
| 942 | goto error; |
| 943 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 944 | argp[idx].type = ARGT_BE; |
| 945 | break; |
| 946 | |
| 947 | case ARGT_TAB: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 948 | if (argp[idx].type != ARGT_STR) { |
| 949 | msg = "string expected"; |
| 950 | goto error; |
| 951 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 952 | 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] | 953 | if (!argp[idx].data.t) { |
| 954 | msg = "table doesn't exist"; |
| 955 | goto error; |
| 956 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 957 | argp[idx].type = ARGT_TAB; |
| 958 | break; |
| 959 | |
| 960 | case ARGT_SRV: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 961 | if (argp[idx].type != ARGT_STR) { |
| 962 | msg = "string expected"; |
| 963 | goto error; |
| 964 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 965 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 966 | if (sname) { |
| 967 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 968 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 969 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 970 | if (!px) { |
| 971 | msg = "backend doesn't exist"; |
| 972 | goto error; |
| 973 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 974 | } |
| 975 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 976 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 977 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 978 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 979 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 980 | if (!argp[idx].data.srv) { |
| 981 | msg = "server doesn't exist"; |
| 982 | goto error; |
| 983 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 984 | argp[idx].type = ARGT_SRV; |
| 985 | break; |
| 986 | |
| 987 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 988 | if (argp[idx].type != ARGT_STR) { |
| 989 | msg = "string expected"; |
| 990 | goto error; |
| 991 | } |
| 992 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 993 | msg = "invalid IPv4 address"; |
| 994 | goto error; |
| 995 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 996 | argp[idx].type = ARGT_IPV4; |
| 997 | break; |
| 998 | |
| 999 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1000 | if (argp[idx].type == ARGT_SINT) |
| 1001 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 1002 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1003 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 1004 | msg = "invalid IPv4 mask"; |
| 1005 | goto error; |
| 1006 | } |
| 1007 | } |
| 1008 | else { |
| 1009 | msg = "integer or string expected"; |
| 1010 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1011 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1012 | argp[idx].type = ARGT_MSK4; |
| 1013 | break; |
| 1014 | |
| 1015 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1016 | if (argp[idx].type != ARGT_STR) { |
| 1017 | msg = "string expected"; |
| 1018 | goto error; |
| 1019 | } |
| 1020 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 1021 | msg = "invalid IPv6 address"; |
| 1022 | goto error; |
| 1023 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1024 | argp[idx].type = ARGT_IPV6; |
| 1025 | break; |
| 1026 | |
| 1027 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1028 | if (argp[idx].type == ARGT_SINT) |
| 1029 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 1030 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1031 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 1032 | msg = "invalid IPv6 mask"; |
| 1033 | goto error; |
| 1034 | } |
| 1035 | } |
| 1036 | else { |
| 1037 | msg = "integer or string expected"; |
| 1038 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1039 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 1040 | argp[idx].type = ARGT_MSK6; |
| 1041 | break; |
| 1042 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 1043 | case ARGT_REG: |
| 1044 | if (argp[idx].type != ARGT_STR) { |
| 1045 | msg = "string expected"; |
| 1046 | goto error; |
| 1047 | } |
| 1048 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 1049 | if (!reg) { |
| 1050 | msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'", |
| 1051 | argp[idx].data.str.area, err); |
| 1052 | free(err); |
| 1053 | goto error; |
| 1054 | } |
| 1055 | argp[idx].type = ARGT_REG; |
| 1056 | argp[idx].data.reg = reg; |
| 1057 | break; |
| 1058 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1059 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1060 | if (argp[idx].type != ARGT_STR) { |
| 1061 | msg = "string expected"; |
| 1062 | goto error; |
| 1063 | } |
| 1064 | if (p->uri_auth && p->uri_auth->userlist && |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1065 | strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0) |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1066 | ul = p->uri_auth->userlist; |
| 1067 | else |
| 1068 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 1069 | |
| 1070 | if (!ul) { |
| 1071 | msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area); |
| 1072 | goto error; |
| 1073 | } |
| 1074 | argp[idx].type = ARGT_USR; |
| 1075 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1076 | break; |
| 1077 | |
| 1078 | case ARGT_STR: |
| 1079 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 1080 | msg = "unable to duplicate string arg"; |
| 1081 | goto error; |
| 1082 | } |
| 1083 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1084 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1085 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1086 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1087 | msg = "type not yet supported"; |
| 1088 | goto error; |
| 1089 | break; |
| 1090 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | /* Check for type of argument. */ |
| 1094 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1095 | msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 1096 | arg_type_names[(mask & ARGT_MASK)], |
| 1097 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 1098 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | /* Next argument. */ |
| 1102 | mask >>= ARGT_BITS; |
| 1103 | idx++; |
| 1104 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1105 | return 0; |
| 1106 | |
| 1107 | error: |
Olivier Houchard | ca43161 | 2022-09-13 00:31:17 +0200 | [diff] [blame] | 1108 | argp[idx].type = ARGT_STOP; |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 1109 | free_args(argp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1110 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 1111 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1112 | } |
| 1113 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1114 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1115 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1116 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1117 | * |
| 1118 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1119 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 1120 | */ |
| 1121 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 1122 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1123 | struct hlua **hlua = lua_getextraspace(L); |
| 1124 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1125 | } |
| 1126 | static inline void hlua_sethlua(struct hlua *hlua) |
| 1127 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1128 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 1129 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1130 | } |
| 1131 | |
Willy Tarreau | 0b7b639 | 2022-06-19 17:39:33 +0200 | [diff] [blame] | 1132 | /* Will return a non-NULL string indicating the Lua call trace if the caller |
| 1133 | * currently is executing from within a Lua function. One line per entry will |
| 1134 | * be emitted, and each extra line will be prefixed with <pfx>. If a current |
| 1135 | * Lua function is not detected, NULL is returned. |
| 1136 | */ |
| 1137 | const char *hlua_show_current_location(const char *pfx) |
| 1138 | { |
| 1139 | lua_State *L; |
| 1140 | lua_Debug ar; |
| 1141 | |
| 1142 | /* global or per-thread stack initializing ? */ |
| 1143 | if (hlua_state_id != -1 && (L = hlua_states[hlua_state_id]) && lua_getstack(L, 0, &ar)) |
| 1144 | return hlua_traceback(L, pfx); |
| 1145 | |
| 1146 | /* per-thread stack running ? */ |
| 1147 | if (hlua_states[tid + 1] && (L = hlua_states[tid + 1]) && lua_getstack(L, 0, &ar)) |
| 1148 | return hlua_traceback(L, pfx); |
| 1149 | |
| 1150 | /* global stack running ? */ |
| 1151 | if (hlua_states[0] && (L = hlua_states[0]) && lua_getstack(L, 0, &ar)) |
| 1152 | return hlua_traceback(L, pfx); |
| 1153 | |
| 1154 | return NULL; |
| 1155 | } |
| 1156 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1157 | /* This function is used to send logs. It try to send on screen (stderr) |
| 1158 | * and on the default syslog server. |
| 1159 | */ |
| 1160 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 1161 | { |
| 1162 | struct tm tm; |
| 1163 | char *p; |
| 1164 | |
| 1165 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1166 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1167 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1168 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 1169 | /* Break the message if exceed the buffer size. */ |
| 1170 | *(p-4) = ' '; |
| 1171 | *(p-3) = '.'; |
| 1172 | *(p-2) = '.'; |
| 1173 | *(p-1) = '.'; |
| 1174 | break; |
| 1175 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1176 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1177 | *p = *msg; |
| 1178 | else |
| 1179 | *p = '.'; |
| 1180 | } |
| 1181 | *p = '\0'; |
| 1182 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1183 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1184 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 1185 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 1186 | return; |
| 1187 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 1188 | get_localtime(date.tv_sec, &tm); |
| 1189 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1190 | 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] | 1191 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1192 | fflush(stderr); |
| 1193 | } |
| 1194 | } |
| 1195 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1196 | /* This function just ensure that the yield will be always |
| 1197 | * returned with a timeout and permit to set some flags |
| 1198 | */ |
| 1199 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1200 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1201 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1202 | struct hlua *hlua; |
| 1203 | |
| 1204 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1205 | hlua = hlua_gethlua(L); |
| 1206 | if (!hlua) { |
| 1207 | return; |
| 1208 | } |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1209 | |
| 1210 | /* Set the wake timeout. If timeout is required, we set |
| 1211 | * the expiration time. |
| 1212 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1213 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1214 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1215 | hlua->flags |= flags; |
| 1216 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1217 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1218 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1219 | } |
| 1220 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1221 | /* This function initialises the Lua environment stored in the stream. |
| 1222 | * 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] | 1223 | * 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] | 1224 | * |
| 1225 | * This function is particular. it initialises a new Lua thread. If the |
| 1226 | * initialisation fails (example: out of memory error), the lua function |
| 1227 | * throws an error (longjmp). |
| 1228 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1229 | * 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] | 1230 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1231 | * threads appear, the safe environment set a lock to ensure only one |
| 1232 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1233 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1234 | * |
| 1235 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1236 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1237 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1238 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1239 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1240 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1241 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1242 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1243 | 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] | 1244 | { |
| 1245 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1246 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1247 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1248 | lua->wake_time = TICK_ETERNITY; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1249 | lua->state_id = state_id; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1250 | LIST_INIT(&lua->com); |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1251 | LIST_INIT(&lua->hc_list); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1252 | if (!already_safe) { |
| 1253 | if (!SET_SAFE_LJMP_PARENT(lua)) { |
| 1254 | lua->Tref = LUA_REFNIL; |
| 1255 | return 0; |
| 1256 | } |
| 1257 | } |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1258 | lua->T = lua_newthread(hlua_states[state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1259 | if (!lua->T) { |
| 1260 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1261 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1262 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1263 | return 0; |
| 1264 | } |
| 1265 | hlua_sethlua(lua); |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1266 | lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1267 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1268 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1269 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1270 | return 1; |
| 1271 | } |
| 1272 | |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1273 | /* kill all associated httpclient to this hlua task */ |
| 1274 | static void hlua_httpclient_destroy_all(struct hlua *hlua) |
| 1275 | { |
| 1276 | struct hlua_httpclient *hlua_hc, *back; |
| 1277 | |
| 1278 | list_for_each_entry_safe(hlua_hc, back, &hlua->hc_list, by_hlua) { |
| 1279 | if (hlua_hc->hc) |
| 1280 | httpclient_stop_and_destroy(hlua_hc->hc); |
| 1281 | hlua_hc->hc = NULL; |
William Lallemand | 94dbfed | 2022-12-08 11:11:36 +0100 | [diff] [blame] | 1282 | LIST_DEL_INIT(&hlua_hc->by_hlua); |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1287 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1288 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 1289 | * is not freed. |
| 1290 | */ |
| 1291 | void hlua_ctx_destroy(struct hlua *lua) |
| 1292 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1293 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1294 | return; |
| 1295 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1296 | if (!lua->T) |
| 1297 | goto end; |
| 1298 | |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1299 | /* clean all running httpclient */ |
| 1300 | hlua_httpclient_destroy_all(lua); |
| 1301 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1302 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1303 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1304 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1305 | if (!SET_SAFE_LJMP(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1306 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1307 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1308 | RESET_SAFE_LJMP(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1309 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1310 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1311 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1312 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1313 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1314 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1315 | * without error, we run the GC on the thread pointer. Its freed all |
| 1316 | * the unused memory. |
| 1317 | * If the thread is finnish with an error or is currently yielded, |
| 1318 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1319 | * so e run the GC on the main thread. |
| 1320 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1321 | * the garbage collection. |
| 1322 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1323 | if (lua->gc_count) { |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1324 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1325 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1326 | lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1327 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1328 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1329 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1330 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1331 | |
| 1332 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1333 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | /* This function is used to restore the Lua context when a coroutine |
| 1337 | * fails. This function copy the common memory between old coroutine |
| 1338 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1339 | * replaced by the new coroutine. |
| 1340 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1341 | * as string error message and it is copied in the new stack. |
| 1342 | */ |
| 1343 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1344 | { |
| 1345 | lua_State *T; |
| 1346 | int new_ref; |
| 1347 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1348 | /* New Lua coroutine. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1349 | T = lua_newthread(hlua_states[lua->state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1350 | if (!T) |
| 1351 | return 0; |
| 1352 | |
| 1353 | /* Copy last error message. */ |
| 1354 | if (keep_msg) |
| 1355 | lua_xmove(lua->T, T, 1); |
| 1356 | |
| 1357 | /* Copy data between the coroutines. */ |
| 1358 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1359 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1360 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1361 | |
| 1362 | /* Destroy old data. */ |
| 1363 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1364 | |
| 1365 | /* The thread is garbage collected by Lua. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1366 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1367 | |
| 1368 | /* Fill the struct with the new coroutine values. */ |
| 1369 | lua->Mref = new_ref; |
| 1370 | lua->T = T; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1371 | lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1372 | |
| 1373 | /* Set context. */ |
| 1374 | hlua_sethlua(lua); |
| 1375 | |
| 1376 | return 1; |
| 1377 | } |
| 1378 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1379 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1380 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1381 | struct hlua *hlua; |
| 1382 | |
| 1383 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1384 | hlua = hlua_gethlua(L); |
| 1385 | if (!hlua) |
| 1386 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1387 | |
| 1388 | /* Lua cannot yield when its returning from a function, |
| 1389 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1390 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1391 | */ |
| 1392 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1393 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1394 | return; |
| 1395 | } |
| 1396 | |
| 1397 | /* restore the interrupt condition. */ |
| 1398 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1399 | |
| 1400 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1401 | * If the state is not yieldable, trying yield causes an error. |
| 1402 | */ |
| 1403 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1404 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1405 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1406 | /* If we cannot yield, update the clock and check the timeout. */ |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 1407 | clock_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1408 | hlua->run_time += now_ms - hlua->start_time; |
| 1409 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1410 | lua_pushfstring(L, "execution timeout"); |
| 1411 | WILL_LJMP(lua_error(L)); |
| 1412 | } |
| 1413 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1414 | /* Update the start time. */ |
| 1415 | hlua->start_time = now_ms; |
| 1416 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1417 | /* Try to interrupt the process at the end of the current |
| 1418 | * unyieldable function. |
| 1419 | */ |
| 1420 | 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] | 1421 | } |
| 1422 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1423 | /* This function start or resumes the Lua stack execution. If the flag |
| 1424 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1425 | * The function return an error. |
| 1426 | * |
| 1427 | * The function can returns 4 values: |
| 1428 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1429 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1430 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1431 | * - 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] | 1432 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1433 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1434 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1435 | * 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] | 1436 | * LUA code. |
| 1437 | */ |
| 1438 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1439 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1440 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1441 | int nres; |
| 1442 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1443 | int ret; |
| 1444 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1445 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1446 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1447 | /* Initialise run time counter. */ |
| 1448 | if (!HLUA_IS_RUNNING(lua)) |
| 1449 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1450 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1451 | /* Lock the whole Lua execution. This lock must be before the |
| 1452 | * label "resume_execution". |
| 1453 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1454 | if (lua->state_id == 0) |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 1455 | lua_take_global_lock(); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1456 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1457 | resume_execution: |
| 1458 | |
| 1459 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1460 | * instructions. it is used for preventing infinite loops. |
| 1461 | */ |
| 1462 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1463 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1464 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1465 | HLUA_SET_RUN(lua); |
| 1466 | HLUA_CLR_CTRLYIELD(lua); |
| 1467 | HLUA_CLR_WAKERESWR(lua); |
| 1468 | HLUA_CLR_WAKEREQWR(lua); |
Christopher Faulet | 1f43a34 | 2021-08-04 17:58:21 +0200 | [diff] [blame] | 1469 | HLUA_CLR_NOYIELD(lua); |
| 1470 | if (!yield_allowed) |
| 1471 | HLUA_SET_NOYIELD(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1472 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1473 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1474 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1475 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1476 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1477 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1478 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1479 | 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] | 1480 | #else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1481 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1482 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1483 | switch (ret) { |
| 1484 | |
| 1485 | case LUA_OK: |
| 1486 | ret = HLUA_E_OK; |
| 1487 | break; |
| 1488 | |
| 1489 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1490 | /* Check if the execution timeout is expired. It it is the case, we |
| 1491 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1492 | */ |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 1493 | clock_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1494 | lua->run_time += now_ms - lua->start_time; |
| 1495 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1496 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1497 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1498 | break; |
| 1499 | } |
| 1500 | /* Process the forced yield. if the general yield is not allowed or |
| 1501 | * if no task were associated this the current Lua execution |
| 1502 | * coroutine, we resume the execution. Else we want to return in the |
| 1503 | * scheduler and we want to be waked up again, to continue the |
| 1504 | * current Lua execution. So we schedule our own task. |
| 1505 | */ |
| 1506 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1507 | if (!yield_allowed || !lua->task) |
| 1508 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1509 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1510 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1511 | if (!yield_allowed) { |
| 1512 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1513 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1514 | break; |
| 1515 | } |
| 1516 | ret = HLUA_E_AGAIN; |
| 1517 | break; |
| 1518 | |
| 1519 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1520 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1521 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1522 | * because the errors ares the only one mean to return immediately |
| 1523 | * from and lua execution. |
| 1524 | */ |
| 1525 | if (lua->flags & HLUA_EXIT) { |
| 1526 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1527 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1528 | break; |
| 1529 | } |
| 1530 | |
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); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 1539 | trace = hlua_traceback(lua->T, ", "); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1540 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1541 | 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] | 1542 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1543 | 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] | 1544 | ret = HLUA_E_ERRMSG; |
| 1545 | break; |
| 1546 | |
| 1547 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1548 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1549 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1550 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1551 | break; |
| 1552 | |
| 1553 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1554 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1555 | if (!lua_checkstack(lua->T, 1)) { |
| 1556 | ret = HLUA_E_ERR; |
| 1557 | break; |
| 1558 | } |
| 1559 | msg = lua_tostring(lua->T, -1); |
| 1560 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1561 | lua_pop(lua->T, 1); |
| 1562 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1563 | 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] | 1564 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1565 | 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] | 1566 | ret = HLUA_E_ERRMSG; |
| 1567 | break; |
| 1568 | |
| 1569 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1570 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1571 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1572 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1573 | break; |
| 1574 | } |
| 1575 | |
| 1576 | switch (ret) { |
| 1577 | case HLUA_E_AGAIN: |
| 1578 | break; |
| 1579 | |
| 1580 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1581 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1582 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1583 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1584 | break; |
| 1585 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1586 | case HLUA_E_ETMOUT: |
| 1587 | case HLUA_E_NOMEM: |
| 1588 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1589 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1590 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1591 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1592 | hlua_ctx_renew(lua, 0); |
| 1593 | break; |
| 1594 | |
| 1595 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1596 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1597 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1598 | break; |
| 1599 | } |
| 1600 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1601 | /* This is the main exit point, remove the Lua lock. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1602 | if (lua->state_id == 0) |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 1603 | lua_drop_global_lock(); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1604 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1605 | return ret; |
| 1606 | } |
| 1607 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1608 | /* This function exit the current code. */ |
| 1609 | __LJMP static int hlua_done(lua_State *L) |
| 1610 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1611 | struct hlua *hlua; |
| 1612 | |
| 1613 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1614 | hlua = hlua_gethlua(L); |
| 1615 | if (!hlua) |
| 1616 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1617 | |
| 1618 | hlua->flags |= HLUA_EXIT; |
| 1619 | WILL_LJMP(lua_error(L)); |
| 1620 | |
| 1621 | return 0; |
| 1622 | } |
| 1623 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1624 | /* This function is an LUA binding. It provides a function |
| 1625 | * for deleting ACL from a referenced ACL file. |
| 1626 | */ |
| 1627 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1628 | { |
| 1629 | const char *name; |
| 1630 | const char *key; |
| 1631 | struct pat_ref *ref; |
| 1632 | |
| 1633 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1634 | |
| 1635 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1636 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1637 | |
| 1638 | ref = pat_ref_lookup(name); |
| 1639 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1640 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1641 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1642 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1643 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1644 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1645 | return 0; |
| 1646 | } |
| 1647 | |
| 1648 | /* This function is an LUA binding. It provides a function |
| 1649 | * for deleting map entry from a referenced map file. |
| 1650 | */ |
| 1651 | static int hlua_del_map(lua_State *L) |
| 1652 | { |
| 1653 | const char *name; |
| 1654 | const char *key; |
| 1655 | struct pat_ref *ref; |
| 1656 | |
| 1657 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1658 | |
| 1659 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1660 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1661 | |
| 1662 | ref = pat_ref_lookup(name); |
| 1663 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1664 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1665 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1666 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1667 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1668 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1669 | return 0; |
| 1670 | } |
| 1671 | |
| 1672 | /* This function is an LUA binding. It provides a function |
| 1673 | * for adding ACL pattern from a referenced ACL file. |
| 1674 | */ |
| 1675 | static int hlua_add_acl(lua_State *L) |
| 1676 | { |
| 1677 | const char *name; |
| 1678 | const char *key; |
| 1679 | struct pat_ref *ref; |
| 1680 | |
| 1681 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1682 | |
| 1683 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1684 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1685 | |
| 1686 | ref = pat_ref_lookup(name); |
| 1687 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1688 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1689 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1690 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1691 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1692 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1693 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1694 | return 0; |
| 1695 | } |
| 1696 | |
| 1697 | /* This function is an LUA binding. It provides a function |
| 1698 | * for setting map pattern and sample from a referenced map |
| 1699 | * file. |
| 1700 | */ |
| 1701 | static int hlua_set_map(lua_State *L) |
| 1702 | { |
| 1703 | const char *name; |
| 1704 | const char *key; |
| 1705 | const char *value; |
| 1706 | struct pat_ref *ref; |
| 1707 | |
| 1708 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1709 | |
| 1710 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1711 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1712 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1713 | |
| 1714 | ref = pat_ref_lookup(name); |
| 1715 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1716 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1717 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1718 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1719 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1720 | pat_ref_set(ref, key, value, NULL); |
| 1721 | else |
| 1722 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1723 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1724 | return 0; |
| 1725 | } |
| 1726 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1727 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1728 | * 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] | 1729 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1730 | * the name of the object (_G[<name>] = <metable> ). |
| 1731 | * |
| 1732 | * A metable is a table that modify the standard behavior of a standard |
| 1733 | * access to the associated data. The entries of this new metatable are |
| 1734 | * defined as is: |
| 1735 | * |
| 1736 | * http://lua-users.org/wiki/MetatableEvents |
| 1737 | * |
| 1738 | * __index |
| 1739 | * |
| 1740 | * we access an absent field in a table, the result is nil. This is |
| 1741 | * true, but it is not the whole truth. Actually, such access triggers |
| 1742 | * the interpreter to look for an __index metamethod: If there is no |
| 1743 | * such method, as usually happens, then the access results in nil; |
| 1744 | * otherwise, the metamethod will provide the result. |
| 1745 | * |
| 1746 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1747 | * the key does not appear in the table, but the metatable has an __index |
| 1748 | * property: |
| 1749 | * |
| 1750 | * - if the value is a function, the function is called, passing in the |
| 1751 | * table and the key; the return value of that function is returned as |
| 1752 | * the result. |
| 1753 | * |
| 1754 | * - if the value is another table, the value of the key in that table is |
| 1755 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1756 | * table's metatable has an __index property, then it continues on up) |
| 1757 | * |
| 1758 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1759 | * |
| 1760 | * http://www.lua.org/pil/13.4.1.html |
| 1761 | * |
| 1762 | * __newindex |
| 1763 | * |
| 1764 | * Like __index, but control property assignment. |
| 1765 | * |
| 1766 | * __mode - Control weak references. A string value with one or both |
| 1767 | * of the characters 'k' and 'v' which specifies that the the |
| 1768 | * keys and/or values in the table are weak references. |
| 1769 | * |
| 1770 | * __call - Treat a table like a function. When a table is followed by |
| 1771 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1772 | * a __call key pointing to a function, that function is invoked |
| 1773 | * (passing any specified arguments) and the return value is |
| 1774 | * returned. |
| 1775 | * |
| 1776 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1777 | * called, if the metatable for myTable has a __metatable |
| 1778 | * key, the value of that key is returned instead of the |
| 1779 | * actual metatable. |
| 1780 | * |
| 1781 | * __tostring - Control string representation. When the builtin |
| 1782 | * "tostring( myTable )" function is called, if the metatable |
| 1783 | * for myTable has a __tostring property set to a function, |
| 1784 | * that function is invoked (passing myTable to it) and the |
| 1785 | * return value is used as the string representation. |
| 1786 | * |
| 1787 | * __len - Control table length. When the table length is requested using |
| 1788 | * the length operator ( '#' ), if the metatable for myTable has |
| 1789 | * a __len key pointing to a function, that function is invoked |
| 1790 | * (passing myTable to it) and the return value used as the value |
| 1791 | * of "#myTable". |
| 1792 | * |
| 1793 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1794 | * collected, if the metatable has a __gc field pointing to a |
| 1795 | * function, that function is first invoked, passing the userdata |
| 1796 | * to it. The __gc metamethod is not called for tables. |
| 1797 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1798 | * |
| 1799 | * Special metamethods for redefining standard operators: |
| 1800 | * http://www.lua.org/pil/13.1.html |
| 1801 | * |
| 1802 | * __add "+" |
| 1803 | * __sub "-" |
| 1804 | * __mul "*" |
| 1805 | * __div "/" |
| 1806 | * __unm "!" |
| 1807 | * __pow "^" |
| 1808 | * __concat ".." |
| 1809 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1810 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1811 | * http://www.lua.org/pil/13.2.html |
| 1812 | * |
| 1813 | * __eq "==" |
| 1814 | * __lt "<" |
| 1815 | * __le "<=" |
| 1816 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1817 | |
| 1818 | /* |
| 1819 | * |
| 1820 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1821 | * Class Map |
| 1822 | * |
| 1823 | * |
| 1824 | */ |
| 1825 | |
| 1826 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1827 | * a class session, otherwise it throws an error. |
| 1828 | */ |
| 1829 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1830 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1831 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1832 | } |
| 1833 | |
| 1834 | /* This function is the map constructor. It don't need |
| 1835 | * the class Map object. It creates and return a new Map |
| 1836 | * object. It must be called only during "body" or "init" |
| 1837 | * context because it process some filesystem accesses. |
| 1838 | */ |
| 1839 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1840 | { |
| 1841 | const char *fn; |
| 1842 | int match = PAT_MATCH_STR; |
| 1843 | struct sample_conv conv; |
| 1844 | const char *file = ""; |
| 1845 | int line = 0; |
| 1846 | lua_Debug ar; |
| 1847 | char *err = NULL; |
| 1848 | struct arg args[2]; |
| 1849 | |
| 1850 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1851 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1852 | |
| 1853 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1854 | |
| 1855 | if (lua_gettop(L) >= 2) { |
| 1856 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1857 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1858 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1859 | } |
| 1860 | |
| 1861 | /* Get Lua filename and line number. */ |
| 1862 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1863 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1864 | if (ar.currentline > 0) { /* is there info? */ |
| 1865 | file = ar.short_src; |
| 1866 | line = ar.currentline; |
| 1867 | } |
| 1868 | } |
| 1869 | |
| 1870 | /* fill fake sample_conv struct. */ |
| 1871 | conv.kw = ""; /* unused. */ |
| 1872 | conv.process = NULL; /* unused. */ |
| 1873 | conv.arg_mask = 0; /* unused. */ |
| 1874 | conv.val_args = NULL; /* unused. */ |
| 1875 | conv.out_type = SMP_T_STR; |
| 1876 | conv.private = (void *)(long)match; |
| 1877 | switch (match) { |
| 1878 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1879 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1880 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1881 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1882 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1883 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1884 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1885 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1886 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1887 | default: |
| 1888 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1889 | } |
| 1890 | |
| 1891 | /* fill fake args. */ |
| 1892 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1893 | args[0].data.str.area = strdup(fn); |
| 1894 | args[0].data.str.data = strlen(fn); |
| 1895 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1896 | args[1].type = ARGT_STOP; |
| 1897 | |
| 1898 | /* load the map. */ |
| 1899 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1900 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1901 | * free the err variable. |
| 1902 | */ |
| 1903 | luaL_where(L, 1); |
| 1904 | lua_pushfstring(L, "'new': %s.", err); |
| 1905 | lua_concat(L, 2); |
| 1906 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1907 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1908 | WILL_LJMP(lua_error(L)); |
| 1909 | } |
| 1910 | |
| 1911 | /* create the lua object. */ |
| 1912 | lua_newtable(L); |
| 1913 | lua_pushlightuserdata(L, args[0].data.map); |
| 1914 | lua_rawseti(L, -2, 0); |
| 1915 | |
| 1916 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1917 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1918 | lua_setmetatable(L, -2); |
| 1919 | |
| 1920 | |
| 1921 | return 1; |
| 1922 | } |
| 1923 | |
| 1924 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1925 | { |
| 1926 | struct map_descriptor *desc; |
| 1927 | struct pattern *pat; |
| 1928 | struct sample smp; |
| 1929 | |
| 1930 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1931 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1932 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1933 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1934 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1935 | } |
| 1936 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1937 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1938 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1939 | 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] | 1940 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1941 | } |
| 1942 | |
| 1943 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1944 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1945 | if (str) |
| 1946 | lua_pushstring(L, ""); |
| 1947 | else |
| 1948 | lua_pushnil(L); |
| 1949 | return 1; |
| 1950 | } |
| 1951 | |
| 1952 | /* 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] | 1953 | 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] | 1954 | return 1; |
| 1955 | } |
| 1956 | |
| 1957 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1958 | { |
| 1959 | return _hlua_map_lookup(L, 0); |
| 1960 | } |
| 1961 | |
| 1962 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1963 | { |
| 1964 | return _hlua_map_lookup(L, 1); |
| 1965 | } |
| 1966 | |
| 1967 | /* |
| 1968 | * |
| 1969 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1970 | * Class Socket |
| 1971 | * |
| 1972 | * |
| 1973 | */ |
| 1974 | |
| 1975 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1976 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1977 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1978 | } |
| 1979 | |
| 1980 | /* 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] | 1981 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1982 | * received. |
| 1983 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1984 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1985 | { |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1986 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 1987 | struct stconn *sc = appctx_sc(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1988 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1989 | if (ctx->die) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 1990 | sc_shutw(sc); |
| 1991 | sc_shutr(sc); |
Christopher Faulet | 6e1bbc4 | 2022-12-12 08:08:15 +0100 | [diff] [blame] | 1992 | sc_ic(sc)->flags |= CF_READ_EVENT; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1993 | notification_wake(&ctx->wake_on_read); |
| 1994 | notification_wake(&ctx->wake_on_write); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 1995 | stream_shutdown(__sc_strm(sc), SF_ERR_KILLED); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1996 | } |
| 1997 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1998 | /* If we can't write, wakeup the pending write signals. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 1999 | if (channel_output_closed(sc_ic(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2000 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 2001 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 2002 | /* If we can't read, wakeup the pending read signals. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2003 | if (channel_input_closed(sc_oc(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2004 | notification_wake(&ctx->wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 2005 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2006 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 2007 | * to be notified whenever the connection completes. |
| 2008 | */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2009 | if (sc_opposite(sc)->state < SC_ST_EST) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 2010 | applet_need_more_data(appctx); |
Willy Tarreau | b23edc8 | 2022-05-24 16:49:03 +0200 | [diff] [blame] | 2011 | se_need_remote_conn(appctx->sedesc); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 2012 | applet_have_more_data(appctx); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 2013 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 2014 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2015 | |
| 2016 | /* This function is called after the connect. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2017 | ctx->connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2018 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2019 | /* 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] | 2020 | if (channel_may_recv(sc_ic(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2021 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2022 | |
| 2023 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2024 | if (!channel_is_empty(sc_oc(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2025 | notification_wake(&ctx->wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2026 | |
| 2027 | /* Some data were injected in the buffer, notify the stream |
| 2028 | * interface. |
| 2029 | */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2030 | if (!channel_is_empty(sc_ic(sc))) |
| 2031 | sc_update(sc); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2032 | |
| 2033 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2034 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2035 | */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2036 | if (notification_registered(&ctx->wake_on_write)) |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 2037 | applet_have_more_data(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2038 | } |
| 2039 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2040 | static int hlua_socket_init(struct appctx *appctx) |
| 2041 | { |
| 2042 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
| 2043 | struct stream *s; |
| 2044 | |
| 2045 | if (appctx_finalize_startup(appctx, socket_proxy, &BUF_NULL) == -1) |
| 2046 | goto error; |
| 2047 | |
| 2048 | s = appctx_strm(appctx); |
| 2049 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 2050 | /* Configure "right" stream connector. This stconn is used to connect |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2051 | * and retrieve data from the server. The connection is initialized |
| 2052 | * with the "struct server". |
| 2053 | */ |
Willy Tarreau | 74568cf | 2022-05-27 09:03:30 +0200 | [diff] [blame] | 2054 | sc_set_state(s->scb, SC_ST_ASS); |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2055 | |
| 2056 | /* Force destination server. */ |
| 2057 | s->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
| 2058 | s->target = &socket_tcp->obj_type; |
| 2059 | |
| 2060 | ctx->appctx = appctx; |
| 2061 | return 0; |
| 2062 | |
| 2063 | error: |
| 2064 | return -1; |
| 2065 | } |
| 2066 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2067 | /* This function is called when the "struct stream" is destroyed. |
| 2068 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2069 | * Wake all the pending signals. |
| 2070 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 2071 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2072 | { |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2073 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2074 | struct xref *peer; |
| 2075 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2076 | /* Remove my link in the original objects. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2077 | peer = xref_get_peer_and_lock(&ctx->xref); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2078 | if (peer) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2079 | xref_disconnect(&ctx->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2080 | |
| 2081 | /* Wake all the task waiting for me. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2082 | notification_wake(&ctx->wake_on_read); |
| 2083 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2084 | } |
| 2085 | |
| 2086 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2087 | * uses this object. If the stream does not exists, just quit. |
| 2088 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2089 | * pending signal can rest in the read and write lists. destroy |
| 2090 | * it. |
| 2091 | */ |
| 2092 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 2093 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2094 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2095 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2096 | struct xref *peer; |
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 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 2099 | |
| 2100 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2101 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2102 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2103 | return 0; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2104 | |
| 2105 | ctx = container_of(peer, struct hlua_csk_ctx, xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2106 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2107 | /* Set the flag which destroy the session. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2108 | ctx->die = 1; |
| 2109 | appctx_wakeup(ctx->appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2110 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2111 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2112 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2113 | return 0; |
| 2114 | } |
| 2115 | |
| 2116 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2117 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2118 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2119 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2120 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2121 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2122 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2123 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2124 | struct hlua *hlua; |
| 2125 | |
| 2126 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2127 | hlua = hlua_gethlua(L); |
| 2128 | if (!hlua) |
| 2129 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2130 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2131 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2132 | |
| 2133 | /* Check if we run on the same thread than the xreator thread. |
| 2134 | * We cannot access to the socket if the thread is different. |
| 2135 | */ |
| 2136 | if (socket->tid != tid) |
| 2137 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2138 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2139 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2140 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2141 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2142 | |
| 2143 | hlua->gc_count--; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2144 | ctx = container_of(peer, struct hlua_csk_ctx, xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2145 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2146 | /* Set the flag which destroy the session. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2147 | ctx->die = 1; |
| 2148 | appctx_wakeup(ctx->appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2149 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2150 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2151 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2152 | return 0; |
| 2153 | } |
| 2154 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2155 | /* The close function calls close_helper. |
| 2156 | */ |
| 2157 | __LJMP static int hlua_socket_close(lua_State *L) |
| 2158 | { |
| 2159 | MAY_LJMP(check_args(L, 1, "close")); |
| 2160 | return hlua_socket_close_helper(L); |
| 2161 | } |
| 2162 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2163 | /* This Lua function assumes that the stack contain three parameters. |
| 2164 | * 1 - USERDATA containing a struct socket |
| 2165 | * 2 - INTEGER with values of the macro defined below |
| 2166 | * If the integer is -1, we must read at most one line. |
| 2167 | * If the integer is -2, we ust read all the data until the |
| 2168 | * end of the stream. |
| 2169 | * If the integer is positive value, we must read a number of |
| 2170 | * bytes corresponding to this value. |
| 2171 | */ |
| 2172 | #define HLSR_READ_LINE (-1) |
| 2173 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2174 | __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] | 2175 | { |
| 2176 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2177 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2178 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2179 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2180 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2181 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2182 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2183 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2184 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2185 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2186 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2187 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 2188 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2189 | struct stream *s; |
| 2190 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2191 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2192 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2193 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2194 | hlua = hlua_gethlua(L); |
| 2195 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2196 | /* Check if this lua stack is schedulable. */ |
| 2197 | if (!hlua || !hlua->task) |
| 2198 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 2199 | "'frontend', 'backend' or 'task'")); |
| 2200 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2201 | /* Check if we run on the same thread than the xreator thread. |
| 2202 | * We cannot access to the socket if the thread is different. |
| 2203 | */ |
| 2204 | if (socket->tid != tid) |
| 2205 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2206 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2207 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2208 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2209 | if (!peer) |
| 2210 | goto no_peer; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2211 | |
| 2212 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2213 | appctx = csk_ctx->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2214 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2215 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2216 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2217 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2218 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2219 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2220 | if (nblk < 0) /* Connection close. */ |
| 2221 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2222 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2223 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2224 | |
| 2225 | /* remove final \r\n. */ |
| 2226 | if (nblk == 1) { |
| 2227 | if (blk1[len1-1] == '\n') { |
| 2228 | len1--; |
| 2229 | skip_at_end++; |
| 2230 | if (blk1[len1-1] == '\r') { |
| 2231 | len1--; |
| 2232 | skip_at_end++; |
| 2233 | } |
| 2234 | } |
| 2235 | } |
| 2236 | else { |
| 2237 | if (blk2[len2-1] == '\n') { |
| 2238 | len2--; |
| 2239 | skip_at_end++; |
| 2240 | if (blk2[len2-1] == '\r') { |
| 2241 | len2--; |
| 2242 | skip_at_end++; |
| 2243 | } |
| 2244 | } |
| 2245 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2246 | } |
| 2247 | |
| 2248 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2249 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2250 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2251 | if (nblk < 0) /* Connection close. */ |
| 2252 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2253 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2254 | goto connection_empty; |
| 2255 | } |
| 2256 | |
| 2257 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2258 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2259 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2260 | if (nblk < 0) /* Connection close. */ |
| 2261 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2262 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2263 | goto connection_empty; |
| 2264 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2265 | missing_bytes = wanted - socket->b.n; |
| 2266 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2267 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2268 | len1 = missing_bytes; |
| 2269 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2270 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2271 | } |
| 2272 | |
| 2273 | len = len1; |
| 2274 | |
| 2275 | luaL_addlstring(&socket->b, blk1, len1); |
| 2276 | if (nblk == 2) { |
| 2277 | len += len2; |
| 2278 | luaL_addlstring(&socket->b, blk2, len2); |
| 2279 | } |
| 2280 | |
| 2281 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2282 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2283 | |
| 2284 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2285 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2286 | |
| 2287 | /* If the pattern reclaim to read all the data |
| 2288 | * in the connection, got out. |
| 2289 | */ |
| 2290 | if (wanted == HLSR_READ_ALL) |
| 2291 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2292 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2293 | goto connection_empty; |
| 2294 | |
| 2295 | /* Return result. */ |
| 2296 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2297 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2298 | return 1; |
| 2299 | |
| 2300 | connection_closed: |
| 2301 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2302 | xref_unlock(&socket->xref, peer); |
| 2303 | |
| 2304 | no_peer: |
| 2305 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2306 | /* If the buffer containds data. */ |
| 2307 | if (socket->b.n > 0) { |
| 2308 | luaL_pushresult(&socket->b); |
| 2309 | return 1; |
| 2310 | } |
| 2311 | lua_pushnil(L); |
| 2312 | lua_pushstring(L, "connection closed."); |
| 2313 | return 2; |
| 2314 | |
| 2315 | connection_empty: |
| 2316 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2317 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_read, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2318 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2319 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2320 | } |
| 2321 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2322 | 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] | 2323 | return 0; |
| 2324 | } |
| 2325 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2326 | /* This Lua function gets two parameters. The first one can be string |
| 2327 | * or a number. If the string is "*l", the user requires one line. If |
| 2328 | * 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] | 2329 | * If the value is a number, the user require a number of bytes equal |
| 2330 | * to the value. The default value is "*l" (a line). |
| 2331 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2332 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2333 | * integer takes this values: |
| 2334 | * -1 : read a line |
| 2335 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2336 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2337 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2338 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2339 | * concatenated with the read data. |
| 2340 | */ |
| 2341 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2342 | { |
| 2343 | int wanted = HLSR_READ_LINE; |
| 2344 | const char *pattern; |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2345 | int lastarg, type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2346 | char *error; |
| 2347 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2348 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2349 | |
| 2350 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2351 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2352 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2353 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2354 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2355 | /* Check if we run on the same thread than the xreator thread. |
| 2356 | * We cannot access to the socket if the thread is different. |
| 2357 | */ |
| 2358 | if (socket->tid != tid) |
| 2359 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2360 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2361 | /* check for pattern. */ |
| 2362 | if (lua_gettop(L) >= 2) { |
| 2363 | type = lua_type(L, 2); |
| 2364 | if (type == LUA_TSTRING) { |
| 2365 | pattern = lua_tostring(L, 2); |
| 2366 | if (strcmp(pattern, "*a") == 0) |
| 2367 | wanted = HLSR_READ_ALL; |
| 2368 | else if (strcmp(pattern, "*l") == 0) |
| 2369 | wanted = HLSR_READ_LINE; |
| 2370 | else { |
| 2371 | wanted = strtoll(pattern, &error, 10); |
| 2372 | if (*error != '\0') |
| 2373 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2374 | } |
| 2375 | } |
| 2376 | else if (type == LUA_TNUMBER) { |
| 2377 | wanted = lua_tointeger(L, 2); |
| 2378 | if (wanted < 0) |
| 2379 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2380 | } |
| 2381 | } |
| 2382 | |
| 2383 | /* Set pattern. */ |
| 2384 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2385 | |
| 2386 | /* Check if we would replace the top by itself. */ |
| 2387 | if (lua_gettop(L) != 2) |
| 2388 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2389 | |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2390 | /* Save index of the top of the stack because since buffers are used, it |
| 2391 | * may change |
| 2392 | */ |
| 2393 | lastarg = lua_gettop(L); |
| 2394 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2395 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2396 | luaL_buffinit(L, &socket->b); |
| 2397 | |
| 2398 | /* Check prefix. */ |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2399 | if (lastarg >= 3) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2400 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2401 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2402 | pattern = lua_tolstring(L, 3, &len); |
| 2403 | luaL_addlstring(&socket->b, pattern, len); |
| 2404 | } |
| 2405 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2406 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2407 | } |
| 2408 | |
| 2409 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2410 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2411 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2412 | 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] | 2413 | { |
| 2414 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2415 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2416 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2417 | struct appctx *appctx; |
| 2418 | size_t buf_len; |
| 2419 | const char *buf; |
| 2420 | int len; |
| 2421 | int send_len; |
| 2422 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2423 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2424 | struct stream *s; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2425 | struct stconn *sc; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2426 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2427 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2428 | hlua = hlua_gethlua(L); |
| 2429 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2430 | /* Check if this lua stack is schedulable. */ |
| 2431 | if (!hlua || !hlua->task) |
| 2432 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2433 | "'frontend', 'backend' or 'task'")); |
| 2434 | |
| 2435 | /* Get object */ |
| 2436 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2437 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2438 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2439 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2440 | /* Check if we run on the same thread than the xreator thread. |
| 2441 | * We cannot access to the socket if the thread is different. |
| 2442 | */ |
| 2443 | if (socket->tid != tid) |
| 2444 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2445 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2446 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2447 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2448 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2449 | lua_pushinteger(L, -1); |
| 2450 | return 1; |
| 2451 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2452 | |
| 2453 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2454 | appctx = csk_ctx->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2455 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2456 | s = __sc_strm(sc); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2457 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2458 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2459 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2460 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2461 | lua_pushinteger(L, -1); |
| 2462 | return 1; |
| 2463 | } |
| 2464 | |
| 2465 | /* Update the input buffer data. */ |
| 2466 | buf += sent; |
| 2467 | send_len = buf_len - sent; |
| 2468 | |
| 2469 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2470 | if (sent >= buf_len) { |
| 2471 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2472 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2473 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2474 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2475 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2476 | * the request buffer if its not required. |
| 2477 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2478 | if (s->req.buf.size == 0) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2479 | if (!sc_alloc_ibuf(sc, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2480 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2481 | } |
| 2482 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2483 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2484 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2485 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2486 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2487 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2488 | |
| 2489 | /* send data */ |
| 2490 | if (len < send_len) |
| 2491 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2492 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2493 | |
| 2494 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2495 | * the data" (-2) are not expected because the available length |
| 2496 | * is tested. |
| 2497 | * Other unknown error are also not expected. |
| 2498 | */ |
| 2499 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2500 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2501 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2502 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2503 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2504 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2505 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2506 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2507 | return 1; |
| 2508 | } |
| 2509 | |
| 2510 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2511 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2512 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2513 | s->req.rex = TICK_ETERNITY; |
| 2514 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2515 | |
| 2516 | /* Update length sent. */ |
| 2517 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2518 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2519 | |
| 2520 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2521 | if (sent + len >= buf_len) { |
| 2522 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2523 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2524 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2525 | |
| 2526 | hlua_socket_write_yield_return: |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2527 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2528 | xref_unlock(&socket->xref, peer); |
| 2529 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2530 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2531 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2532 | 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] | 2533 | return 0; |
| 2534 | } |
| 2535 | |
| 2536 | /* This function initiate the send of data. It just check the input |
| 2537 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2538 | * 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] | 2539 | * "hlua_socket_write_yield" that can yield. |
| 2540 | * |
| 2541 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2542 | * the associated object. The second is a string buffer. The third is |
| 2543 | * a facultative integer that represents where is the buffer position |
| 2544 | * of the start of the data that can send. The first byte is the |
| 2545 | * position "1". The default value is "1". The fourth argument is a |
| 2546 | * facultative integer that represents where is the buffer position |
| 2547 | * of the end of the data that can send. The default is the last byte. |
| 2548 | */ |
| 2549 | static int hlua_socket_send(struct lua_State *L) |
| 2550 | { |
| 2551 | int i; |
| 2552 | int j; |
| 2553 | const char *buf; |
| 2554 | size_t buf_len; |
| 2555 | |
| 2556 | /* Check number of arguments. */ |
| 2557 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2558 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2559 | |
| 2560 | /* Get the string. */ |
| 2561 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2562 | |
| 2563 | /* Get and check j. */ |
| 2564 | if (lua_gettop(L) == 4) { |
| 2565 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2566 | if (j < 0) |
| 2567 | j = buf_len + j + 1; |
| 2568 | if (j > buf_len) |
| 2569 | j = buf_len + 1; |
| 2570 | lua_pop(L, 1); |
| 2571 | } |
| 2572 | else |
| 2573 | j = buf_len; |
| 2574 | |
| 2575 | /* Get and check i. */ |
| 2576 | if (lua_gettop(L) == 3) { |
| 2577 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2578 | if (i < 0) |
| 2579 | i = buf_len + i + 1; |
| 2580 | if (i > buf_len) |
| 2581 | i = buf_len + 1; |
| 2582 | lua_pop(L, 1); |
| 2583 | } else |
| 2584 | i = 1; |
| 2585 | |
| 2586 | /* Check bth i and j. */ |
| 2587 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2588 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2589 | return 1; |
| 2590 | } |
| 2591 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2592 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2593 | return 1; |
| 2594 | } |
| 2595 | if (i == 0) |
| 2596 | i = 1; |
| 2597 | if (j == 0) |
| 2598 | j = 1; |
| 2599 | |
| 2600 | /* Pop the string. */ |
| 2601 | lua_pop(L, 1); |
| 2602 | |
| 2603 | /* Update the buffer length. */ |
| 2604 | buf += i - 1; |
| 2605 | buf_len = j - i + 1; |
| 2606 | lua_pushlstring(L, buf, buf_len); |
| 2607 | |
| 2608 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2609 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2610 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2611 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2612 | } |
| 2613 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2614 | #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] | 2615 | __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] | 2616 | { |
| 2617 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2618 | int ret; |
| 2619 | int len; |
| 2620 | char *p; |
| 2621 | |
| 2622 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2623 | if (ret <= 0) { |
| 2624 | lua_pushnil(L); |
| 2625 | return 1; |
| 2626 | } |
| 2627 | |
| 2628 | if (ret == AF_UNIX) { |
| 2629 | lua_pushstring(L, buffer+1); |
| 2630 | return 1; |
| 2631 | } |
| 2632 | else if (ret == AF_INET6) { |
| 2633 | buffer[0] = '['; |
| 2634 | len = strlen(buffer); |
| 2635 | buffer[len] = ']'; |
| 2636 | len++; |
| 2637 | buffer[len] = ':'; |
| 2638 | len++; |
| 2639 | p = buffer; |
| 2640 | } |
| 2641 | else if (ret == AF_INET) { |
| 2642 | p = buffer + 1; |
| 2643 | len = strlen(p); |
| 2644 | p[len] = ':'; |
| 2645 | len++; |
| 2646 | } |
| 2647 | else { |
| 2648 | lua_pushnil(L); |
| 2649 | return 1; |
| 2650 | } |
| 2651 | |
| 2652 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2653 | lua_pushnil(L); |
| 2654 | return 1; |
| 2655 | } |
| 2656 | |
| 2657 | lua_pushstring(L, p); |
| 2658 | return 1; |
| 2659 | } |
| 2660 | |
| 2661 | /* Returns information about the peer of the connection. */ |
| 2662 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2663 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2664 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2665 | struct xref *peer; |
| 2666 | struct appctx *appctx; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2667 | struct stconn *sc; |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2668 | const struct sockaddr_storage *dst; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2669 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2670 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2671 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2672 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2673 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2674 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2675 | /* Check if we run on the same thread than the xreator thread. |
| 2676 | * We cannot access to the socket if the thread is different. |
| 2677 | */ |
| 2678 | if (socket->tid != tid) |
| 2679 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2680 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2681 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2682 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2683 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2684 | lua_pushnil(L); |
| 2685 | return 1; |
| 2686 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2687 | |
| 2688 | appctx = container_of(peer, struct hlua_csk_ctx, xref)->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2689 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2690 | dst = sc_dst(sc_opposite(sc)); |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2691 | if (!dst) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2692 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2693 | lua_pushnil(L); |
| 2694 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2695 | } |
| 2696 | |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2697 | ret = MAY_LJMP(hlua_socket_info(L, dst)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2698 | xref_unlock(&socket->xref, peer); |
| 2699 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2700 | } |
| 2701 | |
| 2702 | /* Returns information about my connection side. */ |
| 2703 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2704 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2705 | struct hlua_socket *socket; |
| 2706 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2707 | struct appctx *appctx; |
| 2708 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2709 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2710 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2711 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2712 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2713 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2714 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2715 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2716 | /* Check if we run on the same thread than the xreator thread. |
| 2717 | * We cannot access to the socket if the thread is different. |
| 2718 | */ |
| 2719 | if (socket->tid != tid) |
| 2720 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2721 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2722 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2723 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2724 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2725 | lua_pushnil(L); |
| 2726 | return 1; |
| 2727 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2728 | |
| 2729 | appctx = container_of(peer, struct hlua_csk_ctx, xref)->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2730 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2731 | |
Willy Tarreau | fd9417b | 2022-05-18 16:23:22 +0200 | [diff] [blame] | 2732 | conn = sc_conn(s->scb); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2733 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2734 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2735 | lua_pushnil(L); |
| 2736 | return 1; |
| 2737 | } |
| 2738 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2739 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2740 | xref_unlock(&socket->xref, peer); |
| 2741 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2742 | } |
| 2743 | |
| 2744 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2745 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2746 | .obj_type = OBJ_TYPE_APPLET, |
| 2747 | .name = "<LUA_TCP>", |
| 2748 | .fct = hlua_socket_handler, |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2749 | .init = hlua_socket_init, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2750 | .release = hlua_socket_release, |
| 2751 | }; |
| 2752 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2753 | __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] | 2754 | { |
| 2755 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2756 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2757 | struct xref *peer; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2758 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2759 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2760 | struct stream *s; |
| 2761 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2762 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2763 | hlua = hlua_gethlua(L); |
| 2764 | if (!hlua) |
| 2765 | return 0; |
| 2766 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2767 | /* Check if we run on the same thread than the xreator thread. |
| 2768 | * We cannot access to the socket if the thread is different. |
| 2769 | */ |
| 2770 | if (socket->tid != tid) |
| 2771 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2772 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2773 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2774 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2775 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2776 | lua_pushnil(L); |
| 2777 | lua_pushstring(L, "Can't connect"); |
| 2778 | return 2; |
| 2779 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2780 | |
| 2781 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2782 | appctx = csk_ctx->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2783 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2784 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2785 | /* Check if we run on the same thread than the xreator thread. |
| 2786 | * We cannot access to the socket if the thread is different. |
| 2787 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2788 | if (socket->tid != tid) { |
| 2789 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2790 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2791 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2792 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2793 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2794 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2795 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2796 | lua_pushnil(L); |
| 2797 | lua_pushstring(L, "Can't connect"); |
| 2798 | return 2; |
| 2799 | } |
| 2800 | |
Willy Tarreau | 8e7c6e6 | 2022-05-18 17:58:02 +0200 | [diff] [blame] | 2801 | appctx = __sc_appctx(s->scf); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2802 | |
| 2803 | /* Check for connection established. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2804 | if (csk_ctx->connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2805 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2806 | lua_pushinteger(L, 1); |
| 2807 | return 1; |
| 2808 | } |
| 2809 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2810 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2811 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2812 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2813 | } |
| 2814 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2815 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2816 | return 0; |
| 2817 | } |
| 2818 | |
| 2819 | /* This function fail or initite the connection. */ |
| 2820 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2821 | { |
| 2822 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2823 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2824 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2825 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2826 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2827 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2828 | int low, high; |
| 2829 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2830 | struct xref *peer; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2831 | struct stconn *sc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2832 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2833 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2834 | if (lua_gettop(L) < 2) |
| 2835 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2836 | |
| 2837 | /* Get args. */ |
| 2838 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2839 | |
| 2840 | /* Check if we run on the same thread than the xreator thread. |
| 2841 | * We cannot access to the socket if the thread is different. |
| 2842 | */ |
| 2843 | if (socket->tid != tid) |
| 2844 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2845 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2846 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2847 | if (lua_gettop(L) >= 3) { |
| 2848 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2849 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2850 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2851 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2852 | * that are not enclosed within square brackets. |
| 2853 | */ |
| 2854 | if (port > 0) { |
| 2855 | luaL_buffinit(L, &b); |
| 2856 | luaL_addstring(&b, ip); |
| 2857 | luaL_addchar(&b, ':'); |
| 2858 | luaL_pushresult(&b); |
| 2859 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2860 | } |
| 2861 | } |
| 2862 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2863 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2864 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2865 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2866 | lua_pushnil(L); |
| 2867 | return 1; |
| 2868 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2869 | |
| 2870 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2871 | 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] | 2872 | if (!addr) { |
| 2873 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2874 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2875 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2876 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2877 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2878 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2879 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2880 | if (port == -1) { |
| 2881 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2882 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2883 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2884 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2885 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2886 | if (port == -1) { |
| 2887 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2888 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2889 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2890 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2891 | } |
| 2892 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2893 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2894 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2895 | appctx = csk_ctx->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2896 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2897 | s = __sc_strm(sc); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2898 | |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2899 | if (!sockaddr_alloc(&sc_opposite(sc)->dst, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2900 | xref_unlock(&socket->xref, peer); |
| 2901 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2902 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2903 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2904 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2905 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2906 | if (!hlua) |
| 2907 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2908 | |
| 2909 | /* inform the stream that we want to be notified whenever the |
| 2910 | * connection completes. |
| 2911 | */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 2912 | applet_need_more_data(appctx); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 2913 | applet_have_more_data(appctx); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2914 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2915 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2916 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2917 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2918 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2919 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2920 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2921 | } |
| 2922 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2923 | |
| 2924 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2925 | /* Return yield waiting for connection. */ |
| 2926 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2927 | 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] | 2928 | |
| 2929 | return 0; |
| 2930 | } |
| 2931 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2932 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2933 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2934 | { |
| 2935 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2936 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2937 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2938 | |
| 2939 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2940 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2941 | |
| 2942 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2943 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2944 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2945 | lua_pushnil(L); |
| 2946 | return 1; |
| 2947 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2948 | |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2949 | s = appctx_strm(container_of(peer, struct hlua_csk_ctx, xref)->appctx); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2950 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2951 | s->target = &socket_ssl->obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2952 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2953 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2954 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2955 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2956 | |
| 2957 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2958 | { |
| 2959 | return 0; |
| 2960 | } |
| 2961 | |
| 2962 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2963 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2964 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2965 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2966 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2967 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2968 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2969 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2970 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2971 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2972 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2973 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2974 | /* convert the timeout to millis */ |
| 2975 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2976 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2977 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2978 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2979 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2980 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2981 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2982 | 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] | 2983 | |
| 2984 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2985 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2986 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2987 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2988 | /* Check if we run on the same thread than the xreator thread. |
| 2989 | * We cannot access to the socket if the thread is different. |
| 2990 | */ |
| 2991 | if (socket->tid != tid) |
| 2992 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2993 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2994 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2995 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2996 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2997 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2998 | WILL_LJMP(lua_error(L)); |
| 2999 | return 0; |
| 3000 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3001 | |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 3002 | s = appctx_strm(container_of(peer, struct hlua_csk_ctx, xref)->appctx); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3003 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 3004 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3005 | s->req.rto = tmout; |
| 3006 | s->req.wto = tmout; |
| 3007 | s->res.rto = tmout; |
| 3008 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 3009 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 3010 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 3011 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 3012 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 3013 | |
| 3014 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 3015 | task_queue(s->task); |
| 3016 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3017 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3018 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 3019 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 3020 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3021 | } |
| 3022 | |
| 3023 | __LJMP static int hlua_socket_new(lua_State *L) |
| 3024 | { |
| 3025 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3026 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3027 | struct appctx *appctx; |
| 3028 | |
| 3029 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3030 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3031 | hlua_pusherror(L, "socket: full stack"); |
| 3032 | goto out_fail_conf; |
| 3033 | } |
| 3034 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3035 | /* Create the object: obj[0] = userdata. */ |
| 3036 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3037 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3038 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3039 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 3040 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3041 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3042 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3043 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 3044 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 3045 | goto out_fail_conf; |
| 3046 | } |
| 3047 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3048 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3049 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 3050 | lua_setmetatable(L, -2); |
| 3051 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3052 | /* Create the applet context */ |
Christopher Faulet | 6095d57 | 2022-05-16 17:09:48 +0200 | [diff] [blame] | 3053 | appctx = appctx_new_here(&update_applet, NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3054 | if (!appctx) { |
| 3055 | hlua_pusherror(L, "socket: out of memory"); |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 3056 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3057 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3058 | ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
| 3059 | ctx->connected = 0; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3060 | ctx->die = 0; |
| 3061 | LIST_INIT(&ctx->wake_on_write); |
| 3062 | LIST_INIT(&ctx->wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 3063 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 3064 | if (appctx_init(appctx) == -1) { |
| 3065 | hlua_pusherror(L, "socket: fail to init applet."); |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 3066 | goto out_fail_appctx; |
| 3067 | } |
| 3068 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3069 | /* Initialise cross reference between stream and Lua socket object. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3070 | xref_create(&socket->xref, &ctx->xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3071 | return 1; |
| 3072 | |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 3073 | out_fail_appctx: |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 3074 | appctx_free_on_early_error(appctx); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3075 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3076 | WILL_LJMP(lua_error(L)); |
| 3077 | return 0; |
| 3078 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 3079 | |
| 3080 | /* |
| 3081 | * |
| 3082 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3083 | * Class Channel |
| 3084 | * |
| 3085 | * |
| 3086 | */ |
| 3087 | |
| 3088 | /* Returns the struct hlua_channel join to the class channel in the |
| 3089 | * stack entry "ud" or throws an argument error. |
| 3090 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3091 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3092 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3093 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3094 | } |
| 3095 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3096 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 3097 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3098 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 3099 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3100 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3101 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3102 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3103 | return 0; |
| 3104 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3105 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3106 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3107 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3108 | |
| 3109 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3110 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 3111 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3112 | return 1; |
| 3113 | } |
| 3114 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3115 | /* Helper function returning a filter attached to a channel at the position <ud> |
| 3116 | * 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] | 3117 | * filter is attached, NULL is returned and <offset> and <len> are not |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3118 | * initialized. |
| 3119 | */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3120 | 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] | 3121 | { |
| 3122 | struct filter *filter = NULL; |
| 3123 | |
| 3124 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 3125 | struct hlua_flt_ctx *flt_ctx; |
| 3126 | |
| 3127 | filter = lua_touserdata (L, -1); |
| 3128 | flt_ctx = filter->ctx; |
| 3129 | if (hlua_filter_from_payload(filter)) { |
| 3130 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 3131 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 3132 | } |
| 3133 | } |
| 3134 | |
| 3135 | lua_pop(L, 1); |
| 3136 | return filter; |
| 3137 | } |
| 3138 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3139 | /* Copies <len> bytes of data present in the channel's buffer, starting at the |
| 3140 | * 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] | 3141 | * responsibility to ensure <len> and <offset> are valid. It always return the |
| 3142 | * length of the built string. <len> may be 0, in this case, an empty string is |
| 3143 | * created and 0 is returned. |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3144 | */ |
| 3145 | 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] | 3146 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3147 | size_t block1, block2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3148 | luaL_Buffer b; |
| 3149 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3150 | block1 = len; |
| 3151 | if (block1 > b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset))) |
| 3152 | block1 = b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset)); |
| 3153 | block2 = len - block1; |
| 3154 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3155 | luaL_buffinit(L, &b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3156 | luaL_addlstring(&b, b_peek(&chn->buf, offset), block1); |
| 3157 | if (block2) |
| 3158 | luaL_addlstring(&b, b_orig(&chn->buf), block2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3159 | luaL_pushresult(&b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3160 | return len; |
| 3161 | } |
| 3162 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3163 | /* Inserts the string <str> to the channel's buffer at the offset <offset>. This |
| 3164 | * function returns -1 if data cannot be copied. Otherwise, it returns the |
| 3165 | * number of bytes copied. |
| 3166 | */ |
| 3167 | static int _hlua_channel_insert(struct channel *chn, lua_State *L, struct ist str, size_t offset) |
| 3168 | { |
| 3169 | int ret = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3170 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3171 | /* Nothing to do, just return */ |
| 3172 | if (unlikely(istlen(str) == 0)) |
| 3173 | goto end; |
| 3174 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3175 | if (istlen(str) > c_room(chn)) { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3176 | ret = -1; |
| 3177 | goto end; |
| 3178 | } |
| 3179 | ret = b_insert_blk(&chn->buf, offset, istptr(str), istlen(str)); |
| 3180 | |
| 3181 | end: |
| 3182 | return ret; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3183 | } |
| 3184 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3185 | /* Removes <len> bytes of data at the absolute position <offset>. |
| 3186 | */ |
| 3187 | static void _hlua_channel_delete(struct channel *chn, size_t offset, size_t len) |
| 3188 | { |
| 3189 | size_t end = offset + len; |
| 3190 | |
| 3191 | if (b_peek(&chn->buf, end) != b_tail(&chn->buf)) |
| 3192 | b_move(&chn->buf, b_peek_ofs(&chn->buf, end), |
| 3193 | b_data(&chn->buf) - end, -len); |
| 3194 | b_sub(&chn->buf, len); |
| 3195 | } |
| 3196 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3197 | /* Copies input data in the channel's buffer. It is possible to set a specific |
| 3198 | * offset (0 by default) and a length (all remaining input data starting for the |
| 3199 | * offset by default). If there is not enough input data and more data can be |
| 3200 | * received, this function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3201 | * |
| 3202 | * From an action, All input data are considered. For a filter, the offset and |
| 3203 | * 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] | 3204 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3205 | __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] | 3206 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3207 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3208 | struct filter *filter; |
| 3209 | size_t input, output; |
| 3210 | int offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3211 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3212 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3213 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3214 | output = co_data(chn); |
| 3215 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3216 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3217 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3218 | if (filter && !hlua_filter_from_payload(filter)) |
| 3219 | WILL_LJMP(lua_error(L)); |
| 3220 | |
| 3221 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3222 | if (lua_gettop(L) > 1) { |
| 3223 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3224 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3225 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3226 | offset += output; |
| 3227 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3228 | lua_pushfstring(L, "offset out of range."); |
| 3229 | WILL_LJMP(lua_error(L)); |
| 3230 | } |
| 3231 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3232 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3233 | if (lua_gettop(L) == 3) { |
| 3234 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3235 | if (!len) |
| 3236 | goto dup; |
| 3237 | if (len == -1) |
| 3238 | len = global.tune.bufsize; |
| 3239 | if (len < 0) { |
| 3240 | lua_pushfstring(L, "length out of range."); |
| 3241 | WILL_LJMP(lua_error(L)); |
| 3242 | } |
| 3243 | } |
| 3244 | |
Christopher Faulet | 0ae2e63 | 2023-01-10 15:29:54 +0100 | [diff] [blame] | 3245 | /* Wait for more data if possible if no length was specified and there |
| 3246 | * is no data or not enough data was received. |
| 3247 | */ |
| 3248 | if (!len || offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3249 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3250 | /* Yield waiting for more data, as requested */ |
| 3251 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_data_yield, TICK_ETERNITY, 0)); |
| 3252 | } |
Christopher Faulet | 0ae2e63 | 2023-01-10 15:29:54 +0100 | [diff] [blame] | 3253 | |
| 3254 | /* Return 'nil' if there is no data and the channel can't receive more data */ |
| 3255 | if (!len) { |
| 3256 | lua_pushnil(L); |
| 3257 | return -1; |
| 3258 | } |
| 3259 | |
| 3260 | /* Otherwise, return all data */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3261 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3262 | } |
| 3263 | |
| 3264 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3265 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3266 | return 1; |
| 3267 | } |
| 3268 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3269 | /* Copies the first line (including the trailing LF) of input data in the |
| 3270 | * channel's buffer. It is possible to set a specific offset (0 by default) and |
| 3271 | * a length (all remaining input data starting for the offset by default). If |
| 3272 | * there is not enough input data and more data can be received, the function |
| 3273 | * yields. If a length is explicitly specified, no more data are |
| 3274 | * copied. Otherwise, if no LF is found and more data can be received, this |
| 3275 | * function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3276 | * |
| 3277 | * From an action, All input data are considered. For a filter, the offset and |
| 3278 | * 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] | 3279 | */ |
| 3280 | __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] | 3281 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3282 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3283 | struct filter *filter; |
| 3284 | size_t l, input, output; |
| 3285 | int offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3286 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3287 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3288 | output = co_data(chn); |
| 3289 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3290 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3291 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3292 | if (filter && !hlua_filter_from_payload(filter)) |
| 3293 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3294 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3295 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3296 | if (lua_gettop(L) > 1) { |
| 3297 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3298 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3299 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3300 | offset += output; |
| 3301 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3302 | lua_pushfstring(L, "offset out of range."); |
| 3303 | WILL_LJMP(lua_error(L)); |
| 3304 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3305 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3306 | |
| 3307 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3308 | if (lua_gettop(L) == 3) { |
| 3309 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3310 | if (!len) |
| 3311 | goto dup; |
| 3312 | if (len == -1) |
| 3313 | len = global.tune.bufsize; |
| 3314 | if (len < 0) { |
| 3315 | lua_pushfstring(L, "length out of range."); |
| 3316 | WILL_LJMP(lua_error(L)); |
| 3317 | } |
| 3318 | } |
| 3319 | |
| 3320 | for (l = 0; l < len; l++) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3321 | if (l + offset >= output + input) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3322 | break; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3323 | if (*(b_peek(&chn->buf, offset + l)) == '\n') { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3324 | len = l+1; |
| 3325 | goto dup; |
| 3326 | } |
| 3327 | } |
| 3328 | |
Christopher Faulet | 0ae2e63 | 2023-01-10 15:29:54 +0100 | [diff] [blame] | 3329 | /* Wait for more data if possible if no line is found and no length was |
| 3330 | * specified or not enough data was received. |
| 3331 | */ |
| 3332 | if (lua_gettop(L) != 3 || offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3333 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3334 | /* Yield waiting for more data */ |
| 3335 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_line_yield, TICK_ETERNITY, 0)); |
| 3336 | } |
Christopher Faulet | 0ae2e63 | 2023-01-10 15:29:54 +0100 | [diff] [blame] | 3337 | |
| 3338 | /* Return 'nil' if there is no data and the channel can't receive more data */ |
| 3339 | if (!len) { |
| 3340 | lua_pushnil(L); |
| 3341 | return -1; |
| 3342 | } |
| 3343 | |
| 3344 | /* Otherwise, return all data */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3345 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3346 | } |
| 3347 | |
| 3348 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3349 | _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3350 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3351 | } |
| 3352 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3353 | /* [ DEPRECATED ] |
| 3354 | * |
| 3355 | * Duplicate all input data foud in the channel's buffer. The data are not |
| 3356 | * removed from the buffer. This function relies on _hlua_channel_dup(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3357 | * |
| 3358 | * From an action, All input data are considered. For a filter, the offset and |
| 3359 | * 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] | 3360 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3361 | __LJMP static int hlua_channel_dup(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3362 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3363 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3364 | struct filter *filter; |
| 3365 | size_t offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3366 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3367 | MAY_LJMP(check_args(L, 1, "dup")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3368 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3369 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3370 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3371 | WILL_LJMP(lua_error(L)); |
| 3372 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3373 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3374 | offset = co_data(chn); |
| 3375 | len = ci_data(chn); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3376 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3377 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3378 | if (filter && !hlua_filter_from_payload(filter)) |
| 3379 | WILL_LJMP(lua_error(L)); |
| 3380 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3381 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3382 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3383 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3384 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3385 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3386 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3387 | return 1; |
| 3388 | } |
| 3389 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3390 | /* [ DEPRECATED ] |
| 3391 | * |
| 3392 | * Get all input data foud in the channel's buffer. The data are removed from |
| 3393 | * the buffer after the copy. This function relies on _hlua_channel_dup() and |
| 3394 | * _hlua_channel_delete(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3395 | * |
| 3396 | * From an action, All input data are considered. For a filter, the offset and |
| 3397 | * 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] | 3398 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3399 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3400 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3401 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3402 | struct filter *filter; |
| 3403 | size_t offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3404 | int ret; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3405 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3406 | MAY_LJMP(check_args(L, 1, "get")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3407 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3408 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3409 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3410 | WILL_LJMP(lua_error(L)); |
| 3411 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3412 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3413 | offset = co_data(chn); |
| 3414 | len = ci_data(chn); |
| 3415 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3416 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3417 | if (filter && !hlua_filter_from_payload(filter)) |
| 3418 | WILL_LJMP(lua_error(L)); |
| 3419 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3420 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3421 | lua_pushnil(L); |
| 3422 | return 1; |
| 3423 | } |
| 3424 | |
| 3425 | ret = _hlua_channel_dup(chn, L, offset, len); |
| 3426 | _hlua_channel_delete(chn, offset, ret); |
| 3427 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3428 | } |
| 3429 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3430 | /* This functions consumes and returns one line. If the channel is closed, |
| 3431 | * 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] | 3432 | * 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] | 3433 | * value. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3434 | * |
| 3435 | * From an action, All input data are considered. For a filter, the offset and |
| 3436 | * 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] | 3437 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3438 | __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] | 3439 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3440 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3441 | struct filter *filter; |
| 3442 | size_t l, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3443 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3444 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3445 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3446 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3447 | offset = co_data(chn); |
| 3448 | len = ci_data(chn); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3449 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3450 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3451 | if (filter && !hlua_filter_from_payload(filter)) |
| 3452 | WILL_LJMP(lua_error(L)); |
| 3453 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3454 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3455 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3456 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3457 | } |
| 3458 | |
| 3459 | for (l = 0; l < len; l++) { |
| 3460 | if (*(b_peek(&chn->buf, offset+l)) == '\n') { |
| 3461 | len = l+1; |
| 3462 | goto dup; |
| 3463 | } |
| 3464 | } |
| 3465 | |
| 3466 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3467 | /* Yield waiting for more data */ |
| 3468 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0)); |
| 3469 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3470 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3471 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3472 | ret = _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3473 | _hlua_channel_delete(chn, offset, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3474 | return 1; |
| 3475 | } |
| 3476 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3477 | /* [ DEPRECATED ] |
| 3478 | * |
| 3479 | * Check arguments for the function "hlua_channel_getline_yield". |
| 3480 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3481 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3482 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3483 | struct channel *chn; |
| 3484 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3485 | MAY_LJMP(check_args(L, 1, "getline")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3486 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3487 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3488 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3489 | WILL_LJMP(lua_error(L)); |
| 3490 | } |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3491 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3492 | } |
| 3493 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3494 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3495 | * available input data are returned. The offset may be negactive to start from |
| 3496 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3497 | * size. |
| 3498 | */ |
| 3499 | __LJMP static int hlua_channel_get_data(lua_State *L) |
| 3500 | { |
| 3501 | struct channel *chn; |
| 3502 | |
| 3503 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3504 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 3505 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3506 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3507 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3508 | WILL_LJMP(lua_error(L)); |
| 3509 | } |
| 3510 | return MAY_LJMP(hlua_channel_get_data_yield(L, 0, 0)); |
| 3511 | } |
| 3512 | |
| 3513 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3514 | * available input data are returned. The offset may be negactive to start from |
| 3515 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3516 | * size. |
| 3517 | */ |
| 3518 | __LJMP static int hlua_channel_get_line(lua_State *L) |
| 3519 | { |
| 3520 | struct channel *chn; |
| 3521 | |
| 3522 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3523 | WILL_LJMP(luaL_error(L, "'line' expects at most 2 arguments")); |
| 3524 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3525 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3526 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3527 | WILL_LJMP(lua_error(L)); |
| 3528 | } |
| 3529 | return MAY_LJMP(hlua_channel_get_line_yield(L, 0, 0)); |
| 3530 | } |
| 3531 | |
| 3532 | /* Appends a string into the input side of channel. It returns the length of the |
| 3533 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3534 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3535 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3536 | * |
| 3537 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3538 | */ |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3539 | __LJMP static int hlua_channel_append(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3540 | { |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3541 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3542 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3543 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3544 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3545 | int ret; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3546 | |
| 3547 | MAY_LJMP(check_args(L, 2, "append")); |
| 3548 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3549 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3550 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3551 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3552 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3553 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3554 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3555 | offset = co_data(chn); |
| 3556 | len = ci_data(chn); |
| 3557 | |
| 3558 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3559 | if (filter && !hlua_filter_from_payload(filter)) |
| 3560 | WILL_LJMP(lua_error(L)); |
| 3561 | |
| 3562 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3563 | if (ret > 0 && filter) { |
| 3564 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3565 | |
| 3566 | flt_update_offsets(filter, chn, ret); |
| 3567 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3568 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3569 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3570 | return 1; |
| 3571 | } |
| 3572 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3573 | /* Prepends a string into the input side of channel. It returns the length of the |
| 3574 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3575 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3576 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3577 | * |
| 3578 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3579 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3580 | __LJMP static int hlua_channel_prepend(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3581 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3582 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3583 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3584 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3585 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3586 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3587 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3588 | MAY_LJMP(check_args(L, 2, "prepend")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3589 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3590 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3591 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3592 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3593 | WILL_LJMP(lua_error(L)); |
| 3594 | } |
| 3595 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3596 | offset = co_data(chn); |
| 3597 | len = ci_data(chn); |
| 3598 | |
| 3599 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3600 | if (filter && !hlua_filter_from_payload(filter)) |
| 3601 | WILL_LJMP(lua_error(L)); |
| 3602 | |
| 3603 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3604 | if (ret > 0 && filter) { |
| 3605 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3606 | |
| 3607 | flt_update_offsets(filter, chn, ret); |
| 3608 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3609 | } |
| 3610 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3611 | lua_pushinteger(L, ret); |
| 3612 | return 1; |
| 3613 | } |
| 3614 | |
| 3615 | /* Inserts a given amount of input data at the given offset by a string |
Aurelien DARRAGON | afb7daf | 2022-10-04 12:16:05 +0200 | [diff] [blame] | 3616 | * content. By default the string is appended in front of input data. It |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3617 | * returns the length of the written string, or -1 if the channel is closed or |
| 3618 | * if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3619 | * |
| 3620 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3621 | */ |
| 3622 | __LJMP static int hlua_channel_insert_data(lua_State *L) |
| 3623 | { |
| 3624 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3625 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3626 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3627 | size_t sz, input, output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3628 | int ret, offset; |
| 3629 | |
| 3630 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 3631 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
| 3632 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3633 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3634 | |
| 3635 | output = co_data(chn); |
| 3636 | input = ci_data(chn); |
| 3637 | |
| 3638 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3639 | if (filter && !hlua_filter_from_payload(filter)) |
| 3640 | WILL_LJMP(lua_error(L)); |
| 3641 | |
Aurelien DARRAGON | afb7daf | 2022-10-04 12:16:05 +0200 | [diff] [blame] | 3642 | offset = output; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3643 | if (lua_gettop(L) > 2) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3644 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3645 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3646 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3647 | offset += output; |
Aurelien DARRAGON | afb7daf | 2022-10-04 12:16:05 +0200 | [diff] [blame] | 3648 | if (offset > output + input) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3649 | lua_pushfstring(L, "offset out of range."); |
| 3650 | WILL_LJMP(lua_error(L)); |
| 3651 | } |
| 3652 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3653 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3654 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3655 | WILL_LJMP(lua_error(L)); |
| 3656 | } |
| 3657 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3658 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3659 | if (ret > 0 && filter) { |
| 3660 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3661 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3662 | flt_update_offsets(filter, chn, ret); |
| 3663 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3664 | } |
| 3665 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3666 | lua_pushinteger(L, ret); |
| 3667 | return 1; |
| 3668 | } |
| 3669 | /* Replaces a given amount of input data at the given offset by a string |
| 3670 | * content. By default all remaining data are removed (offset = 0 and len = |
| 3671 | * -1). It returns the length of the written string, or -1 if the channel is |
| 3672 | * closed or if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3673 | * |
| 3674 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3675 | */ |
| 3676 | __LJMP static int hlua_channel_set_data(lua_State *L) |
| 3677 | { |
| 3678 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3679 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3680 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3681 | size_t sz, input, output; |
| 3682 | int ret, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3683 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3684 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 3685 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 3686 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3687 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3688 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3689 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3690 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3691 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3692 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3693 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3694 | output = co_data(chn); |
| 3695 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3696 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3697 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3698 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3699 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3700 | |
| 3701 | offset = output; |
| 3702 | if (lua_gettop(L) > 2) { |
| 3703 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3704 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3705 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3706 | offset += output; |
| 3707 | if (offset < output || offset > input + output) { |
| 3708 | lua_pushfstring(L, "offset out of range."); |
| 3709 | WILL_LJMP(lua_error(L)); |
| 3710 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3711 | } |
| 3712 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3713 | len = output + input - offset; |
| 3714 | if (lua_gettop(L) == 4) { |
| 3715 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 3716 | if (!len) |
| 3717 | goto set; |
| 3718 | if (len == -1) |
| 3719 | len = output + input - offset; |
| 3720 | if (len < 0 || offset + len > output + input) { |
| 3721 | lua_pushfstring(L, "length out of range."); |
| 3722 | WILL_LJMP(lua_error(L)); |
| 3723 | } |
| 3724 | } |
| 3725 | |
| 3726 | set: |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3727 | /* 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] | 3728 | if (sz > c_room(chn) + len) |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3729 | lua_pushinteger(L, -1); |
| 3730 | else { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3731 | _hlua_channel_delete(chn, offset, len); |
| 3732 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3733 | if (filter) { |
| 3734 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3735 | |
| 3736 | len -= (ret > 0 ? ret : 0); |
| 3737 | flt_update_offsets(filter, chn, -len); |
| 3738 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3739 | } |
| 3740 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3741 | lua_pushinteger(L, ret); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3742 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3743 | return 1; |
| 3744 | } |
| 3745 | |
| 3746 | /* 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] | 3747 | * 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] | 3748 | * the removed data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3749 | * |
| 3750 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3751 | */ |
| 3752 | __LJMP static int hlua_channel_del_data(lua_State *L) |
| 3753 | { |
| 3754 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3755 | struct filter *filter; |
| 3756 | size_t input, output; |
| 3757 | int offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3758 | |
| 3759 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3760 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
| 3761 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3762 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3763 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3764 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3765 | WILL_LJMP(lua_error(L)); |
| 3766 | } |
| 3767 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3768 | output = co_data(chn); |
| 3769 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3770 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3771 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3772 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3773 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3774 | |
| 3775 | offset = output; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 3776 | if (lua_gettop(L) > 1) { |
| 3777 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3778 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3779 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3780 | offset += output; |
| 3781 | if (offset < output || offset > input + output) { |
| 3782 | lua_pushfstring(L, "offset out of range."); |
| 3783 | WILL_LJMP(lua_error(L)); |
| 3784 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3785 | } |
| 3786 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3787 | len = output + input - offset; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 3788 | if (lua_gettop(L) == 3) { |
| 3789 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3790 | if (!len) |
| 3791 | goto end; |
| 3792 | if (len == -1) |
| 3793 | len = output + input - offset; |
| 3794 | if (len < 0 || offset + len > output + input) { |
| 3795 | lua_pushfstring(L, "length out of range."); |
| 3796 | WILL_LJMP(lua_error(L)); |
| 3797 | } |
| 3798 | } |
| 3799 | |
| 3800 | _hlua_channel_delete(chn, offset, len); |
| 3801 | if (filter) { |
| 3802 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3803 | |
| 3804 | flt_update_offsets(filter, chn, -len); |
| 3805 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3806 | } |
| 3807 | |
| 3808 | end: |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3809 | lua_pushinteger(L, len); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3810 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3811 | } |
| 3812 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3813 | /* Append data in the output side of the buffer. This data is immediately |
| 3814 | * sent. The function returns the amount of data written. If the buffer |
| 3815 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3816 | * if the channel is closed. |
| 3817 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3818 | __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] | 3819 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3820 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3821 | struct filter *filter; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3822 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3823 | size_t offset, len, sz; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3824 | int l, ret; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3825 | struct hlua *hlua; |
| 3826 | |
| 3827 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3828 | hlua = hlua_gethlua(L); |
| 3829 | if (!hlua) { |
| 3830 | lua_pushnil(L); |
| 3831 | return 1; |
| 3832 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3833 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3834 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3835 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3836 | l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3837 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3838 | offset = co_data(chn); |
| 3839 | len = ci_data(chn); |
| 3840 | |
| 3841 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3842 | if (filter && !hlua_filter_from_payload(filter)) |
| 3843 | WILL_LJMP(lua_error(L)); |
| 3844 | |
| 3845 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3846 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3847 | lua_pushinteger(L, -1); |
| 3848 | return 1; |
| 3849 | } |
| 3850 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3851 | len = c_room(chn); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3852 | if (len > sz -l) { |
| 3853 | if (filter) { |
| 3854 | lua_pushinteger(L, -1); |
| 3855 | return 1; |
| 3856 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3857 | len = sz - l; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3858 | } |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3859 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3860 | ret = _hlua_channel_insert(chn, L, ist2(str, len), offset); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3861 | if (ret == -1) { |
| 3862 | lua_pop(L, 1); |
| 3863 | lua_pushinteger(L, -1); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3864 | return 1; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3865 | } |
| 3866 | if (ret) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3867 | if (filter) { |
| 3868 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3869 | |
| 3870 | |
| 3871 | flt_update_offsets(filter, chn, ret); |
| 3872 | FLT_OFF(filter, chn) += ret; |
| 3873 | flt_ctx->cur_off[CHN_IDX(chn)] += ret; |
| 3874 | } |
| 3875 | else |
| 3876 | c_adv(chn, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3877 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3878 | l += ret; |
| 3879 | lua_pop(L, 1); |
| 3880 | lua_pushinteger(L, l); |
| 3881 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3882 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3883 | if (l < sz) { |
| 3884 | /* Yield only if the channel's output is not empty. |
| 3885 | * Otherwise it means we cannot add more data. */ |
| 3886 | if (co_data(chn) == 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3887 | return 1; |
| 3888 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3889 | /* If we are waiting for space in the response buffer, we |
| 3890 | * must set the flag WAKERESWR. This flag required the task |
| 3891 | * wake up if any activity is detected on the response buffer. |
| 3892 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3893 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3894 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3895 | else |
| 3896 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3897 | 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] | 3898 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3899 | |
| 3900 | return 1; |
| 3901 | } |
| 3902 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3903 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3904 | * yield the LUA process, and resume it without checking the |
| 3905 | * input arguments. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3906 | * |
| 3907 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3908 | */ |
| 3909 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3910 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3911 | struct channel *chn; |
| 3912 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3913 | MAY_LJMP(check_args(L, 2, "send")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3914 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3915 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3916 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3917 | WILL_LJMP(lua_error(L)); |
| 3918 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3919 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3920 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3921 | } |
| 3922 | |
| 3923 | /* This function forward and amount of butes. The data pass from |
| 3924 | * the input side of the buffer to the output side, and can be |
| 3925 | * forwarded. This function never fails. |
| 3926 | * |
| 3927 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3928 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3929 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3930 | __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] | 3931 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3932 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3933 | struct filter *filter; |
| 3934 | size_t offset, len, fwd; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3935 | int l, max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3936 | struct hlua *hlua; |
| 3937 | |
| 3938 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3939 | hlua = hlua_gethlua(L); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3940 | if (!hlua) { |
| 3941 | lua_pushnil(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3942 | return 1; |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3943 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3944 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3945 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3946 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3947 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3948 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3949 | offset = co_data(chn); |
| 3950 | len = ci_data(chn); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3951 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3952 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3953 | if (filter && !hlua_filter_from_payload(filter)) |
| 3954 | WILL_LJMP(lua_error(L)); |
| 3955 | |
| 3956 | max = fwd - l; |
| 3957 | if (max > len) |
| 3958 | max = len; |
| 3959 | |
| 3960 | if (filter) { |
| 3961 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3962 | |
| 3963 | FLT_OFF(filter, chn) += max; |
| 3964 | flt_ctx->cur_off[CHN_IDX(chn)] += max; |
| 3965 | flt_ctx->cur_len[CHN_IDX(chn)] -= max; |
| 3966 | } |
| 3967 | else |
| 3968 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3969 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3970 | l += max; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3971 | lua_pop(L, 1); |
| 3972 | lua_pushinteger(L, l); |
| 3973 | |
| 3974 | /* Check if it miss bytes to forward. */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3975 | if (l < fwd) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3976 | /* The the input channel or the output channel are closed, we |
| 3977 | * must return the amount of data forwarded. |
| 3978 | */ |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3979 | 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] | 3980 | return 1; |
| 3981 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3982 | /* If we are waiting for space data in the response buffer, we |
| 3983 | * must set the flag WAKERESWR. This flag required the task |
| 3984 | * wake up if any activity is detected on the response buffer. |
| 3985 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3986 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3987 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3988 | else |
| 3989 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3990 | |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 3991 | /* Otherwise, we can yield waiting for new data in the input side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3992 | 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] | 3993 | } |
| 3994 | |
| 3995 | return 1; |
| 3996 | } |
| 3997 | |
| 3998 | /* Just check the input and prepare the stack for the previous |
| 3999 | * function "hlua_channel_forward_yield" |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4000 | * |
| 4001 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4002 | */ |
| 4003 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 4004 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4005 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4006 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4007 | MAY_LJMP(check_args(L, 2, "forward")); |
| 4008 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4009 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 4010 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 4011 | WILL_LJMP(lua_error(L)); |
| 4012 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4013 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 4014 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4015 | } |
| 4016 | |
| 4017 | /* Just returns the number of bytes available in the input |
| 4018 | * side of the buffer. This function never fails. |
| 4019 | */ |
| 4020 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 4021 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4022 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4023 | struct filter *filter; |
| 4024 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4025 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4026 | MAY_LJMP(check_args(L, 1, "input")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4027 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4028 | |
| 4029 | output = co_data(chn); |
| 4030 | input = ci_data(chn); |
| 4031 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 4032 | if (filter || !IS_HTX_STRM(chn_strm(chn))) |
| 4033 | lua_pushinteger(L, input); |
| 4034 | else { |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 4035 | struct htx *htx = htxbuf(&chn->buf); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4036 | |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 4037 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 4038 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4039 | return 1; |
| 4040 | } |
| 4041 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 4042 | /* Returns true if the channel is full. */ |
| 4043 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 4044 | { |
| 4045 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 4046 | |
| 4047 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 4048 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 4049 | /* ignore the reserve, we are not on a producer side (ie in an |
| 4050 | * applet). |
| 4051 | */ |
| 4052 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 4053 | return 1; |
| 4054 | } |
| 4055 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4056 | /* Returns true if the channel may still receive data. */ |
| 4057 | __LJMP static int hlua_channel_may_recv(lua_State *L) |
| 4058 | { |
| 4059 | struct channel *chn; |
| 4060 | |
| 4061 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 4062 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4063 | lua_pushboolean(L, (!channel_input_closed(chn) && channel_may_recv(chn))); |
| 4064 | return 1; |
| 4065 | } |
| 4066 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 4067 | /* Returns true if the channel is the response channel. */ |
| 4068 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 4069 | { |
| 4070 | struct channel *chn; |
| 4071 | |
| 4072 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 4073 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4074 | |
| 4075 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 4076 | return 1; |
| 4077 | } |
| 4078 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4079 | /* Just returns the number of bytes available in the output |
| 4080 | * side of the buffer. This function never fails. |
| 4081 | */ |
| 4082 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 4083 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4084 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4085 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4086 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4087 | MAY_LJMP(check_args(L, 1, "output")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4088 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4089 | |
| 4090 | output = co_data(chn); |
| 4091 | input = ci_data(chn); |
| 4092 | hlua_channel_filter(L, 1, chn, &output, &input); |
| 4093 | |
| 4094 | lua_pushinteger(L, output); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4095 | return 1; |
| 4096 | } |
| 4097 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4098 | /* |
| 4099 | * |
| 4100 | * |
| 4101 | * Class Fetches |
| 4102 | * |
| 4103 | * |
| 4104 | */ |
| 4105 | |
| 4106 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4107 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4108 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4109 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4110 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4111 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4112 | } |
| 4113 | |
| 4114 | /* This function creates and push in the stack a fetch object according |
| 4115 | * with a current TXN. |
| 4116 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4117 | 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] | 4118 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4119 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4120 | |
| 4121 | /* Check stack size. */ |
| 4122 | if (!lua_checkstack(L, 3)) |
| 4123 | return 0; |
| 4124 | |
| 4125 | /* Create the object: obj[0] = userdata. |
| 4126 | * Note that the base of the Fetches object is the |
| 4127 | * transaction object. |
| 4128 | */ |
| 4129 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4130 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4131 | lua_rawseti(L, -2, 0); |
| 4132 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4133 | hsmp->s = txn->s; |
| 4134 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4135 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4136 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4137 | |
| 4138 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 4139 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 4140 | lua_setmetatable(L, -2); |
| 4141 | |
| 4142 | return 1; |
| 4143 | } |
| 4144 | |
| 4145 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 4146 | * It uses closure argument to store the associated sample-fetch. It |
| 4147 | * returns only one argument or throws an error. An error is thrown |
| 4148 | * only if an error is encountered during the argument parsing. If |
| 4149 | * the "sample-fetch" function fails, nil is returned. |
| 4150 | */ |
| 4151 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 4152 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4153 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4154 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4155 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4156 | int i; |
| 4157 | struct sample smp; |
| 4158 | |
| 4159 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4160 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4161 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4162 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4163 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4164 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 4165 | /* Check execution authorization. */ |
| 4166 | if (f->use & SMP_USE_HTTP_ANY && |
| 4167 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 4168 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 4169 | "is not available in Lua services", f->kw); |
| 4170 | WILL_LJMP(lua_error(L)); |
| 4171 | } |
| 4172 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4173 | /* Get extra arguments. */ |
| 4174 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 4175 | if (i >= ARGM_NBARGS) |
| 4176 | break; |
| 4177 | hlua_lua2arg(L, i + 2, &args[i]); |
| 4178 | } |
| 4179 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4180 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4181 | |
| 4182 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4183 | 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] | 4184 | |
| 4185 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4186 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4187 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4188 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4189 | } |
| 4190 | |
| 4191 | /* Initialise the sample. */ |
| 4192 | memset(&smp, 0, sizeof(smp)); |
| 4193 | |
| 4194 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4195 | 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] | 4196 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4197 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4198 | lua_pushstring(L, ""); |
| 4199 | else |
| 4200 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4201 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4202 | } |
| 4203 | |
| 4204 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4205 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4206 | hlua_smp2lua_str(L, &smp); |
| 4207 | else |
| 4208 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4209 | |
| 4210 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4211 | free_args(args); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4212 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4213 | |
| 4214 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4215 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4216 | WILL_LJMP(lua_error(L)); |
| 4217 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4218 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4219 | |
| 4220 | /* |
| 4221 | * |
| 4222 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4223 | * Class Converters |
| 4224 | * |
| 4225 | * |
| 4226 | */ |
| 4227 | |
| 4228 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4229 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4230 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4231 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4232 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4233 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4234 | } |
| 4235 | |
| 4236 | /* This function creates and push in the stack a Converters object |
| 4237 | * according with a current TXN. |
| 4238 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4239 | 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] | 4240 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4241 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4242 | |
| 4243 | /* Check stack size. */ |
| 4244 | if (!lua_checkstack(L, 3)) |
| 4245 | return 0; |
| 4246 | |
| 4247 | /* Create the object: obj[0] = userdata. |
| 4248 | * Note that the base of the Converters object is the |
| 4249 | * same than the TXN object. |
| 4250 | */ |
| 4251 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4252 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4253 | lua_rawseti(L, -2, 0); |
| 4254 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4255 | hsmp->s = txn->s; |
| 4256 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4257 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4258 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4259 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4260 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4261 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 4262 | lua_setmetatable(L, -2); |
| 4263 | |
| 4264 | return 1; |
| 4265 | } |
| 4266 | |
| 4267 | /* This function is an LUA binding. It is called with each converter. |
| 4268 | * It uses closure argument to store the associated converter. It |
| 4269 | * returns only one argument or throws an error. An error is thrown |
| 4270 | * only if an error is encountered during the argument parsing. If |
| 4271 | * the converter function function fails, nil is returned. |
| 4272 | */ |
| 4273 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 4274 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4275 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4276 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4277 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4278 | int i; |
| 4279 | struct sample smp; |
| 4280 | |
| 4281 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4282 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4283 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4284 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4285 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4286 | |
| 4287 | /* Get extra arguments. */ |
| 4288 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 4289 | if (i >= ARGM_NBARGS) |
| 4290 | break; |
| 4291 | hlua_lua2arg(L, i + 3, &args[i]); |
| 4292 | } |
| 4293 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4294 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4295 | |
| 4296 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4297 | 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] | 4298 | |
| 4299 | /* Run the special args checker. */ |
| 4300 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 4301 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4302 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4303 | } |
| 4304 | |
| 4305 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4306 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4307 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 4308 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4309 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4310 | } |
| 4311 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4312 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 4313 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4314 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4315 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4316 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4317 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4318 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4319 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4320 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 4321 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4322 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4323 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4324 | } |
| 4325 | |
| 4326 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4327 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4328 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4329 | lua_pushstring(L, ""); |
| 4330 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4331 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4332 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4333 | } |
| 4334 | |
| 4335 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4336 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4337 | hlua_smp2lua_str(L, &smp); |
| 4338 | else |
| 4339 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4340 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4341 | free_args(args); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4342 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4343 | |
| 4344 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4345 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4346 | WILL_LJMP(lua_error(L)); |
| 4347 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4348 | } |
| 4349 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4350 | /* |
| 4351 | * |
| 4352 | * |
| 4353 | * Class AppletTCP |
| 4354 | * |
| 4355 | * |
| 4356 | */ |
| 4357 | |
| 4358 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4359 | * a class stream, otherwise it throws an error. |
| 4360 | */ |
| 4361 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 4362 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4363 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4364 | } |
| 4365 | |
| 4366 | /* This function creates and push in the stack an Applet object |
| 4367 | * according with a current TXN. |
| 4368 | */ |
| 4369 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 4370 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4371 | struct hlua_appctx *luactx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 4372 | struct stream *s = appctx_strm(ctx); |
Christopher Faulet | 2da02ae | 2022-02-24 13:45:27 +0100 | [diff] [blame] | 4373 | struct proxy *p; |
| 4374 | |
| 4375 | ALREADY_CHECKED(s); |
| 4376 | p = s->be; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4377 | |
| 4378 | /* Check stack size. */ |
| 4379 | if (!lua_checkstack(L, 3)) |
| 4380 | return 0; |
| 4381 | |
| 4382 | /* Create the object: obj[0] = userdata. |
| 4383 | * Note that the base of the Converters object is the |
| 4384 | * same than the TXN object. |
| 4385 | */ |
| 4386 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4387 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4388 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4389 | luactx->appctx = ctx; |
| 4390 | luactx->htxn.s = s; |
| 4391 | luactx->htxn.p = p; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4392 | |
| 4393 | /* Create the "f" field that contains a list of fetches. */ |
| 4394 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4395 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4396 | return 0; |
| 4397 | lua_settable(L, -3); |
| 4398 | |
| 4399 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4400 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4401 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4402 | return 0; |
| 4403 | lua_settable(L, -3); |
| 4404 | |
| 4405 | /* Create the "c" field that contains a list of converters. */ |
| 4406 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4407 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4408 | return 0; |
| 4409 | lua_settable(L, -3); |
| 4410 | |
| 4411 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4412 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4413 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4414 | return 0; |
| 4415 | lua_settable(L, -3); |
| 4416 | |
| 4417 | /* Pop a class stream metatable and affect it to the table. */ |
| 4418 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 4419 | lua_setmetatable(L, -2); |
| 4420 | |
| 4421 | return 1; |
| 4422 | } |
| 4423 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4424 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 4425 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4426 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4427 | struct stream *s; |
| 4428 | const char *name; |
| 4429 | size_t len; |
| 4430 | struct sample smp; |
| 4431 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4432 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4433 | 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] | 4434 | |
| 4435 | /* It is useles to retrieve the stream, but this function |
| 4436 | * runs only in a stream context. |
| 4437 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4438 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4439 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4440 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4441 | |
| 4442 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4443 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4444 | hlua_lua2smp(L, 3, &smp); |
| 4445 | |
| 4446 | /* Store the sample in a variable. */ |
| 4447 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4448 | |
| 4449 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4450 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4451 | else |
| 4452 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4453 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4454 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4455 | } |
| 4456 | |
| 4457 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 4458 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4459 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4460 | struct stream *s; |
| 4461 | const char *name; |
| 4462 | size_t len; |
| 4463 | struct sample smp; |
| 4464 | |
| 4465 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4466 | |
| 4467 | /* It is useles to retrieve the stream, but this function |
| 4468 | * runs only in a stream context. |
| 4469 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4470 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4471 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4472 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4473 | |
| 4474 | /* Unset the variable. */ |
| 4475 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4476 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4477 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4478 | } |
| 4479 | |
| 4480 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 4481 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4482 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4483 | struct stream *s; |
| 4484 | const char *name; |
| 4485 | size_t len; |
| 4486 | struct sample smp; |
| 4487 | |
| 4488 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4489 | |
| 4490 | /* It is useles to retrieve the stream, but this function |
| 4491 | * runs only in a stream context. |
| 4492 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4493 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4494 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4495 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4496 | |
| 4497 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 4498 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4499 | lua_pushnil(L); |
| 4500 | return 1; |
| 4501 | } |
| 4502 | |
| 4503 | return hlua_smp2lua(L, &smp); |
| 4504 | } |
| 4505 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4506 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 4507 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4508 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4509 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4510 | struct hlua *hlua; |
| 4511 | |
| 4512 | /* 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] | 4513 | if (!s->hlua) |
| 4514 | return 0; |
| 4515 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4516 | |
| 4517 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4518 | |
| 4519 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4520 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4521 | |
| 4522 | /* Get and store new value. */ |
| 4523 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4524 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4525 | |
| 4526 | return 0; |
| 4527 | } |
| 4528 | |
| 4529 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 4530 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4531 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4532 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4533 | struct hlua *hlua; |
| 4534 | |
| 4535 | /* 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] | 4536 | if (!s->hlua) { |
| 4537 | lua_pushnil(L); |
| 4538 | return 1; |
| 4539 | } |
| 4540 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4541 | |
| 4542 | /* Push configuration index in the stack. */ |
| 4543 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4544 | |
| 4545 | return 1; |
| 4546 | } |
| 4547 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4548 | /* If expected data not yet available, it returns a yield. This function |
| 4549 | * consumes the data in the buffer. It returns a string containing the |
| 4550 | * data. This string can be empty. |
| 4551 | */ |
| 4552 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4553 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4554 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4555 | struct stconn *sc = appctx_sc(luactx->appctx); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4556 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4557 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4558 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4559 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4560 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4561 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4562 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4563 | ret = co_getline_nc(sc_oc(sc), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4564 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4565 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4566 | if (ret == 0) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4567 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4568 | 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] | 4569 | } |
| 4570 | |
| 4571 | /* End of data: commit the total strings and return. */ |
| 4572 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4573 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4574 | return 1; |
| 4575 | } |
| 4576 | |
| 4577 | /* Ensure that the block 2 length is usable. */ |
| 4578 | if (ret == 1) |
| 4579 | len2 = 0; |
| 4580 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4581 | /* don't check the max length read and don't check. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4582 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4583 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4584 | |
| 4585 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4586 | co_skip(sc_oc(sc), len1 + len2); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4587 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4588 | return 1; |
| 4589 | } |
| 4590 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4591 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4592 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 4593 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4594 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4595 | |
| 4596 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4597 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4598 | |
| 4599 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 4600 | } |
| 4601 | |
| 4602 | /* If expected data not yet available, it returns a yield. This function |
| 4603 | * consumes the data in the buffer. It returns a string containing the |
| 4604 | * data. This string can be empty. |
| 4605 | */ |
| 4606 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4607 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4608 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4609 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4610 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4611 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4612 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4613 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4614 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4615 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4616 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4617 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4618 | ret = co_getblk_nc(sc_oc(sc), &blk1, &len1, &blk2, &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 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4621 | if (ret == 0) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4622 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4623 | 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] | 4624 | } |
| 4625 | |
| 4626 | /* End of data: commit the total strings and return. */ |
| 4627 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4628 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4629 | return 1; |
| 4630 | } |
| 4631 | |
| 4632 | /* Ensure that the block 2 length is usable. */ |
| 4633 | if (ret == 1) |
| 4634 | len2 = 0; |
| 4635 | |
| 4636 | if (len == -1) { |
| 4637 | |
| 4638 | /* If len == -1, catenate all the data avalaile and |
| 4639 | * yield because we want to get all the data until |
| 4640 | * the end of data stream. |
| 4641 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4642 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4643 | luaL_addlstring(&luactx->b, blk2, len2); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4644 | co_skip(sc_oc(sc), len1 + len2); |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4645 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4646 | 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] | 4647 | |
| 4648 | } else { |
| 4649 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4650 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4651 | if (len1 > len) |
| 4652 | len1 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4653 | luaL_addlstring(&luactx->b, blk1, len1); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4654 | len -= len1; |
| 4655 | |
| 4656 | /* Copy the second block. */ |
| 4657 | if (len2 > len) |
| 4658 | len2 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4659 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4660 | len -= len2; |
| 4661 | |
| 4662 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4663 | co_skip(sc_oc(sc), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4664 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4665 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4666 | if (len > 0) { |
| 4667 | lua_pushinteger(L, len); |
| 4668 | lua_replace(L, 2); |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4669 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4670 | 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] | 4671 | } |
| 4672 | |
| 4673 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4674 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4675 | return 1; |
| 4676 | } |
| 4677 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4678 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4679 | hlua_pusherror(L, "Lua: internal error"); |
| 4680 | WILL_LJMP(lua_error(L)); |
| 4681 | return 0; |
| 4682 | } |
| 4683 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4684 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4685 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 4686 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4687 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4688 | int len = -1; |
| 4689 | |
| 4690 | if (lua_gettop(L) > 2) |
| 4691 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4692 | if (lua_gettop(L) >= 2) { |
| 4693 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4694 | lua_pop(L, 1); |
| 4695 | } |
| 4696 | |
| 4697 | /* Confirm or set the required length */ |
| 4698 | lua_pushinteger(L, len); |
| 4699 | |
| 4700 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4701 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4702 | |
| 4703 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4704 | } |
| 4705 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4706 | /* Append data in the output side of the buffer. This data is immediately |
| 4707 | * sent. The function returns the amount of data written. If the buffer |
| 4708 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4709 | * if the channel is closed. |
| 4710 | */ |
| 4711 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4712 | { |
| 4713 | size_t len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4714 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4715 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4716 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4717 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4718 | struct channel *chn = sc_ic(sc); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4719 | int max; |
| 4720 | |
| 4721 | /* Get the max amount of data which can write as input in the channel. */ |
| 4722 | max = channel_recv_max(chn); |
| 4723 | if (max > (len - l)) |
| 4724 | max = len - l; |
| 4725 | |
| 4726 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4727 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4728 | |
| 4729 | /* update counters. */ |
| 4730 | l += max; |
| 4731 | lua_pop(L, 1); |
| 4732 | lua_pushinteger(L, l); |
| 4733 | |
| 4734 | /* If some data is not send, declares the situation to the |
| 4735 | * applet, and returns a yield. |
| 4736 | */ |
| 4737 | if (l < len) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4738 | sc_need_room(sc); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4739 | 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] | 4740 | } |
| 4741 | |
| 4742 | return 1; |
| 4743 | } |
| 4744 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4745 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4746 | * yield the LUA process, and resume it without checking the |
| 4747 | * input arguments. |
| 4748 | */ |
| 4749 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4750 | { |
| 4751 | MAY_LJMP(check_args(L, 2, "send")); |
| 4752 | lua_pushinteger(L, 0); |
| 4753 | |
| 4754 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4755 | } |
| 4756 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4757 | /* |
| 4758 | * |
| 4759 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4760 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4761 | * |
| 4762 | * |
| 4763 | */ |
| 4764 | |
| 4765 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4766 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4767 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4768 | __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] | 4769 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4770 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4771 | } |
| 4772 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4773 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4774 | * according with a current TXN. |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4775 | * It relies on the caller to have already reserved the room in ctx->svcctx |
| 4776 | * for the local storage of hlua_http_ctx. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4777 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4778 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4779 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4780 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4781 | struct hlua_appctx *luactx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4782 | struct hlua_txn htxn; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 4783 | struct stream *s = appctx_strm(ctx); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4784 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4785 | struct htx *htx; |
| 4786 | struct htx_blk *blk; |
| 4787 | struct htx_sl *sl; |
| 4788 | struct ist path; |
| 4789 | unsigned long long len = 0; |
| 4790 | int32_t pos; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4791 | struct http_uri_parser parser; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4792 | |
| 4793 | /* Check stack size. */ |
| 4794 | if (!lua_checkstack(L, 3)) |
| 4795 | return 0; |
| 4796 | |
| 4797 | /* Create the object: obj[0] = userdata. |
| 4798 | * Note that the base of the Converters object is the |
| 4799 | * same than the TXN object. |
| 4800 | */ |
| 4801 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4802 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4803 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4804 | luactx->appctx = ctx; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4805 | http_ctx->status = 200; /* Default status code returned. */ |
| 4806 | http_ctx->reason = NULL; /* Use default reason based on status */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4807 | luactx->htxn.s = s; |
| 4808 | luactx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4809 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4810 | /* Create the "f" field that contains a list of fetches. */ |
| 4811 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4812 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4813 | return 0; |
| 4814 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4815 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4816 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4817 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4818 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4819 | return 0; |
| 4820 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4821 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4822 | /* Create the "c" field that contains a list of converters. */ |
| 4823 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4824 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4825 | return 0; |
| 4826 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4827 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4828 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4829 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4830 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4831 | return 0; |
| 4832 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4833 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4834 | htx = htxbuf(&s->req.buf); |
| 4835 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4836 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4837 | sl = htx_get_blk_ptr(htx, blk); |
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 | /* Stores the request method. */ |
| 4840 | lua_pushstring(L, "method"); |
| 4841 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4842 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4843 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4844 | /* Stores the http version. */ |
| 4845 | lua_pushstring(L, "version"); |
| 4846 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4847 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4848 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4849 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4850 | * the array on the top of the stack. |
| 4851 | */ |
| 4852 | lua_pushstring(L, "headers"); |
| 4853 | htxn.s = s; |
| 4854 | htxn.p = px; |
| 4855 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4856 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4857 | return 0; |
| 4858 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4859 | |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4860 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 4861 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4862 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4863 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4864 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4865 | p = path.ptr; |
Tim Duesterhus | 4c8f75f | 2021-11-06 15:14:44 +0100 | [diff] [blame] | 4866 | end = istend(path); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4867 | q = p; |
| 4868 | while (q < end && *q != '?') |
| 4869 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4870 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4871 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4872 | lua_pushstring(L, "path"); |
| 4873 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4874 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4875 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4876 | /* Stores the query string. */ |
| 4877 | lua_pushstring(L, "qs"); |
| 4878 | if (*q == '?') |
| 4879 | q++; |
| 4880 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4881 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4882 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4883 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4884 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4885 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4886 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4887 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4888 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4889 | break; |
| 4890 | if (type == HTX_BLK_DATA) |
| 4891 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4892 | } |
Christopher Faulet | 2e47e3a | 2023-01-13 11:40:24 +0100 | [diff] [blame] | 4893 | if (htx->extra != HTX_UNKOWN_PAYLOAD_LENGTH) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4894 | len += htx->extra; |
| 4895 | |
| 4896 | /* Stores the request path. */ |
| 4897 | lua_pushstring(L, "length"); |
| 4898 | lua_pushinteger(L, len); |
| 4899 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4900 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4901 | /* Create an empty array of HTTP request headers. */ |
| 4902 | lua_pushstring(L, "response"); |
| 4903 | lua_newtable(L); |
| 4904 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4905 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4906 | /* Pop a class stream metatable and affect it to the table. */ |
| 4907 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4908 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4909 | |
| 4910 | return 1; |
| 4911 | } |
| 4912 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4913 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4914 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4915 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4916 | struct stream *s; |
| 4917 | const char *name; |
| 4918 | size_t len; |
| 4919 | struct sample smp; |
| 4920 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4921 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4922 | 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] | 4923 | |
| 4924 | /* It is useles to retrieve the stream, but this function |
| 4925 | * runs only in a stream context. |
| 4926 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4927 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4928 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4929 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4930 | |
| 4931 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4932 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4933 | hlua_lua2smp(L, 3, &smp); |
| 4934 | |
| 4935 | /* Store the sample in a variable. */ |
| 4936 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4937 | |
| 4938 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4939 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4940 | else |
| 4941 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4942 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4943 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4944 | } |
| 4945 | |
| 4946 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4947 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4948 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4949 | struct stream *s; |
| 4950 | const char *name; |
| 4951 | size_t len; |
| 4952 | struct sample smp; |
| 4953 | |
| 4954 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4955 | |
| 4956 | /* It is useles to retrieve the stream, but this function |
| 4957 | * runs only in a stream context. |
| 4958 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4959 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4960 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4961 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4962 | |
| 4963 | /* Unset the variable. */ |
| 4964 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4965 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4966 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4967 | } |
| 4968 | |
| 4969 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4970 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4971 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4972 | struct stream *s; |
| 4973 | const char *name; |
| 4974 | size_t len; |
| 4975 | struct sample smp; |
| 4976 | |
| 4977 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4978 | |
| 4979 | /* It is useles to retrieve the stream, but this function |
| 4980 | * runs only in a stream context. |
| 4981 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4982 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4983 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4984 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4985 | |
| 4986 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 4987 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4988 | lua_pushnil(L); |
| 4989 | return 1; |
| 4990 | } |
| 4991 | |
| 4992 | return hlua_smp2lua(L, &smp); |
| 4993 | } |
| 4994 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4995 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4996 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4997 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4998 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4999 | struct hlua *hlua; |
| 5000 | |
| 5001 | /* 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] | 5002 | if (!s->hlua) |
| 5003 | return 0; |
| 5004 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 5005 | |
| 5006 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5007 | |
| 5008 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5009 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 5010 | |
| 5011 | /* Get and store new value. */ |
| 5012 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5013 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5014 | |
| 5015 | return 0; |
| 5016 | } |
| 5017 | |
| 5018 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 5019 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5020 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5021 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 5022 | struct hlua *hlua; |
| 5023 | |
| 5024 | /* 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] | 5025 | if (!s->hlua) { |
| 5026 | lua_pushnil(L); |
| 5027 | return 1; |
| 5028 | } |
| 5029 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 5030 | |
| 5031 | /* Push configuration index in the stack. */ |
| 5032 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5033 | |
| 5034 | return 1; |
| 5035 | } |
| 5036 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5037 | /* If expected data not yet available, it returns a yield. This function |
| 5038 | * consumes the data in the buffer. It returns a string containing the |
| 5039 | * data. This string can be empty. |
| 5040 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5041 | __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] | 5042 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5043 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5044 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5045 | struct channel *req = sc_oc(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5046 | struct htx *htx; |
| 5047 | struct htx_blk *blk; |
| 5048 | size_t count; |
| 5049 | int stop = 0; |
| 5050 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5051 | htx = htx_from_buf(&req->buf); |
| 5052 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 5053 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 5054 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5055 | while (count && !stop && blk) { |
| 5056 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5057 | uint32_t sz = htx_get_blksz(blk); |
| 5058 | struct ist v; |
| 5059 | uint32_t vlen; |
| 5060 | char *nl; |
| 5061 | |
| 5062 | vlen = sz; |
| 5063 | if (vlen > count) { |
| 5064 | if (type != HTX_BLK_DATA) |
| 5065 | break; |
| 5066 | vlen = count; |
| 5067 | } |
| 5068 | |
| 5069 | switch (type) { |
| 5070 | case HTX_BLK_UNUSED: |
| 5071 | break; |
| 5072 | |
| 5073 | case HTX_BLK_DATA: |
| 5074 | v = htx_get_blk_value(htx, blk); |
| 5075 | v.len = vlen; |
| 5076 | nl = istchr(v, '\n'); |
| 5077 | if (nl != NULL) { |
| 5078 | stop = 1; |
| 5079 | vlen = nl - v.ptr + 1; |
| 5080 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5081 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5082 | break; |
| 5083 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5084 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5085 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5086 | stop = 1; |
| 5087 | break; |
| 5088 | |
| 5089 | default: |
| 5090 | break; |
| 5091 | } |
| 5092 | |
Willy Tarreau | 8424004 | 2022-02-28 16:51:23 +0100 | [diff] [blame] | 5093 | c_rew(req, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5094 | count -= vlen; |
| 5095 | if (sz == vlen) |
| 5096 | blk = htx_remove_blk(htx, blk); |
| 5097 | else { |
| 5098 | htx_cut_data_blk(htx, blk, vlen); |
| 5099 | break; |
| 5100 | } |
| 5101 | } |
| 5102 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5103 | /* The message was fully consumed and no more data are expected |
| 5104 | * (EOM flag set). |
| 5105 | */ |
Christopher Faulet | 4a20972 | 2022-08-29 15:37:16 +0200 | [diff] [blame] | 5106 | if (htx_is_empty(htx) && (req->flags & CF_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5107 | stop = 1; |
| 5108 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5109 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5110 | if (!stop) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 5111 | applet_need_more_data(luactx->appctx); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5112 | 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] | 5113 | } |
| 5114 | |
| 5115 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5116 | luaL_pushresult(&luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5117 | return 1; |
| 5118 | } |
| 5119 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5120 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5121 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5122 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5123 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5124 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5125 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5126 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5127 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5128 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5129 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5130 | } |
| 5131 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5132 | /* If expected data not yet available, it returns a yield. This function |
| 5133 | * consumes the data in the buffer. It returns a string containing the |
| 5134 | * data. This string can be empty. |
| 5135 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5136 | __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] | 5137 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5138 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5139 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5140 | struct channel *req = sc_oc(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5141 | struct htx *htx; |
| 5142 | struct htx_blk *blk; |
| 5143 | size_t count; |
| 5144 | int len; |
| 5145 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5146 | htx = htx_from_buf(&req->buf); |
| 5147 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5148 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 5149 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5150 | while (count && len && blk) { |
| 5151 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5152 | uint32_t sz = htx_get_blksz(blk); |
| 5153 | struct ist v; |
| 5154 | uint32_t vlen; |
| 5155 | |
| 5156 | vlen = sz; |
| 5157 | if (len > 0 && vlen > len) |
| 5158 | vlen = len; |
| 5159 | if (vlen > count) { |
| 5160 | if (type != HTX_BLK_DATA) |
| 5161 | break; |
| 5162 | vlen = count; |
| 5163 | } |
| 5164 | |
| 5165 | switch (type) { |
| 5166 | case HTX_BLK_UNUSED: |
| 5167 | break; |
| 5168 | |
| 5169 | case HTX_BLK_DATA: |
| 5170 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5171 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5172 | break; |
| 5173 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5174 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5175 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5176 | len = 0; |
| 5177 | break; |
| 5178 | |
| 5179 | default: |
| 5180 | break; |
| 5181 | } |
| 5182 | |
Willy Tarreau | 8424004 | 2022-02-28 16:51:23 +0100 | [diff] [blame] | 5183 | c_rew(req, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5184 | count -= vlen; |
| 5185 | if (len > 0) |
| 5186 | len -= vlen; |
| 5187 | if (sz == vlen) |
| 5188 | blk = htx_remove_blk(htx, blk); |
| 5189 | else { |
| 5190 | htx_cut_data_blk(htx, blk, vlen); |
| 5191 | break; |
| 5192 | } |
| 5193 | } |
| 5194 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5195 | /* The message was fully consumed and no more data are expected |
| 5196 | * (EOM flag set). |
| 5197 | */ |
Christopher Faulet | 4a20972 | 2022-08-29 15:37:16 +0200 | [diff] [blame] | 5198 | if (htx_is_empty(htx) && (req->flags & CF_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5199 | len = 0; |
| 5200 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5201 | htx_to_buf(htx, &req->buf); |
| 5202 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5203 | /* If we are no other data available, yield waiting for new data. */ |
| 5204 | if (len) { |
| 5205 | if (len > 0) { |
| 5206 | lua_pushinteger(L, len); |
| 5207 | lua_replace(L, 2); |
| 5208 | } |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 5209 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5210 | 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] | 5211 | } |
| 5212 | |
| 5213 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5214 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5215 | return 1; |
| 5216 | } |
| 5217 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5218 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5219 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 5220 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5221 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5222 | int len = -1; |
| 5223 | |
| 5224 | /* Check arguments. */ |
| 5225 | if (lua_gettop(L) > 2) |
| 5226 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 5227 | if (lua_gettop(L) >= 2) { |
| 5228 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5229 | lua_pop(L, 1); |
| 5230 | } |
| 5231 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5232 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5233 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5234 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5235 | luaL_buffinit(L, &luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5236 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5237 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5238 | } |
| 5239 | |
| 5240 | /* Append data in the output side of the buffer. This data is immediately |
| 5241 | * sent. The function returns the amount of data written. If the buffer |
| 5242 | * cannot contain the data, the function yields. The function returns -1 |
| 5243 | * if the channel is closed. |
| 5244 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5245 | __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] | 5246 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5247 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5248 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5249 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5250 | struct htx *htx = htx_from_buf(&res->buf); |
| 5251 | const char *data; |
| 5252 | size_t len; |
| 5253 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 5254 | int max; |
| 5255 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 5256 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5257 | if (!max) |
| 5258 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5259 | |
| 5260 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5261 | |
| 5262 | /* Get the max amount of data which can write as input in the channel. */ |
| 5263 | if (max > (len - l)) |
| 5264 | max = len - l; |
| 5265 | |
| 5266 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 5267 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5268 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5269 | |
| 5270 | /* update counters. */ |
| 5271 | l += max; |
| 5272 | lua_pop(L, 1); |
| 5273 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5274 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5275 | /* If some data is not send, declares the situation to the |
| 5276 | * applet, and returns a yield. |
| 5277 | */ |
| 5278 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5279 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5280 | htx_to_buf(htx, &res->buf); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5281 | sc_need_room(sc); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5282 | 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] | 5283 | } |
| 5284 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5285 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5286 | return 1; |
| 5287 | } |
| 5288 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5289 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5290 | * yield the LUA process, and resume it without checking the |
| 5291 | * input arguments. |
| 5292 | */ |
| 5293 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 5294 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5295 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5296 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5297 | |
| 5298 | /* We want to send some data. Headers must be sent. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5299 | if (!(http_ctx->flags & APPLET_HDR_SENT)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5300 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 5301 | WILL_LJMP(lua_error(L)); |
| 5302 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5303 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5304 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5305 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5306 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5307 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5308 | } |
| 5309 | |
| 5310 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 5311 | { |
| 5312 | const char *name; |
| 5313 | int ret; |
| 5314 | |
| 5315 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5316 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5317 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 5318 | |
| 5319 | /* Push in the stack the "response" entry. */ |
| 5320 | ret = lua_getfield(L, 1, "response"); |
| 5321 | if (ret != LUA_TTABLE) { |
| 5322 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 5323 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 5324 | WILL_LJMP(lua_error(L)); |
| 5325 | } |
| 5326 | |
| 5327 | /* check if the header is already registered if it is not |
| 5328 | * the case, register it. |
| 5329 | */ |
| 5330 | ret = lua_getfield(L, -1, name); |
| 5331 | if (ret == LUA_TNIL) { |
| 5332 | |
| 5333 | /* Entry not found. */ |
| 5334 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 5335 | |
| 5336 | /* Insert the new header name in the array in the top of the stack. |
| 5337 | * It left the new array in the top of the stack. |
| 5338 | */ |
| 5339 | lua_newtable(L); |
| 5340 | lua_pushvalue(L, 2); |
| 5341 | lua_pushvalue(L, -2); |
| 5342 | lua_settable(L, -4); |
| 5343 | |
| 5344 | } else if (ret != LUA_TTABLE) { |
| 5345 | |
| 5346 | /* corruption error. */ |
| 5347 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 5348 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 5349 | WILL_LJMP(lua_error(L)); |
| 5350 | } |
| 5351 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5352 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5353 | * the header value as new entry. |
| 5354 | */ |
| 5355 | lua_pushvalue(L, 3); |
| 5356 | ret = lua_rawlen(L, -2); |
| 5357 | lua_rawseti(L, -2, ret + 1); |
| 5358 | lua_pushboolean(L, 1); |
| 5359 | return 1; |
| 5360 | } |
| 5361 | |
| 5362 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 5363 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5364 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5365 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5366 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5367 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5368 | |
| 5369 | if (status < 100 || status > 599) { |
| 5370 | lua_pushboolean(L, 0); |
| 5371 | return 1; |
| 5372 | } |
| 5373 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5374 | http_ctx->status = status; |
| 5375 | http_ctx->reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5376 | lua_pushboolean(L, 1); |
| 5377 | return 1; |
| 5378 | } |
| 5379 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5380 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5381 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5382 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5383 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5384 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5385 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5386 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5387 | struct htx *htx; |
| 5388 | struct htx_sl *sl; |
| 5389 | struct h1m h1m; |
| 5390 | const char *status, *reason; |
| 5391 | const char *name, *value; |
| 5392 | size_t nlen, vlen; |
| 5393 | unsigned int flags; |
| 5394 | |
| 5395 | /* Send the message at once. */ |
| 5396 | htx = htx_from_buf(&res->buf); |
| 5397 | h1m_init_res(&h1m); |
| 5398 | |
| 5399 | /* Use the same http version than the request. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5400 | status = ultoa_r(http_ctx->status, trash.area, trash.size); |
| 5401 | reason = http_ctx->reason; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5402 | if (reason == NULL) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5403 | reason = http_get_reason(http_ctx->status); |
| 5404 | if (http_ctx->flags & APPLET_HTTP11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5405 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5406 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 5407 | } |
| 5408 | else { |
| 5409 | flags = HTX_SL_F_IS_RESP; |
| 5410 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 5411 | } |
| 5412 | if (!sl) { |
| 5413 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5414 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5415 | WILL_LJMP(lua_error(L)); |
| 5416 | } |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5417 | sl->info.res.status = http_ctx->status; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5418 | |
| 5419 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5420 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 5421 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5422 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5423 | WILL_LJMP(lua_error(L)); |
| 5424 | } |
| 5425 | |
| 5426 | /* Browse the list of headers. */ |
| 5427 | lua_pushnil(L); |
| 5428 | while(lua_next(L, -2) != 0) { |
| 5429 | /* We expect a string as -2. */ |
| 5430 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 5431 | 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] | 5432 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5433 | lua_typename(L, lua_type(L, -2))); |
| 5434 | WILL_LJMP(lua_error(L)); |
| 5435 | } |
| 5436 | name = lua_tolstring(L, -2, &nlen); |
| 5437 | |
| 5438 | /* We expect an array as -1. */ |
| 5439 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 5440 | 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] | 5441 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5442 | name, |
| 5443 | lua_typename(L, lua_type(L, -1))); |
| 5444 | WILL_LJMP(lua_error(L)); |
| 5445 | } |
| 5446 | |
| 5447 | /* Browse the table who is on the top of the stack. */ |
| 5448 | lua_pushnil(L); |
| 5449 | while(lua_next(L, -2) != 0) { |
| 5450 | int id; |
| 5451 | |
| 5452 | /* We expect a number as -2. */ |
| 5453 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 5454 | 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] | 5455 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5456 | name, |
| 5457 | lua_typename(L, lua_type(L, -2))); |
| 5458 | WILL_LJMP(lua_error(L)); |
| 5459 | } |
| 5460 | id = lua_tointeger(L, -2); |
| 5461 | |
| 5462 | /* We expect a string as -2. */ |
| 5463 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 5464 | 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] | 5465 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5466 | name, id, |
| 5467 | lua_typename(L, lua_type(L, -1))); |
| 5468 | WILL_LJMP(lua_error(L)); |
| 5469 | } |
| 5470 | value = lua_tolstring(L, -1, &vlen); |
| 5471 | |
| 5472 | /* Simple Protocol checks. */ |
Christopher Faulet | 545fbba | 2021-09-28 09:36:25 +0200 | [diff] [blame] | 5473 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) { |
| 5474 | int ret; |
| 5475 | |
| 5476 | ret = h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
| 5477 | if (ret < 0) { |
| 5478 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 5479 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
| 5480 | name); |
| 5481 | WILL_LJMP(lua_error(L)); |
| 5482 | } |
| 5483 | else if (ret == 0) |
| 5484 | goto next; /* Skip it */ |
| 5485 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5486 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 5487 | struct ist v = ist2(value, vlen); |
| 5488 | int ret; |
| 5489 | |
| 5490 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 5491 | if (ret < 0) { |
| 5492 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5493 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5494 | name); |
| 5495 | WILL_LJMP(lua_error(L)); |
| 5496 | } |
| 5497 | else if (ret == 0) |
| 5498 | goto next; /* Skip it */ |
| 5499 | } |
| 5500 | |
| 5501 | /* Add a new header */ |
| 5502 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 5503 | 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] | 5504 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5505 | name); |
| 5506 | WILL_LJMP(lua_error(L)); |
| 5507 | } |
| 5508 | next: |
| 5509 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5510 | lua_pop(L, 1); |
| 5511 | } |
| 5512 | |
| 5513 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5514 | lua_pop(L, 1); |
| 5515 | } |
| 5516 | |
| 5517 | if (h1m.flags & H1_MF_CHNK) |
| 5518 | h1m.flags &= ~H1_MF_CLEN; |
| 5519 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5520 | h1m.flags |= H1_MF_XFER_LEN; |
| 5521 | |
| 5522 | /* Uset HTX start-line flags */ |
| 5523 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5524 | flags |= HTX_SL_F_XFER_ENC; |
| 5525 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5526 | flags |= HTX_SL_F_XFER_LEN; |
| 5527 | if (h1m.flags & H1_MF_CHNK) |
| 5528 | flags |= HTX_SL_F_CHNK; |
| 5529 | else if (h1m.flags & H1_MF_CLEN) |
| 5530 | flags |= HTX_SL_F_CLEN; |
| 5531 | if (h1m.body_len == 0) |
| 5532 | flags |= HTX_SL_F_BODYLESS; |
| 5533 | } |
| 5534 | sl->flags |= flags; |
| 5535 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5536 | /* 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] | 5537 | * and the status code implies the presence of a message body, we must |
| 5538 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5539 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 5540 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5541 | */ |
| 5542 | 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] | 5543 | http_ctx->status >= 200 && http_ctx->status != 204 && http_ctx->status != 304) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5544 | /* Add a new header */ |
| 5545 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 5546 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 5547 | 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] | 5548 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5549 | WILL_LJMP(lua_error(L)); |
| 5550 | } |
| 5551 | } |
| 5552 | |
| 5553 | /* Finalize headers. */ |
| 5554 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 5555 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5556 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5557 | WILL_LJMP(lua_error(L)); |
| 5558 | } |
| 5559 | |
| 5560 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 5561 | b_reset(&res->buf); |
| 5562 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 5563 | WILL_LJMP(lua_error(L)); |
| 5564 | } |
| 5565 | |
| 5566 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5567 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5568 | |
| 5569 | /* Headers sent, set the flag. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5570 | http_ctx->flags |= APPLET_HDR_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5571 | return 0; |
| 5572 | |
| 5573 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5574 | /* We will build the status line and the headers of the HTTP response. |
| 5575 | * We will try send at once if its not possible, we give back the hand |
| 5576 | * waiting for more room. |
| 5577 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5578 | __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] | 5579 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5580 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5581 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5582 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5583 | |
| 5584 | if (co_data(res)) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5585 | sc_need_room(sc); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5586 | 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] | 5587 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5588 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5589 | } |
| 5590 | |
| 5591 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5592 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5593 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5594 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5595 | } |
| 5596 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5597 | /* |
| 5598 | * |
| 5599 | * |
| 5600 | * Class HTTP |
| 5601 | * |
| 5602 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5603 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5604 | |
| 5605 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5606 | * a class stream, otherwise it throws an error. |
| 5607 | */ |
| 5608 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5609 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5610 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 5611 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5612 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5613 | /* This function creates and push in the stack a HTTP object |
| 5614 | * according with a current TXN. |
| 5615 | */ |
| 5616 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5617 | { |
| 5618 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5619 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5620 | /* Check stack size. */ |
| 5621 | if (!lua_checkstack(L, 3)) |
| 5622 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5623 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5624 | /* Create the object: obj[0] = userdata. |
| 5625 | * Note that the base of the Converters object is the |
| 5626 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5627 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5628 | lua_newtable(L); |
| 5629 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5630 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5631 | |
| 5632 | htxn->s = txn->s; |
| 5633 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 5634 | htxn->dir = txn->dir; |
| 5635 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5636 | |
| 5637 | /* Pop a class stream metatable and affect it to the table. */ |
| 5638 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5639 | lua_setmetatable(L, -2); |
| 5640 | |
| 5641 | return 1; |
| 5642 | } |
| 5643 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5644 | /* This function creates and returns an array containing the status-line |
| 5645 | * elements. This function does not fails. |
| 5646 | */ |
| 5647 | __LJMP static int hlua_http_get_stline(lua_State *L, struct htx_sl *sl) |
| 5648 | { |
| 5649 | /* Create the table. */ |
| 5650 | lua_newtable(L); |
| 5651 | |
| 5652 | if (sl->flags & HTX_SL_F_IS_RESP) { |
| 5653 | lua_pushstring(L, "version"); |
| 5654 | lua_pushlstring(L, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); |
| 5655 | lua_settable(L, -3); |
| 5656 | lua_pushstring(L, "code"); |
| 5657 | lua_pushlstring(L, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); |
| 5658 | lua_settable(L, -3); |
| 5659 | lua_pushstring(L, "reason"); |
| 5660 | lua_pushlstring(L, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); |
| 5661 | lua_settable(L, -3); |
| 5662 | } |
| 5663 | else { |
| 5664 | lua_pushstring(L, "method"); |
| 5665 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 5666 | lua_settable(L, -3); |
| 5667 | lua_pushstring(L, "uri"); |
| 5668 | lua_pushlstring(L, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); |
| 5669 | lua_settable(L, -3); |
| 5670 | lua_pushstring(L, "version"); |
| 5671 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 5672 | lua_settable(L, -3); |
| 5673 | } |
| 5674 | return 1; |
| 5675 | } |
| 5676 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5677 | /* This function creates ans returns an array of HTTP headers. |
| 5678 | * This function does not fails. It is used as wrapper with the |
| 5679 | * 2 following functions. |
| 5680 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5681 | __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] | 5682 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5683 | struct htx *htx; |
| 5684 | int32_t pos; |
| 5685 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5686 | /* Create the table. */ |
| 5687 | lua_newtable(L); |
| 5688 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5689 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5690 | htx = htxbuf(&msg->chn->buf); |
| 5691 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5692 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5693 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5694 | struct ist n, v; |
| 5695 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5696 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5697 | if (type == HTX_BLK_HDR) { |
| 5698 | n = htx_get_blk_name(htx,blk); |
| 5699 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5700 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5701 | else if (type == HTX_BLK_EOH) |
| 5702 | break; |
| 5703 | else |
| 5704 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5705 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5706 | /* Check for existing entry: |
| 5707 | * assume that the table is on the top of the stack, and |
| 5708 | * push the key in the stack, the function lua_gettable() |
| 5709 | * perform the lookup. |
| 5710 | */ |
| 5711 | lua_pushlstring(L, n.ptr, n.len); |
| 5712 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5713 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5714 | switch (lua_type(L, -1)) { |
| 5715 | case LUA_TNIL: |
| 5716 | /* Table not found, create it. */ |
| 5717 | lua_pop(L, 1); /* remove the nil value. */ |
| 5718 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5719 | lua_newtable(L); /* create and push empty table. */ |
| 5720 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5721 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5722 | 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] | 5723 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5724 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5725 | case LUA_TTABLE: |
| 5726 | /* Entry found: push the value in the table. */ |
| 5727 | len = lua_rawlen(L, -1); |
| 5728 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5729 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5730 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5731 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5732 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5733 | default: |
| 5734 | /* Other cases are errors. */ |
| 5735 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5736 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5737 | } |
| 5738 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5739 | return 1; |
| 5740 | } |
| 5741 | |
| 5742 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5743 | { |
| 5744 | struct hlua_txn *htxn; |
| 5745 | |
| 5746 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5747 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5748 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5749 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5750 | WILL_LJMP(lua_error(L)); |
| 5751 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5752 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5753 | } |
| 5754 | |
| 5755 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5756 | { |
| 5757 | struct hlua_txn *htxn; |
| 5758 | |
| 5759 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5760 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5761 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5762 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5763 | WILL_LJMP(lua_error(L)); |
| 5764 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5765 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5766 | } |
| 5767 | |
| 5768 | /* This function replace full header, or just a value in |
| 5769 | * the request or in the response. It is a wrapper fir the |
| 5770 | * 4 following functions. |
| 5771 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5772 | __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] | 5773 | { |
| 5774 | size_t name_len; |
| 5775 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5776 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5777 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5778 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5779 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5780 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5781 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5782 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5783 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5784 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5785 | 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] | 5786 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5787 | return 0; |
| 5788 | } |
| 5789 | |
| 5790 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5791 | { |
| 5792 | struct hlua_txn *htxn; |
| 5793 | |
| 5794 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5795 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5796 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5797 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5798 | WILL_LJMP(lua_error(L)); |
| 5799 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5800 | 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] | 5801 | } |
| 5802 | |
| 5803 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5804 | { |
| 5805 | struct hlua_txn *htxn; |
| 5806 | |
| 5807 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5808 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5809 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5810 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5811 | WILL_LJMP(lua_error(L)); |
| 5812 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5813 | 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] | 5814 | } |
| 5815 | |
| 5816 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5817 | { |
| 5818 | struct hlua_txn *htxn; |
| 5819 | |
| 5820 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5821 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5822 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5823 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5824 | WILL_LJMP(lua_error(L)); |
| 5825 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5826 | 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] | 5827 | } |
| 5828 | |
| 5829 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5830 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5831 | struct hlua_txn *htxn; |
| 5832 | |
| 5833 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5834 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5835 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5836 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5837 | WILL_LJMP(lua_error(L)); |
| 5838 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5839 | 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] | 5840 | } |
| 5841 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5842 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5843 | * It is a wrapper for the 2 following functions. |
| 5844 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5845 | __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] | 5846 | { |
| 5847 | size_t len; |
| 5848 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5849 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5850 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5851 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5852 | ctx.blk = NULL; |
| 5853 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5854 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5855 | return 0; |
| 5856 | } |
| 5857 | |
| 5858 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5859 | { |
| 5860 | struct hlua_txn *htxn; |
| 5861 | |
| 5862 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5863 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5864 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5865 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5866 | WILL_LJMP(lua_error(L)); |
| 5867 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5868 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5869 | } |
| 5870 | |
| 5871 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5872 | { |
| 5873 | struct hlua_txn *htxn; |
| 5874 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5875 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5876 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5877 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5878 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5879 | WILL_LJMP(lua_error(L)); |
| 5880 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5881 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5882 | } |
| 5883 | |
| 5884 | /* This function adds an header. It is a wrapper used by |
| 5885 | * the 2 following functions. |
| 5886 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5887 | __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] | 5888 | { |
| 5889 | size_t name_len; |
| 5890 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5891 | size_t value_len; |
| 5892 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5893 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5894 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5895 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5896 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5897 | return 0; |
| 5898 | } |
| 5899 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5900 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5901 | { |
| 5902 | struct hlua_txn *htxn; |
| 5903 | |
| 5904 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5905 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5906 | |
| 5907 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5908 | WILL_LJMP(lua_error(L)); |
| 5909 | |
| 5910 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 5911 | } |
| 5912 | |
| 5913 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5914 | { |
| 5915 | struct hlua_txn *htxn; |
| 5916 | |
| 5917 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5918 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5919 | |
| 5920 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5921 | WILL_LJMP(lua_error(L)); |
| 5922 | |
| 5923 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 5924 | } |
| 5925 | |
| 5926 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5927 | { |
| 5928 | struct hlua_txn *htxn; |
| 5929 | |
| 5930 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5931 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5932 | |
| 5933 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5934 | WILL_LJMP(lua_error(L)); |
| 5935 | |
| 5936 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5937 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 5938 | } |
| 5939 | |
| 5940 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5941 | { |
| 5942 | struct hlua_txn *htxn; |
| 5943 | |
| 5944 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5945 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5946 | |
| 5947 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5948 | WILL_LJMP(lua_error(L)); |
| 5949 | |
| 5950 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5951 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 5952 | } |
| 5953 | |
| 5954 | /* This function set the method. */ |
| 5955 | static int hlua_http_req_set_meth(lua_State *L) |
| 5956 | { |
| 5957 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5958 | size_t name_len; |
| 5959 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5960 | |
| 5961 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5962 | WILL_LJMP(lua_error(L)); |
| 5963 | |
| 5964 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1); |
| 5965 | return 1; |
| 5966 | } |
| 5967 | |
| 5968 | /* This function set the method. */ |
| 5969 | static int hlua_http_req_set_path(lua_State *L) |
| 5970 | { |
| 5971 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5972 | size_t name_len; |
| 5973 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5974 | |
| 5975 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5976 | WILL_LJMP(lua_error(L)); |
| 5977 | |
| 5978 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1); |
| 5979 | return 1; |
| 5980 | } |
| 5981 | |
| 5982 | /* This function set the query-string. */ |
| 5983 | static int hlua_http_req_set_query(lua_State *L) |
| 5984 | { |
| 5985 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5986 | size_t name_len; |
| 5987 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5988 | |
| 5989 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5990 | WILL_LJMP(lua_error(L)); |
| 5991 | |
| 5992 | /* Check length. */ |
| 5993 | if (name_len > trash.size - 1) { |
| 5994 | lua_pushboolean(L, 0); |
| 5995 | return 1; |
| 5996 | } |
| 5997 | |
| 5998 | /* Add the mark question as prefix. */ |
| 5999 | chunk_reset(&trash); |
| 6000 | trash.area[trash.data++] = '?'; |
| 6001 | memcpy(trash.area + trash.data, name, name_len); |
| 6002 | trash.data += name_len; |
| 6003 | |
| 6004 | lua_pushboolean(L, |
| 6005 | http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1); |
| 6006 | return 1; |
| 6007 | } |
| 6008 | |
| 6009 | /* This function set the uri. */ |
| 6010 | static int hlua_http_req_set_uri(lua_State *L) |
| 6011 | { |
| 6012 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6013 | size_t name_len; |
| 6014 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6015 | |
| 6016 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 6017 | WILL_LJMP(lua_error(L)); |
| 6018 | |
| 6019 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1); |
| 6020 | return 1; |
| 6021 | } |
| 6022 | |
| 6023 | /* This function set the response code & optionally reason. */ |
| 6024 | static int hlua_http_res_set_status(lua_State *L) |
| 6025 | { |
| 6026 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6027 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6028 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 6029 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
| 6030 | |
| 6031 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 6032 | WILL_LJMP(lua_error(L)); |
| 6033 | |
| 6034 | http_res_set_status(code, reason, htxn->s); |
| 6035 | return 0; |
| 6036 | } |
| 6037 | |
| 6038 | /* |
| 6039 | * |
| 6040 | * |
| 6041 | * Class HTTPMessage |
| 6042 | * |
| 6043 | * |
| 6044 | */ |
| 6045 | |
| 6046 | /* Returns a struct http_msg if the stack entry "ud" is a class HTTPMessage, |
| 6047 | * otherwise it throws an error. |
| 6048 | */ |
| 6049 | __LJMP static struct http_msg *hlua_checkhttpmsg(lua_State *L, int ud) |
| 6050 | { |
| 6051 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_msg_ref)); |
| 6052 | } |
| 6053 | |
| 6054 | /* Creates and pushes on the stack a HTTP object according with a current TXN. |
| 6055 | */ |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 6056 | 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] | 6057 | { |
| 6058 | /* Check stack size. */ |
| 6059 | if (!lua_checkstack(L, 3)) |
| 6060 | return 0; |
| 6061 | |
| 6062 | lua_newtable(L); |
| 6063 | lua_pushlightuserdata(L, msg); |
| 6064 | lua_rawseti(L, -2, 0); |
| 6065 | |
| 6066 | /* Create the "channel" field that contains the request channel object. */ |
| 6067 | lua_pushstring(L, "channel"); |
| 6068 | if (!hlua_channel_new(L, msg->chn)) |
| 6069 | return 0; |
| 6070 | lua_rawset(L, -3); |
| 6071 | |
| 6072 | /* Pop a class stream metatable and affect it to the table. */ |
| 6073 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_msg_ref); |
| 6074 | lua_setmetatable(L, -2); |
| 6075 | |
| 6076 | return 1; |
| 6077 | } |
| 6078 | |
| 6079 | /* Helper function returning a filter attached to the HTTP message at the |
| 6080 | * 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] | 6081 | * 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] | 6082 | * filled with output and input length respectively. |
| 6083 | */ |
| 6084 | static struct filter *hlua_http_msg_filter(lua_State *L, int ud, struct http_msg *msg, size_t *offset, size_t *len) |
| 6085 | { |
| 6086 | struct channel *chn = msg->chn; |
| 6087 | struct htx *htx = htxbuf(&chn->buf); |
| 6088 | struct filter *filter = NULL; |
| 6089 | |
| 6090 | *offset = co_data(msg->chn); |
| 6091 | *len = htx->data - co_data(msg->chn); |
| 6092 | |
| 6093 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 6094 | filter = lua_touserdata (L, -1); |
| 6095 | if (msg->msg_state >= HTTP_MSG_DATA) { |
| 6096 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6097 | |
| 6098 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 6099 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 6100 | } |
| 6101 | } |
| 6102 | |
| 6103 | lua_pop(L, 1); |
| 6104 | return filter; |
| 6105 | } |
| 6106 | |
| 6107 | /* Returns true if the channel attached to the HTTP message is the response |
| 6108 | * channel. |
| 6109 | */ |
| 6110 | __LJMP static int hlua_http_msg_is_resp(lua_State *L) |
| 6111 | { |
| 6112 | struct http_msg *msg; |
| 6113 | |
| 6114 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 6115 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6116 | |
| 6117 | lua_pushboolean(L, !!(msg->chn->flags & CF_ISRESP)); |
| 6118 | return 1; |
| 6119 | } |
| 6120 | |
| 6121 | /* Returns an array containing the elements status-line of the HTTP message. It relies |
| 6122 | * on hlua_http_get_stline(). |
| 6123 | */ |
| 6124 | __LJMP static int hlua_http_msg_get_stline(lua_State *L) |
| 6125 | { |
| 6126 | struct http_msg *msg; |
| 6127 | struct htx *htx; |
| 6128 | struct htx_sl *sl; |
| 6129 | |
| 6130 | MAY_LJMP(check_args(L, 1, "get_stline")); |
| 6131 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6132 | |
| 6133 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6134 | WILL_LJMP(lua_error(L)); |
| 6135 | |
| 6136 | htx = htxbuf(&msg->chn->buf); |
| 6137 | sl = http_get_stline(htx); |
| 6138 | if (!sl) |
| 6139 | return 0; |
| 6140 | return hlua_http_get_stline(L, sl); |
| 6141 | } |
| 6142 | |
| 6143 | /* Returns an array containing all headers of the HTTP message. it relies on |
| 6144 | * hlua_http_get_headers(). |
| 6145 | */ |
| 6146 | __LJMP static int hlua_http_msg_get_headers(lua_State *L) |
| 6147 | { |
| 6148 | struct http_msg *msg; |
| 6149 | |
| 6150 | MAY_LJMP(check_args(L, 1, "get_headers")); |
| 6151 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6152 | |
| 6153 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6154 | WILL_LJMP(lua_error(L)); |
| 6155 | |
| 6156 | return hlua_http_get_headers(L, msg); |
| 6157 | } |
| 6158 | |
| 6159 | /* Deletes all occurrences of an header in the HTTP message matching on its |
| 6160 | * name. It relies on hlua_http_del_hdr(). |
| 6161 | */ |
| 6162 | __LJMP static int hlua_http_msg_del_hdr(lua_State *L) |
| 6163 | { |
| 6164 | struct http_msg *msg; |
| 6165 | |
| 6166 | MAY_LJMP(check_args(L, 2, "del_header")); |
| 6167 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6168 | |
| 6169 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6170 | WILL_LJMP(lua_error(L)); |
| 6171 | |
| 6172 | return hlua_http_del_hdr(L, msg); |
| 6173 | } |
| 6174 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6175 | /* 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] | 6176 | * message given its name against a regex and replaces it if it matches. It |
| 6177 | * relies on hlua_http_rep_hdr(). |
| 6178 | */ |
| 6179 | __LJMP static int hlua_http_msg_rep_hdr(lua_State *L) |
| 6180 | { |
| 6181 | struct http_msg *msg; |
| 6182 | |
| 6183 | MAY_LJMP(check_args(L, 4, "rep_header")); |
| 6184 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6185 | |
| 6186 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6187 | WILL_LJMP(lua_error(L)); |
| 6188 | |
| 6189 | return hlua_http_rep_hdr(L, msg, 1); |
| 6190 | } |
| 6191 | |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 6192 | /* 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] | 6193 | * message given its name against a regex and replaces it if it matches. It |
| 6194 | * relies on hlua_http_rep_hdr(). |
| 6195 | */ |
| 6196 | __LJMP static int hlua_http_msg_rep_val(lua_State *L) |
| 6197 | { |
| 6198 | struct http_msg *msg; |
| 6199 | |
| 6200 | MAY_LJMP(check_args(L, 4, "rep_value")); |
| 6201 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6202 | |
| 6203 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6204 | WILL_LJMP(lua_error(L)); |
| 6205 | |
| 6206 | return hlua_http_rep_hdr(L, msg, 0); |
| 6207 | } |
| 6208 | |
| 6209 | /* Add an header in the HTTP message. It relies on hlua_http_add_hdr() */ |
| 6210 | __LJMP static int hlua_http_msg_add_hdr(lua_State *L) |
| 6211 | { |
| 6212 | struct http_msg *msg; |
| 6213 | |
| 6214 | MAY_LJMP(check_args(L, 3, "add_header")); |
| 6215 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6216 | |
| 6217 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6218 | WILL_LJMP(lua_error(L)); |
| 6219 | |
| 6220 | return hlua_http_add_hdr(L, msg); |
| 6221 | } |
| 6222 | |
| 6223 | /* Add an header in the HTTP message removing existing headers with the same |
| 6224 | * name. It relies on hlua_http_del_hdr() and hlua_http_add_hdr(). |
| 6225 | */ |
| 6226 | __LJMP static int hlua_http_msg_set_hdr(lua_State *L) |
| 6227 | { |
| 6228 | struct http_msg *msg; |
| 6229 | |
| 6230 | MAY_LJMP(check_args(L, 3, "set_header")); |
| 6231 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6232 | |
| 6233 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6234 | WILL_LJMP(lua_error(L)); |
| 6235 | |
| 6236 | hlua_http_del_hdr(L, msg); |
| 6237 | return hlua_http_add_hdr(L, msg); |
| 6238 | } |
| 6239 | |
| 6240 | /* Rewrites the request method. It relies on http_req_replace_stline(). */ |
| 6241 | __LJMP static int hlua_http_msg_set_meth(lua_State *L) |
| 6242 | { |
| 6243 | struct stream *s; |
| 6244 | struct http_msg *msg; |
| 6245 | const char *name; |
| 6246 | size_t name_len; |
| 6247 | |
| 6248 | MAY_LJMP(check_args(L, 2, "set_method")); |
| 6249 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6250 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6251 | |
| 6252 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6253 | WILL_LJMP(lua_error(L)); |
| 6254 | |
| 6255 | s = chn_strm(msg->chn); |
| 6256 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, s->be, s) != -1); |
| 6257 | return 1; |
| 6258 | } |
| 6259 | |
| 6260 | /* Rewrites the request path. It relies on http_req_replace_stline(). */ |
| 6261 | __LJMP static int hlua_http_msg_set_path(lua_State *L) |
| 6262 | { |
| 6263 | struct stream *s; |
| 6264 | struct http_msg *msg; |
| 6265 | const char *name; |
| 6266 | size_t name_len; |
| 6267 | |
| 6268 | MAY_LJMP(check_args(L, 2, "set_path")); |
| 6269 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6270 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6271 | |
| 6272 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6273 | WILL_LJMP(lua_error(L)); |
| 6274 | |
| 6275 | s = chn_strm(msg->chn); |
| 6276 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, s->be, s) != -1); |
| 6277 | return 1; |
| 6278 | } |
| 6279 | |
| 6280 | /* Rewrites the request query-string. It relies on http_req_replace_stline(). */ |
| 6281 | __LJMP static int hlua_http_msg_set_query(lua_State *L) |
| 6282 | { |
| 6283 | struct stream *s; |
| 6284 | struct http_msg *msg; |
| 6285 | const char *name; |
| 6286 | size_t name_len; |
| 6287 | |
| 6288 | MAY_LJMP(check_args(L, 2, "set_query")); |
| 6289 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6290 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6291 | |
| 6292 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6293 | WILL_LJMP(lua_error(L)); |
| 6294 | |
| 6295 | /* Check length. */ |
| 6296 | if (name_len > trash.size - 1) { |
| 6297 | lua_pushboolean(L, 0); |
| 6298 | return 1; |
| 6299 | } |
| 6300 | |
| 6301 | /* Add the mark question as prefix. */ |
| 6302 | chunk_reset(&trash); |
| 6303 | trash.area[trash.data++] = '?'; |
| 6304 | memcpy(trash.area + trash.data, name, name_len); |
| 6305 | trash.data += name_len; |
| 6306 | |
| 6307 | s = chn_strm(msg->chn); |
| 6308 | lua_pushboolean(L, http_req_replace_stline(2, trash.area, trash.data, s->be, s) != -1); |
| 6309 | return 1; |
| 6310 | } |
| 6311 | |
| 6312 | /* Rewrites the request URI. It relies on http_req_replace_stline(). */ |
| 6313 | __LJMP static int hlua_http_msg_set_uri(lua_State *L) |
| 6314 | { |
| 6315 | struct stream *s; |
| 6316 | struct http_msg *msg; |
| 6317 | const char *name; |
| 6318 | size_t name_len; |
| 6319 | |
| 6320 | MAY_LJMP(check_args(L, 2, "set_uri")); |
| 6321 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6322 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6323 | |
| 6324 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6325 | WILL_LJMP(lua_error(L)); |
| 6326 | |
| 6327 | s = chn_strm(msg->chn); |
| 6328 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, s->be, s) != -1); |
| 6329 | return 1; |
| 6330 | } |
| 6331 | |
| 6332 | /* Rewrites the response status code. It relies on http_res_set_status(). */ |
| 6333 | __LJMP static int hlua_http_msg_set_status(lua_State *L) |
| 6334 | { |
| 6335 | struct http_msg *msg; |
| 6336 | unsigned int code; |
| 6337 | const char *reason; |
| 6338 | size_t reason_len; |
| 6339 | |
| 6340 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6341 | code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6342 | reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, &reason_len)); |
| 6343 | |
| 6344 | if (!(msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6345 | WILL_LJMP(lua_error(L)); |
| 6346 | |
| 6347 | lua_pushboolean(L, http_res_set_status(code, ist2(reason, reason_len), chn_strm(msg->chn)) != -1); |
| 6348 | return 1; |
| 6349 | } |
| 6350 | |
| 6351 | /* Returns true if the HTTP message is full. */ |
| 6352 | __LJMP static int hlua_http_msg_is_full(lua_State *L) |
| 6353 | { |
| 6354 | struct http_msg *msg; |
| 6355 | |
| 6356 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 6357 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6358 | lua_pushboolean(L, channel_full(msg->chn, 0)); |
| 6359 | return 1; |
| 6360 | } |
| 6361 | |
| 6362 | /* Returns true if the HTTP message may still receive data. */ |
| 6363 | __LJMP static int hlua_http_msg_may_recv(lua_State *L) |
| 6364 | { |
| 6365 | struct http_msg *msg; |
| 6366 | struct htx *htx; |
| 6367 | |
| 6368 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6369 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6370 | htx = htxbuf(&msg->chn->buf); |
| 6371 | lua_pushboolean(L, (htx_expect_more(htx) && !channel_input_closed(msg->chn) && channel_may_recv(msg->chn))); |
| 6372 | return 1; |
| 6373 | } |
| 6374 | |
| 6375 | /* Returns true if the HTTP message EOM was received */ |
| 6376 | __LJMP static int hlua_http_msg_is_eom(lua_State *L) |
| 6377 | { |
| 6378 | struct http_msg *msg; |
| 6379 | struct htx *htx; |
| 6380 | |
| 6381 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6382 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6383 | htx = htxbuf(&msg->chn->buf); |
| 6384 | lua_pushboolean(L, !htx_expect_more(htx)); |
| 6385 | return 1; |
| 6386 | } |
| 6387 | |
| 6388 | /* Returns the number of bytes available in the input side of the HTTP |
| 6389 | * message. This function never fails. |
| 6390 | */ |
| 6391 | __LJMP static int hlua_http_msg_get_in_len(lua_State *L) |
| 6392 | { |
| 6393 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6394 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6395 | |
| 6396 | MAY_LJMP(check_args(L, 1, "input")); |
| 6397 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6398 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6399 | lua_pushinteger(L, input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6400 | return 1; |
| 6401 | } |
| 6402 | |
| 6403 | /* Returns the number of bytes available in the output side of the HTTP |
| 6404 | * message. This function never fails. |
| 6405 | */ |
| 6406 | __LJMP static int hlua_http_msg_get_out_len(lua_State *L) |
| 6407 | { |
| 6408 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6409 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6410 | |
| 6411 | MAY_LJMP(check_args(L, 1, "output")); |
| 6412 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6413 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6414 | lua_pushinteger(L, output); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6415 | return 1; |
| 6416 | } |
| 6417 | |
| 6418 | /* Copies at most <len> bytes of DATA blocks from the HTTP message <msg> |
| 6419 | * 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] | 6420 | * 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] | 6421 | * block. This function is called during the payload filtering, so the headers |
| 6422 | * are already scheduled for output (from the filter point of view). |
| 6423 | */ |
| 6424 | static int _hlua_http_msg_dup(struct http_msg *msg, lua_State *L, size_t offset, size_t len) |
| 6425 | { |
| 6426 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 6427 | struct htx_blk *blk; |
| 6428 | struct htx_ret htxret; |
| 6429 | luaL_Buffer b; |
| 6430 | int ret = 0; |
| 6431 | |
| 6432 | luaL_buffinit(L, &b); |
| 6433 | htxret = htx_find_offset(htx, offset); |
| 6434 | for (blk = htxret.blk, offset = htxret.ret; blk && len; blk = htx_get_next_blk(htx, blk)) { |
| 6435 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6436 | struct ist v; |
| 6437 | |
| 6438 | switch (type) { |
| 6439 | case HTX_BLK_UNUSED: |
| 6440 | break; |
| 6441 | |
| 6442 | case HTX_BLK_DATA: |
| 6443 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6444 | v = istadv(v, offset); |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6445 | v = isttrim(v, len); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6446 | |
| 6447 | luaL_addlstring(&b, v.ptr, v.len); |
| 6448 | ret += v.len; |
| 6449 | break; |
| 6450 | |
| 6451 | default: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6452 | if (!ret) |
| 6453 | goto no_data; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6454 | goto end; |
| 6455 | } |
| 6456 | offset = 0; |
| 6457 | } |
| 6458 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6459 | end: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6460 | if (!ret && (htx->flags & HTX_FL_EOM)) |
| 6461 | goto no_data; |
| 6462 | luaL_pushresult(&b); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6463 | return ret; |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6464 | |
| 6465 | no_data: |
| 6466 | /* Remove the empty string and push nil on the stack */ |
| 6467 | lua_pop(L, 1); |
| 6468 | lua_pushnil(L); |
| 6469 | return 0; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6470 | } |
| 6471 | |
| 6472 | /* Copies the string <str> to the HTTP message <msg> at the offset |
| 6473 | * <offset>. This function returns -1 if data cannot be copied. Otherwise, it |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6474 | * returns the amount of data written. This function is responsible to update |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6475 | * the filter context. |
| 6476 | */ |
| 6477 | static int _hlua_http_msg_insert(struct http_msg *msg, struct filter *filter, struct ist str, size_t offset) |
| 6478 | { |
| 6479 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6480 | struct htx_ret htxret; |
| 6481 | int /*max, */ret = 0; |
| 6482 | |
| 6483 | /* Nothing to do, just return */ |
| 6484 | if (unlikely(istlen(str) == 0)) |
| 6485 | goto end; |
| 6486 | |
| 6487 | if (istlen(str) > htx_free_data_space(htx)) { |
| 6488 | ret = -1; |
| 6489 | goto end; |
| 6490 | } |
| 6491 | |
| 6492 | htxret = htx_find_offset(htx, offset); |
| 6493 | if (!htxret.blk || htx_get_blk_type(htxret.blk) != HTX_BLK_DATA) { |
| 6494 | if (!htx_add_last_data(htx, str)) |
| 6495 | goto end; |
| 6496 | } |
| 6497 | else { |
| 6498 | struct ist v = htx_get_blk_value(htx, htxret.blk); |
| 6499 | v.ptr += htxret.ret; |
| 6500 | v.len = 0; |
| 6501 | if (!htx_replace_blk_value(htx, htxret.blk, v, str)) |
| 6502 | goto end; |
| 6503 | } |
| 6504 | ret = str.len; |
| 6505 | if (ret) { |
| 6506 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6507 | flt_update_offsets(filter, msg->chn, ret); |
| 6508 | flt_ctx->cur_len[CHN_IDX(msg->chn)] += ret; |
| 6509 | } |
| 6510 | |
| 6511 | end: |
| 6512 | htx_to_buf(htx, &msg->chn->buf); |
| 6513 | return ret; |
| 6514 | } |
| 6515 | |
| 6516 | /* Helper function removing at most <len> bytes of DATA blocks at the absolute |
| 6517 | * position <offset>. It stops on the first non-DATA HTX block. This function is |
| 6518 | * called during the payload filtering, so the headers are already scheduled for |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6519 | * output (from the filter point of view). This function is responsible to |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6520 | * update the filter context. |
| 6521 | */ |
| 6522 | static void _hlua_http_msg_delete(struct http_msg *msg, struct filter *filter, size_t offset, size_t len) |
| 6523 | { |
| 6524 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6525 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6526 | struct htx_blk *blk; |
| 6527 | struct htx_ret htxret; |
| 6528 | size_t ret = 0; |
| 6529 | |
| 6530 | /* Be sure <len> is always the amount of DATA to remove */ |
| 6531 | if (htx->data == offset+len && htx_get_tail_type(htx) == HTX_BLK_DATA) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6532 | /* When htx tail type == HTX_BLK_DATA, no need to take care |
| 6533 | * of special blocks like HTX_BLK_EOT. |
| 6534 | * We simply truncate after offset |
| 6535 | * (truncate targeted blk and discard the following ones) |
| 6536 | */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6537 | htx_truncate(htx, offset); |
| 6538 | ret = len; |
| 6539 | goto end; |
| 6540 | } |
| 6541 | |
| 6542 | htxret = htx_find_offset(htx, offset); |
| 6543 | blk = htxret.blk; |
| 6544 | if (htxret.ret) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6545 | /* dealing with offset: we need to trim targeted blk */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6546 | struct ist v; |
| 6547 | |
| 6548 | if (htx_get_blk_type(blk) != HTX_BLK_DATA) |
| 6549 | goto end; |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6550 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6551 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | a029d78 | 2022-10-08 12:33:18 +0200 | [diff] [blame] | 6552 | v = istadv(v, htxret.ret); |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6553 | |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6554 | v = isttrim(v, len); |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6555 | /* trimming data in blk: discard everything after the offset |
| 6556 | * (replace 'v' with 'IST_NULL') |
| 6557 | */ |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6558 | blk = htx_replace_blk_value(htx, blk, v, IST_NULL); |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6559 | if (blk && v.len < len) { |
| 6560 | /* In this case, caller wants to keep removing data, |
| 6561 | * but we need to spare current blk |
| 6562 | * because it was already trimmed |
| 6563 | */ |
| 6564 | blk = htx_get_next_blk(htx, blk); |
| 6565 | } |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6566 | len -= v.len; |
| 6567 | ret += v.len; |
| 6568 | } |
| 6569 | |
| 6570 | |
| 6571 | while (blk && len) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6572 | /* there is more data that needs to be discarded */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6573 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6574 | uint32_t sz = htx_get_blksz(blk); |
| 6575 | |
| 6576 | switch (type) { |
| 6577 | case HTX_BLK_UNUSED: |
| 6578 | break; |
| 6579 | |
| 6580 | case HTX_BLK_DATA: |
| 6581 | if (len < sz) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6582 | /* don't discard whole blk, only part of it |
| 6583 | * (from the beginning) |
| 6584 | */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6585 | htx_cut_data_blk(htx, blk, len); |
| 6586 | ret += len; |
| 6587 | goto end; |
| 6588 | } |
| 6589 | break; |
| 6590 | |
| 6591 | default: |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6592 | /* HTX_BLK_EOT blk won't be removed */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6593 | goto end; |
| 6594 | } |
| 6595 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6596 | /* Remove all the data block */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6597 | len -= sz; |
| 6598 | ret += sz; |
| 6599 | blk = htx_remove_blk(htx, blk); |
| 6600 | } |
| 6601 | |
| 6602 | end: |
| 6603 | flt_update_offsets(filter, msg->chn, -ret); |
| 6604 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6605 | /* WARNING: we don't call htx_to_buf() on purpose, because we don't want |
| 6606 | * to loose the EOM flag if the message is empty. |
| 6607 | */ |
| 6608 | } |
| 6609 | |
| 6610 | /* Copies input data found in an HTTP message. Unlike the channel function used |
| 6611 | * to duplicate raw data, this one can only be called inside a filter, from |
| 6612 | * http_payload callback. So it cannot yield. An exception is returned if it is |
| 6613 | * called from another callback. If nothing was copied, a nil value is pushed on |
| 6614 | * the stack. |
| 6615 | */ |
| 6616 | __LJMP static int hlua_http_msg_get_body(lua_State *L) |
| 6617 | { |
| 6618 | struct http_msg *msg; |
| 6619 | struct filter *filter; |
| 6620 | size_t output, input; |
| 6621 | int offset, len; |
| 6622 | |
| 6623 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 6624 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 6625 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6626 | |
| 6627 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6628 | WILL_LJMP(lua_error(L)); |
| 6629 | |
| 6630 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6631 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6632 | WILL_LJMP(lua_error(L)); |
| 6633 | |
| 6634 | if (!ci_data(msg->chn) && channel_input_closed(msg->chn)) { |
| 6635 | lua_pushnil(L); |
| 6636 | return 1; |
| 6637 | } |
| 6638 | |
| 6639 | offset = output; |
| 6640 | if (lua_gettop(L) > 1) { |
| 6641 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6642 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6643 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6644 | offset += output; |
| 6645 | if (offset < output || offset > input + output) { |
| 6646 | lua_pushfstring(L, "offset out of range."); |
| 6647 | WILL_LJMP(lua_error(L)); |
| 6648 | } |
| 6649 | } |
| 6650 | len = output + input - offset; |
| 6651 | if (lua_gettop(L) == 3) { |
| 6652 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6653 | if (!len) |
| 6654 | goto dup; |
| 6655 | if (len == -1) |
| 6656 | len = global.tune.bufsize; |
| 6657 | if (len < 0) { |
| 6658 | lua_pushfstring(L, "length out of range."); |
| 6659 | WILL_LJMP(lua_error(L)); |
| 6660 | } |
| 6661 | } |
| 6662 | |
| 6663 | dup: |
| 6664 | _hlua_http_msg_dup(msg, L, offset, len); |
| 6665 | return 1; |
| 6666 | } |
| 6667 | |
| 6668 | /* Appends a string to the HTTP message, after all existing DATA blocks but |
| 6669 | * before the trailers, if any. It returns the amount of data written or -1 if |
| 6670 | * nothing was copied. Unlike the channel function used to append data, this one |
| 6671 | * can only be called inside a filter, from http_payload callback. So it cannot |
| 6672 | * yield. An exception is returned if it is called from another callback. |
| 6673 | */ |
| 6674 | __LJMP static int hlua_http_msg_append(lua_State *L) |
| 6675 | { |
| 6676 | struct http_msg *msg; |
| 6677 | struct filter *filter; |
| 6678 | const char *str; |
| 6679 | size_t offset, len, sz; |
| 6680 | int ret; |
| 6681 | |
| 6682 | MAY_LJMP(check_args(L, 2, "append")); |
| 6683 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6684 | |
| 6685 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6686 | WILL_LJMP(lua_error(L)); |
| 6687 | |
| 6688 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6689 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6690 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6691 | WILL_LJMP(lua_error(L)); |
| 6692 | |
| 6693 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset+len); |
| 6694 | lua_pushinteger(L, ret); |
| 6695 | return 1; |
| 6696 | } |
| 6697 | |
| 6698 | /* Prepends a string to the HTTP message, before all existing DATA blocks. It |
| 6699 | * returns the amount of data written or -1 if nothing was copied. Unlike the |
| 6700 | * channel function used to prepend data, this one can only be called inside a |
| 6701 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6702 | * returned if it is called from another callback. |
| 6703 | */ |
| 6704 | __LJMP static int hlua_http_msg_prepend(lua_State *L) |
| 6705 | { |
| 6706 | struct http_msg *msg; |
| 6707 | struct filter *filter; |
| 6708 | const char *str; |
| 6709 | size_t offset, len, sz; |
| 6710 | int ret; |
| 6711 | |
| 6712 | MAY_LJMP(check_args(L, 2, "prepend")); |
| 6713 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
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 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6716 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6717 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6718 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6719 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6720 | if (!filter || !hlua_filter_from_payload(filter)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6721 | WILL_LJMP(lua_error(L)); |
| 6722 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6723 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6724 | lua_pushinteger(L, ret); |
| 6725 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6726 | } |
| 6727 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6728 | /* Inserts a string to the HTTP message at a given offset. By default the string |
| 6729 | * is appended at the end of DATA blocks. It returns the amount of data written |
| 6730 | * or -1 if nothing was copied. Unlike the channel function used to insert data, |
| 6731 | * this one can only be called inside a filter, from http_payload callback. So |
| 6732 | * it cannot yield. An exception is returned if it is called from another |
| 6733 | * callback. |
| 6734 | */ |
| 6735 | __LJMP static int hlua_http_msg_insert_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6736 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6737 | struct http_msg *msg; |
| 6738 | struct filter *filter; |
| 6739 | const char *str; |
| 6740 | size_t input, output, sz; |
| 6741 | int offset; |
| 6742 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6743 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6744 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 6745 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6746 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6747 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6748 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6749 | WILL_LJMP(lua_error(L)); |
| 6750 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6751 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Aurelien DARRAGON | 7fdba0a | 2022-09-28 16:03:45 +0200 | [diff] [blame] | 6752 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6753 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6754 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6755 | |
Aurelien DARRAGON | 7fdba0a | 2022-09-28 16:03:45 +0200 | [diff] [blame] | 6756 | offset = output; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6757 | if (lua_gettop(L) > 2) { |
| 6758 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6759 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6760 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6761 | offset += output; |
Aurelien DARRAGON | 7fdba0a | 2022-09-28 16:03:45 +0200 | [diff] [blame] | 6762 | if (offset > output + input) { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6763 | lua_pushfstring(L, "offset out of range."); |
| 6764 | WILL_LJMP(lua_error(L)); |
| 6765 | } |
| 6766 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6767 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6768 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6769 | lua_pushinteger(L, ret); |
| 6770 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6771 | } |
| 6772 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6773 | /* Removes a given amount of data from the HTTP message at a given offset. By |
| 6774 | * default all DATA blocks are removed. It returns the amount of data |
| 6775 | * removed. Unlike the channel function used to remove data, this one can only |
| 6776 | * be called inside a filter, from http_payload callback. So it cannot yield. An |
| 6777 | * exception is returned if it is called from another callback. |
| 6778 | */ |
| 6779 | __LJMP static int hlua_http_msg_del_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6780 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6781 | struct http_msg *msg; |
| 6782 | struct filter *filter; |
| 6783 | size_t input, output; |
| 6784 | int offset, len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6785 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6786 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6787 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6788 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6789 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6790 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6791 | WILL_LJMP(lua_error(L)); |
| 6792 | |
Aurelien DARRAGON | d7c71b0 | 2022-09-28 15:52:18 +0200 | [diff] [blame] | 6793 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6794 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6795 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6796 | |
Aurelien DARRAGON | d7c71b0 | 2022-09-28 15:52:18 +0200 | [diff] [blame] | 6797 | offset = output; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6798 | if (lua_gettop(L) > 1) { |
| 6799 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6800 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6801 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6802 | offset += output; |
Aurelien DARRAGON | d7c71b0 | 2022-09-28 15:52:18 +0200 | [diff] [blame] | 6803 | if (offset > output + input) { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6804 | lua_pushfstring(L, "offset out of range."); |
| 6805 | WILL_LJMP(lua_error(L)); |
| 6806 | } |
| 6807 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6808 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6809 | len = output + input - offset; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6810 | if (lua_gettop(L) == 3) { |
| 6811 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6812 | if (!len) |
| 6813 | goto end; |
| 6814 | if (len == -1) |
| 6815 | len = output + input - offset; |
| 6816 | if (len < 0 || offset + len > output + input) { |
| 6817 | lua_pushfstring(L, "length out of range."); |
| 6818 | WILL_LJMP(lua_error(L)); |
| 6819 | } |
| 6820 | } |
| 6821 | |
| 6822 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6823 | |
| 6824 | end: |
| 6825 | lua_pushinteger(L, len); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6826 | return 1; |
| 6827 | } |
| 6828 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6829 | /* 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] | 6830 | * all remaining data are removed, accordingly to the filter context. It returns |
| 6831 | * the amount of data written or -1 if nothing was copied. Unlike the channel |
| 6832 | * function used to replace data, this one can only be called inside a filter, |
| 6833 | * from http_payload callback. So it cannot yield. An exception is returned if |
| 6834 | * it is called from another callback. |
| 6835 | */ |
| 6836 | __LJMP static int hlua_http_msg_set_data(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 input, output, sz; |
| 6843 | int offset, len; |
| 6844 | int ret; |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 6845 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6846 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 6847 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 6848 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6849 | |
| 6850 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6851 | WILL_LJMP(lua_error(L)); |
| 6852 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6853 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6854 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6855 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6856 | WILL_LJMP(lua_error(L)); |
| 6857 | |
| 6858 | offset = output; |
| 6859 | if (lua_gettop(L) > 2) { |
| 6860 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6861 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6862 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6863 | offset += output; |
| 6864 | if (offset < output || offset > input + output) { |
| 6865 | lua_pushfstring(L, "offset out of range."); |
| 6866 | WILL_LJMP(lua_error(L)); |
| 6867 | } |
| 6868 | } |
| 6869 | |
| 6870 | len = output + input - offset; |
| 6871 | if (lua_gettop(L) == 4) { |
| 6872 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6873 | if (!len) |
| 6874 | goto set; |
| 6875 | if (len == -1) |
| 6876 | len = output + input - offset; |
| 6877 | if (len < 0 || offset + len > output + input) { |
| 6878 | lua_pushfstring(L, "length out of range."); |
| 6879 | WILL_LJMP(lua_error(L)); |
| 6880 | } |
| 6881 | } |
| 6882 | |
| 6883 | set: |
| 6884 | /* Be sure we can copied the string once input data will be removed. */ |
| 6885 | htx = htx_from_buf(&msg->chn->buf); |
| 6886 | if (sz > htx_free_data_space(htx) + len) |
| 6887 | lua_pushinteger(L, -1); |
| 6888 | else { |
| 6889 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6890 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6891 | lua_pushinteger(L, ret); |
| 6892 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6893 | return 1; |
| 6894 | } |
| 6895 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6896 | /* Prepends data into an HTTP message and forward it, from the filter point of |
| 6897 | * view. It returns the amount of data written or -1 if nothing was sent. Unlike |
| 6898 | * the channel function used to send data, this one can only be called inside a |
| 6899 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6900 | * returned if it is called from another callback. |
| 6901 | */ |
| 6902 | __LJMP static int hlua_http_msg_send(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6903 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6904 | struct http_msg *msg; |
| 6905 | struct filter *filter; |
| 6906 | struct htx *htx; |
| 6907 | const char *str; |
| 6908 | size_t offset, len, sz; |
| 6909 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6910 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6911 | MAY_LJMP(check_args(L, 2, "send")); |
| 6912 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6913 | |
| 6914 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6915 | WILL_LJMP(lua_error(L)); |
| 6916 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6917 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6918 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6919 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6920 | WILL_LJMP(lua_error(L)); |
| 6921 | |
| 6922 | /* Return an error if the channel's output is closed */ |
| 6923 | if (unlikely(channel_output_closed(msg->chn))) { |
| 6924 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6925 | return 1; |
| 6926 | } |
| 6927 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6928 | htx = htx_from_buf(&msg->chn->buf); |
| 6929 | if (sz > htx_free_data_space(htx)) { |
| 6930 | lua_pushinteger(L, -1); |
| 6931 | return 1; |
| 6932 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6933 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6934 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6935 | if (ret > 0) { |
| 6936 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6937 | |
| 6938 | FLT_OFF(filter, msg->chn) += ret; |
| 6939 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6940 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 6941 | } |
| 6942 | |
| 6943 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6944 | return 1; |
| 6945 | } |
| 6946 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6947 | /* Forwards a given amount of bytes. It return -1 if the channel's output is |
| 6948 | * closed. Otherwise, it returns the number of bytes forwarded. Unlike the |
| 6949 | * channel function used to forward data, this one can only be called inside a |
| 6950 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6951 | * returned if it is called from another callback. All other functions deal with |
| 6952 | * DATA block, this one not. |
| 6953 | */ |
| 6954 | __LJMP static int hlua_http_msg_forward(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6955 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6956 | struct http_msg *msg; |
| 6957 | struct filter *filter; |
| 6958 | size_t offset, len; |
| 6959 | int fwd, ret = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6960 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6961 | MAY_LJMP(check_args(L, 2, "forward")); |
| 6962 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6963 | |
| 6964 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6965 | WILL_LJMP(lua_error(L)); |
| 6966 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6967 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6968 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6969 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6970 | WILL_LJMP(lua_error(L)); |
| 6971 | |
| 6972 | /* Nothing to do, just return */ |
| 6973 | if (!fwd) |
| 6974 | goto end; |
| 6975 | |
| 6976 | /* Return an error if the channel's output is closed */ |
| 6977 | if (unlikely(channel_output_closed(msg->chn))) { |
| 6978 | ret = -1; |
| 6979 | goto end; |
| 6980 | } |
| 6981 | |
| 6982 | ret = fwd; |
| 6983 | if (ret > len) |
| 6984 | ret = len; |
| 6985 | |
| 6986 | if (ret) { |
| 6987 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6988 | |
| 6989 | FLT_OFF(filter, msg->chn) += ret; |
| 6990 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 6991 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6992 | } |
| 6993 | |
| 6994 | end: |
| 6995 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6996 | return 1; |
| 6997 | } |
| 6998 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6999 | /* Set EOM flag on the HTX message. |
| 7000 | * |
| 7001 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 7002 | * really know how to do without this feature. |
| 7003 | */ |
| 7004 | __LJMP static int hlua_http_msg_set_eom(lua_State *L) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 7005 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7006 | struct http_msg *msg; |
| 7007 | struct htx *htx; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 7008 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7009 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 7010 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 7011 | htx = htxbuf(&msg->chn->buf); |
| 7012 | htx->flags |= HTX_FL_EOM; |
| 7013 | return 0; |
| 7014 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 7015 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7016 | /* Unset EOM flag on the HTX message. |
| 7017 | * |
| 7018 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 7019 | * really know how to do without this feature. |
| 7020 | */ |
| 7021 | __LJMP static int hlua_http_msg_unset_eom(lua_State *L) |
| 7022 | { |
| 7023 | struct http_msg *msg; |
| 7024 | struct htx *htx; |
| 7025 | |
| 7026 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 7027 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 7028 | htx = htxbuf(&msg->chn->buf); |
| 7029 | htx->flags &= ~HTX_FL_EOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 7030 | return 0; |
| 7031 | } |
| 7032 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7033 | /* |
| 7034 | * |
| 7035 | * |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7036 | * Class HTTPClient |
| 7037 | * |
| 7038 | * |
| 7039 | */ |
| 7040 | __LJMP static struct hlua_httpclient *hlua_checkhttpclient(lua_State *L, int ud) |
| 7041 | { |
| 7042 | return MAY_LJMP(hlua_checkudata(L, ud, class_httpclient_ref)); |
| 7043 | } |
| 7044 | |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 7045 | |
| 7046 | /* stops the httpclient and ask it to kill itself */ |
| 7047 | __LJMP static int hlua_httpclient_gc(lua_State *L) |
| 7048 | { |
| 7049 | struct hlua_httpclient *hlua_hc; |
| 7050 | |
| 7051 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 7052 | |
| 7053 | hlua_hc = MAY_LJMP(hlua_checkhttpclient(L, 1)); |
| 7054 | |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 7055 | if (hlua_hc->hc) |
| 7056 | httpclient_stop_and_destroy(hlua_hc->hc); |
| 7057 | |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 7058 | hlua_hc->hc = NULL; |
William Lallemand | 94dbfed | 2022-12-08 11:11:36 +0100 | [diff] [blame] | 7059 | LIST_DEL_INIT(&hlua_hc->by_hlua); |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 7060 | |
| 7061 | return 0; |
| 7062 | } |
| 7063 | |
| 7064 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7065 | __LJMP static int hlua_httpclient_new(lua_State *L) |
| 7066 | { |
| 7067 | struct hlua_httpclient *hlua_hc; |
| 7068 | struct hlua *hlua; |
| 7069 | |
| 7070 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 7071 | hlua = hlua_gethlua(L); |
| 7072 | if (!hlua) |
| 7073 | return 0; |
| 7074 | |
| 7075 | /* Check stack size. */ |
| 7076 | if (!lua_checkstack(L, 3)) { |
| 7077 | hlua_pusherror(L, "httpclient: full stack"); |
| 7078 | goto err; |
| 7079 | } |
| 7080 | /* Create the object: obj[0] = userdata. */ |
| 7081 | lua_newtable(L); |
| 7082 | hlua_hc = MAY_LJMP(lua_newuserdata(L, sizeof(*hlua_hc))); |
| 7083 | lua_rawseti(L, -2, 0); |
| 7084 | memset(hlua_hc, 0, sizeof(*hlua_hc)); |
| 7085 | |
| 7086 | hlua_hc->hc = httpclient_new(hlua, 0, IST_NULL); |
| 7087 | if (!hlua_hc->hc) |
| 7088 | goto err; |
| 7089 | |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 7090 | LIST_APPEND(&hlua->hc_list, &hlua_hc->by_hlua); |
| 7091 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7092 | /* Pop a class stream metatable and affect it to the userdata. */ |
| 7093 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_httpclient_ref); |
| 7094 | lua_setmetatable(L, -2); |
| 7095 | |
| 7096 | return 1; |
| 7097 | |
| 7098 | err: |
| 7099 | WILL_LJMP(lua_error(L)); |
| 7100 | return 0; |
| 7101 | } |
| 7102 | |
| 7103 | |
| 7104 | /* |
| 7105 | * Callback of the httpclient, this callback wakes the lua task up, once the |
| 7106 | * httpclient receives some data |
| 7107 | * |
| 7108 | */ |
| 7109 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7110 | static void hlua_httpclient_cb(struct httpclient *hc) |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7111 | { |
| 7112 | struct hlua *hlua = hc->caller; |
| 7113 | |
| 7114 | if (!hlua || !hlua->task) |
| 7115 | return; |
| 7116 | |
| 7117 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 7118 | } |
| 7119 | |
| 7120 | /* |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 7121 | * Fill the lua stack with headers from the httpclient response |
| 7122 | * This works the same way as the hlua_http_get_headers() function |
| 7123 | */ |
| 7124 | __LJMP static int hlua_httpclient_get_headers(lua_State *L, struct hlua_httpclient *hlua_hc) |
| 7125 | { |
| 7126 | struct http_hdr *hdr; |
| 7127 | |
| 7128 | lua_newtable(L); |
| 7129 | |
William Lallemand | ef574b2 | 2021-10-05 16:19:31 +0200 | [diff] [blame] | 7130 | for (hdr = hlua_hc->hc->res.hdrs; hdr && isttest(hdr->n); hdr++) { |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 7131 | struct ist n, v; |
| 7132 | int len; |
| 7133 | |
| 7134 | n = hdr->n; |
| 7135 | v = hdr->v; |
| 7136 | |
| 7137 | /* Check for existing entry: |
| 7138 | * assume that the table is on the top of the stack, and |
| 7139 | * push the key in the stack, the function lua_gettable() |
| 7140 | * perform the lookup. |
| 7141 | */ |
| 7142 | |
| 7143 | lua_pushlstring(L, n.ptr, n.len); |
| 7144 | lua_gettable(L, -2); |
| 7145 | |
| 7146 | switch (lua_type(L, -1)) { |
| 7147 | case LUA_TNIL: |
| 7148 | /* Table not found, create it. */ |
| 7149 | lua_pop(L, 1); /* remove the nil value. */ |
| 7150 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 7151 | lua_newtable(L); /* create and push empty table. */ |
| 7152 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7153 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 7154 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 7155 | break; |
| 7156 | |
| 7157 | case LUA_TTABLE: |
| 7158 | /* Entry found: push the value in the table. */ |
| 7159 | len = lua_rawlen(L, -1); |
| 7160 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7161 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 7162 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 7163 | break; |
| 7164 | |
| 7165 | default: |
| 7166 | /* Other cases are errors. */ |
| 7167 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 7168 | WILL_LJMP(lua_error(L)); |
| 7169 | } |
| 7170 | } |
| 7171 | return 1; |
| 7172 | } |
| 7173 | |
| 7174 | /* |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7175 | * Allocate and return an array of http_hdr ist extracted from the <headers> lua table |
| 7176 | * |
| 7177 | * Caller must free the result |
| 7178 | */ |
| 7179 | struct http_hdr *hlua_httpclient_table_to_hdrs(lua_State *L) |
| 7180 | { |
| 7181 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
| 7182 | struct http_hdr *result = NULL; |
| 7183 | uint32_t hdr_num = 0; |
| 7184 | |
| 7185 | lua_pushnil(L); |
| 7186 | while (lua_next(L, -2) != 0) { |
| 7187 | struct ist name, value; |
| 7188 | const char *n, *v; |
| 7189 | size_t nlen, vlen; |
| 7190 | |
| 7191 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 7192 | /* Skip element if the key is not a string or if the value is not a table */ |
| 7193 | goto next_hdr; |
| 7194 | } |
| 7195 | |
| 7196 | n = lua_tolstring(L, -2, &nlen); |
| 7197 | name = ist2(n, nlen); |
| 7198 | |
| 7199 | /* Loop on header's values */ |
| 7200 | lua_pushnil(L); |
| 7201 | while (lua_next(L, -2)) { |
| 7202 | if (!lua_isstring(L, -1)) { |
| 7203 | /* Skip the value if it is not a string */ |
| 7204 | goto next_value; |
| 7205 | } |
| 7206 | |
| 7207 | v = lua_tolstring(L, -1, &vlen); |
| 7208 | value = ist2(v, vlen); |
| 7209 | name = ist2(n, nlen); |
| 7210 | |
| 7211 | hdrs[hdr_num].n = istdup(name); |
| 7212 | hdrs[hdr_num].v = istdup(value); |
| 7213 | |
| 7214 | hdr_num++; |
| 7215 | |
| 7216 | next_value: |
| 7217 | lua_pop(L, 1); |
| 7218 | } |
| 7219 | |
| 7220 | next_hdr: |
| 7221 | lua_pop(L, 1); |
| 7222 | |
| 7223 | } |
| 7224 | |
| 7225 | if (hdr_num) { |
| 7226 | /* alloc and copy the headers in the httpclient struct */ |
Tim Duesterhus | 16cc16d | 2021-11-06 15:14:45 +0100 | [diff] [blame] | 7227 | result = calloc((hdr_num + 1), sizeof(*result)); |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7228 | if (!result) |
| 7229 | goto skip_headers; |
| 7230 | memcpy(result, hdrs, sizeof(struct http_hdr) * (hdr_num + 1)); |
| 7231 | |
| 7232 | result[hdr_num].n = IST_NULL; |
| 7233 | result[hdr_num].v = IST_NULL; |
| 7234 | } |
| 7235 | |
| 7236 | skip_headers: |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7237 | |
| 7238 | return result; |
| 7239 | } |
| 7240 | |
| 7241 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7242 | /* |
| 7243 | * For each yield, checks if there is some data in the httpclient and push them |
| 7244 | * in the lua buffer, once the httpclient finished its job, push the result on |
| 7245 | * the stack |
| 7246 | */ |
| 7247 | __LJMP static int hlua_httpclient_rcv_yield(lua_State *L, int status, lua_KContext ctx) |
| 7248 | { |
| 7249 | struct buffer *tr; |
| 7250 | int res; |
| 7251 | struct hlua *hlua = hlua_gethlua(L); |
| 7252 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7253 | |
| 7254 | |
| 7255 | tr = get_trash_chunk(); |
| 7256 | |
| 7257 | res = httpclient_res_xfer(hlua_hc->hc, tr); |
| 7258 | luaL_addlstring(&hlua_hc->b, b_orig(tr), res); |
| 7259 | |
| 7260 | if (!httpclient_data(hlua_hc->hc) && httpclient_ended(hlua_hc->hc)) { |
| 7261 | |
| 7262 | luaL_pushresult(&hlua_hc->b); |
| 7263 | lua_settable(L, -3); |
| 7264 | |
| 7265 | lua_pushstring(L, "status"); |
| 7266 | lua_pushinteger(L, hlua_hc->hc->res.status); |
| 7267 | lua_settable(L, -3); |
| 7268 | |
| 7269 | |
| 7270 | lua_pushstring(L, "reason"); |
| 7271 | lua_pushlstring(L, hlua_hc->hc->res.reason.ptr, hlua_hc->hc->res.reason.len); |
| 7272 | lua_settable(L, -3); |
| 7273 | |
| 7274 | lua_pushstring(L, "headers"); |
| 7275 | hlua_httpclient_get_headers(L, hlua_hc); |
| 7276 | lua_settable(L, -3); |
| 7277 | |
| 7278 | return 1; |
| 7279 | } |
| 7280 | |
| 7281 | if (httpclient_data(hlua_hc->hc)) |
| 7282 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 7283 | |
| 7284 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7285 | |
| 7286 | return 0; |
| 7287 | } |
| 7288 | |
| 7289 | /* |
| 7290 | * Call this when trying to stream a body during a request |
| 7291 | */ |
| 7292 | __LJMP static int hlua_httpclient_snd_yield(lua_State *L, int status, lua_KContext ctx) |
| 7293 | { |
| 7294 | struct hlua *hlua; |
| 7295 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7296 | const char *body_str = NULL; |
| 7297 | int ret; |
| 7298 | int end = 0; |
| 7299 | size_t buf_len; |
| 7300 | size_t to_send = 0; |
| 7301 | |
| 7302 | hlua = hlua_gethlua(L); |
| 7303 | |
| 7304 | if (!hlua || !hlua->task) |
| 7305 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7306 | "'frontend', 'backend' or 'task'")); |
| 7307 | |
| 7308 | ret = lua_getfield(L, -1, "body"); |
| 7309 | if (ret != LUA_TSTRING) |
| 7310 | goto rcv; |
| 7311 | |
| 7312 | body_str = lua_tolstring(L, -1, &buf_len); |
| 7313 | lua_pop(L, 1); |
| 7314 | |
Christopher Faulet | fc59129 | 2022-01-12 14:46:03 +0100 | [diff] [blame] | 7315 | to_send = buf_len - hlua_hc->sent; |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7316 | |
| 7317 | if ((hlua_hc->sent + to_send) >= buf_len) |
| 7318 | end = 1; |
| 7319 | |
| 7320 | /* the end flag is always set since we are using the whole remaining size */ |
| 7321 | hlua_hc->sent += httpclient_req_xfer(hlua_hc->hc, ist2(body_str + hlua_hc->sent, to_send), end); |
| 7322 | |
| 7323 | if (buf_len > hlua_hc->sent) { |
| 7324 | /* still need to process the buffer */ |
| 7325 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_snd_yield, TICK_ETERNITY, 0)); |
| 7326 | } else { |
| 7327 | goto rcv; |
| 7328 | /* we sent the whole request buffer we can recv */ |
| 7329 | } |
| 7330 | return 0; |
| 7331 | |
| 7332 | rcv: |
| 7333 | |
| 7334 | /* we return a "res" object */ |
| 7335 | lua_newtable(L); |
| 7336 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7337 | lua_pushstring(L, "body"); |
William Lallemand | d1187eb | 2021-11-02 10:40:06 +0100 | [diff] [blame] | 7338 | luaL_buffinit(L, &hlua_hc->b); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7339 | |
William Lallemand | 933fe39 | 2021-11-04 09:45:58 +0100 | [diff] [blame] | 7340 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7341 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7342 | |
| 7343 | return 1; |
| 7344 | } |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7345 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7346 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7347 | * Send an HTTP request and wait for a response |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7348 | */ |
| 7349 | |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7350 | __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] | 7351 | { |
| 7352 | struct hlua_httpclient *hlua_hc; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7353 | struct http_hdr *hdrs = NULL; |
| 7354 | struct http_hdr *hdrs_i = NULL; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7355 | struct hlua *hlua; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7356 | const char *url_str = NULL; |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7357 | const char *body_str = NULL; |
Christopher Faulet | 380ae9c | 2022-10-14 14:57:04 +0200 | [diff] [blame] | 7358 | size_t buf_len = 0; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7359 | int ret; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7360 | |
| 7361 | hlua = hlua_gethlua(L); |
| 7362 | |
| 7363 | if (!hlua || !hlua->task) |
| 7364 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7365 | "'frontend', 'backend' or 'task'")); |
| 7366 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7367 | if (lua_gettop(L) != 2 || lua_type(L, -1) != LUA_TTABLE) |
| 7368 | WILL_LJMP(luaL_error(L, "'get' needs a table as argument")); |
| 7369 | |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 7370 | hlua_hc = hlua_checkhttpclient(L, 1); |
| 7371 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7372 | lua_pushnil(L); /* first key */ |
| 7373 | while (lua_next(L, 2)) { |
| 7374 | if (strcmp(lua_tostring(L, -2), "dst") == 0) { |
| 7375 | if (httpclient_set_dst(hlua_hc->hc, lua_tostring(L, -1)) < 0) |
| 7376 | WILL_LJMP(luaL_error(L, "Can't use the 'dst' argument")); |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 7377 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7378 | } else if (strcmp(lua_tostring(L, -2), "url") == 0) { |
| 7379 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7380 | WILL_LJMP(luaL_error(L, "invalid parameter in 'url', must be a string")); |
| 7381 | url_str = lua_tostring(L, -1); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7382 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7383 | } else if (strcmp(lua_tostring(L, -2), "timeout") == 0) { |
| 7384 | if (lua_type(L, -1) != LUA_TNUMBER) |
| 7385 | WILL_LJMP(luaL_error(L, "invalid parameter in 'timeout', must be a number")); |
| 7386 | httpclient_set_timeout(hlua_hc->hc, lua_tointeger(L, -1)); |
William Lallemand | b4a4ef6 | 2022-02-23 14:18:16 +0100 | [diff] [blame] | 7387 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7388 | } else if (strcmp(lua_tostring(L, -2), "headers") == 0) { |
| 7389 | if (lua_type(L, -1) != LUA_TTABLE) |
| 7390 | WILL_LJMP(luaL_error(L, "invalid parameter in 'headers', must be a table")); |
| 7391 | hdrs = hlua_httpclient_table_to_hdrs(L); |
William Lallemand | 79416cb | 2021-09-24 14:51:44 +0200 | [diff] [blame] | 7392 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7393 | } else if (strcmp(lua_tostring(L, -2), "body") == 0) { |
| 7394 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7395 | WILL_LJMP(luaL_error(L, "invalid parameter in 'body', must be a string")); |
| 7396 | body_str = lua_tolstring(L, -1, &buf_len); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7397 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7398 | } else { |
| 7399 | WILL_LJMP(luaL_error(L, "'%s' invalid parameter name", lua_tostring(L, -2))); |
| 7400 | } |
| 7401 | /* removes 'value'; keeps 'key' for next iteration */ |
| 7402 | lua_pop(L, 1); |
| 7403 | } |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7404 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7405 | if (!url_str) { |
| 7406 | WILL_LJMP(luaL_error(L, "'get' need a 'url' argument")); |
| 7407 | return 0; |
| 7408 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7409 | |
William Lallemand | 10a3736 | 2022-03-02 16:18:26 +0100 | [diff] [blame] | 7410 | hlua_hc->sent = 0; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7411 | |
| 7412 | hlua_hc->hc->req.url = istdup(ist(url_str)); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7413 | hlua_hc->hc->req.meth = meth; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7414 | |
| 7415 | /* update the httpclient callbacks */ |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7416 | hlua_hc->hc->ops.res_stline = hlua_httpclient_cb; |
| 7417 | hlua_hc->hc->ops.res_headers = hlua_httpclient_cb; |
| 7418 | hlua_hc->hc->ops.res_payload = hlua_httpclient_cb; |
William Lallemand | 8f170c7 | 2022-03-15 10:52:07 +0100 | [diff] [blame] | 7419 | hlua_hc->hc->ops.res_end = hlua_httpclient_cb; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7420 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7421 | /* a body is available, it will use the request callback */ |
Christopher Faulet | 380ae9c | 2022-10-14 14:57:04 +0200 | [diff] [blame] | 7422 | if (body_str && buf_len) { |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7423 | hlua_hc->hc->ops.req_payload = hlua_httpclient_cb; |
| 7424 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7425 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7426 | 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] | 7427 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7428 | /* free the temporary headers array */ |
| 7429 | hdrs_i = hdrs; |
| 7430 | while (hdrs_i && isttest(hdrs_i->n)) { |
| 7431 | istfree(&hdrs_i->n); |
| 7432 | istfree(&hdrs_i->v); |
| 7433 | hdrs_i++; |
| 7434 | } |
| 7435 | ha_free(&hdrs); |
| 7436 | |
| 7437 | |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7438 | if (ret != ERR_NONE) { |
| 7439 | WILL_LJMP(luaL_error(L, "Can't generate the HTTP request")); |
| 7440 | return 0; |
| 7441 | } |
| 7442 | |
William Lallemand | c2d3db4 | 2022-04-26 11:46:13 +0200 | [diff] [blame] | 7443 | if (!httpclient_start(hlua_hc->hc)) |
| 7444 | WILL_LJMP(luaL_error(L, "couldn't start the httpclient")); |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7445 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7446 | 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] | 7447 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7448 | return 0; |
| 7449 | } |
| 7450 | |
| 7451 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7452 | * Sends an HTTP HEAD request and wait for a response |
| 7453 | * |
| 7454 | * httpclient:head(url, headers, payload) |
| 7455 | */ |
| 7456 | __LJMP static int hlua_httpclient_head(lua_State *L) |
| 7457 | { |
| 7458 | return hlua_httpclient_send(L, HTTP_METH_HEAD); |
| 7459 | } |
| 7460 | |
| 7461 | /* |
| 7462 | * Send an HTTP GET request and wait for a response |
| 7463 | * |
| 7464 | * httpclient:get(url, headers, payload) |
| 7465 | */ |
| 7466 | __LJMP static int hlua_httpclient_get(lua_State *L) |
| 7467 | { |
| 7468 | return hlua_httpclient_send(L, HTTP_METH_GET); |
| 7469 | |
| 7470 | } |
| 7471 | |
| 7472 | /* |
| 7473 | * Sends an HTTP PUT request and wait for a response |
| 7474 | * |
| 7475 | * httpclient:put(url, headers, payload) |
| 7476 | */ |
| 7477 | __LJMP static int hlua_httpclient_put(lua_State *L) |
| 7478 | { |
| 7479 | return hlua_httpclient_send(L, HTTP_METH_PUT); |
| 7480 | } |
| 7481 | |
| 7482 | /* |
| 7483 | * Send an HTTP POST request and wait for a response |
| 7484 | * |
| 7485 | * httpclient:post(url, headers, payload) |
| 7486 | */ |
| 7487 | __LJMP static int hlua_httpclient_post(lua_State *L) |
| 7488 | { |
| 7489 | return hlua_httpclient_send(L, HTTP_METH_POST); |
| 7490 | } |
| 7491 | |
| 7492 | |
| 7493 | /* |
| 7494 | * Sends an HTTP DELETE request and wait for a response |
| 7495 | * |
| 7496 | * httpclient:delete(url, headers, payload) |
| 7497 | */ |
| 7498 | __LJMP static int hlua_httpclient_delete(lua_State *L) |
| 7499 | { |
| 7500 | return hlua_httpclient_send(L, HTTP_METH_DELETE); |
| 7501 | } |
| 7502 | |
| 7503 | /* |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7504 | * |
| 7505 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7506 | * Class TXN |
| 7507 | * |
| 7508 | * |
| 7509 | */ |
| 7510 | |
| 7511 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7512 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7513 | */ |
| 7514 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 7515 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7516 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7517 | } |
| 7518 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7519 | __LJMP static int hlua_set_var(lua_State *L) |
| 7520 | { |
| 7521 | struct hlua_txn *htxn; |
| 7522 | const char *name; |
| 7523 | size_t len; |
| 7524 | struct sample smp; |
| 7525 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 7526 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7527 | 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] | 7528 | |
| 7529 | /* It is useles to retrieve the stream, but this function |
| 7530 | * runs only in a stream context. |
| 7531 | */ |
| 7532 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7533 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7534 | |
| 7535 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 7536 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7537 | hlua_lua2smp(L, 3, &smp); |
| 7538 | |
| 7539 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7540 | 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] | 7541 | |
| 7542 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 7543 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 7544 | else |
| 7545 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 7546 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 7547 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7548 | } |
| 7549 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7550 | __LJMP static int hlua_unset_var(lua_State *L) |
| 7551 | { |
| 7552 | struct hlua_txn *htxn; |
| 7553 | const char *name; |
| 7554 | size_t len; |
| 7555 | struct sample smp; |
| 7556 | |
| 7557 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 7558 | |
| 7559 | /* It is useles to retrieve the stream, but this function |
| 7560 | * runs only in a stream context. |
| 7561 | */ |
| 7562 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7563 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7564 | |
| 7565 | /* Unset the variable. */ |
| 7566 | 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] | 7567 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 7568 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7569 | } |
| 7570 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7571 | __LJMP static int hlua_get_var(lua_State *L) |
| 7572 | { |
| 7573 | struct hlua_txn *htxn; |
| 7574 | const char *name; |
| 7575 | size_t len; |
| 7576 | struct sample smp; |
| 7577 | |
| 7578 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 7579 | |
| 7580 | /* It is useles to retrieve the stream, but this function |
| 7581 | * runs only in a stream context. |
| 7582 | */ |
| 7583 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7584 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7585 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7586 | 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] | 7587 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7588 | lua_pushnil(L); |
| 7589 | return 1; |
| 7590 | } |
| 7591 | |
| 7592 | return hlua_smp2lua(L, &smp); |
| 7593 | } |
| 7594 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7595 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7596 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7597 | struct hlua *hlua; |
| 7598 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7599 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 7600 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7601 | /* It is useles to retrieve the stream, but this function |
| 7602 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7603 | */ |
| 7604 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7605 | |
| 7606 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7607 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7608 | if (!hlua) |
| 7609 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7610 | |
| 7611 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 7612 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7613 | |
| 7614 | /* Get and store new value. */ |
| 7615 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 7616 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 7617 | |
| 7618 | return 0; |
| 7619 | } |
| 7620 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7621 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7622 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7623 | struct hlua *hlua; |
| 7624 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7625 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 7626 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7627 | /* It is useles to retrieve the stream, but this function |
| 7628 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7629 | */ |
| 7630 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7631 | |
| 7632 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7633 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7634 | if (!hlua) { |
| 7635 | lua_pushnil(L); |
| 7636 | return 1; |
| 7637 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7638 | |
| 7639 | /* Push configuration index in the stack. */ |
| 7640 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 7641 | |
| 7642 | return 1; |
| 7643 | } |
| 7644 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7645 | /* Create stack entry containing a class TXN. This function |
| 7646 | * return 0 if the stack does not contains free slots, |
| 7647 | * otherwise it returns 1. |
| 7648 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7649 | 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] | 7650 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7651 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7652 | |
| 7653 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7654 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7655 | return 0; |
| 7656 | |
| 7657 | /* NOTE: The allocation never fails. The failure |
| 7658 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7659 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7660 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7661 | /* Create the object: obj[0] = userdata. */ |
| 7662 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7663 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7664 | lua_rawseti(L, -2, 0); |
| 7665 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7666 | htxn->s = s; |
| 7667 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 7668 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7669 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7670 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7671 | /* Create the "f" field that contains a list of fetches. */ |
| 7672 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7673 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7674 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7675 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7676 | |
| 7677 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 7678 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7679 | 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] | 7680 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7681 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7682 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7683 | /* Create the "c" field that contains a list of converters. */ |
| 7684 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7685 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7686 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7687 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7688 | |
| 7689 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 7690 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 7691 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7692 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7693 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7694 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7695 | /* Create the "req" field that contains the request channel object. */ |
| 7696 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7697 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7698 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7699 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7700 | |
| 7701 | /* Create the "res" field that contains the response channel object. */ |
| 7702 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7703 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7704 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7705 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7706 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7707 | /* Creates the HTTP object is the current proxy allows http. */ |
| 7708 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 7709 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7710 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7711 | return 0; |
| 7712 | } |
| 7713 | else |
| 7714 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7715 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7716 | |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 7717 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) { |
| 7718 | /* HTTPMessage object are created when a lua TXN is created from |
| 7719 | * a filter context only |
| 7720 | */ |
| 7721 | |
| 7722 | /* Creates the HTTP-Request object is the current proxy allows http. */ |
| 7723 | lua_pushstring(L, "http_req"); |
| 7724 | if (p->mode == PR_MODE_HTTP) { |
| 7725 | if (!hlua_http_msg_new(L, &s->txn->req)) |
| 7726 | return 0; |
| 7727 | } |
| 7728 | else |
| 7729 | lua_pushnil(L); |
| 7730 | lua_rawset(L, -3); |
| 7731 | |
| 7732 | /* Creates the HTTP-Response object is the current proxy allows http. */ |
| 7733 | lua_pushstring(L, "http_res"); |
| 7734 | if (p->mode == PR_MODE_HTTP) { |
| 7735 | if (!hlua_http_msg_new(L, &s->txn->rsp)) |
| 7736 | return 0; |
| 7737 | } |
| 7738 | else |
| 7739 | lua_pushnil(L); |
| 7740 | lua_rawset(L, -3); |
| 7741 | } |
| 7742 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7743 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 7744 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 7745 | lua_setmetatable(L, -2); |
| 7746 | |
| 7747 | return 1; |
| 7748 | } |
| 7749 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7750 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 7751 | { |
| 7752 | const char *msg; |
| 7753 | struct hlua_txn *htxn; |
| 7754 | |
| 7755 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 7756 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7757 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7758 | |
| 7759 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 7760 | return 0; |
| 7761 | } |
| 7762 | |
| 7763 | __LJMP static int hlua_txn_log(lua_State *L) |
| 7764 | { |
| 7765 | int level; |
| 7766 | const char *msg; |
| 7767 | struct hlua_txn *htxn; |
| 7768 | |
| 7769 | MAY_LJMP(check_args(L, 3, "log")); |
| 7770 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7771 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7772 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 7773 | |
| 7774 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 7775 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 7776 | |
| 7777 | hlua_sendlog(htxn->s->be, level, msg); |
| 7778 | return 0; |
| 7779 | } |
| 7780 | |
| 7781 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 7782 | { |
| 7783 | const char *msg; |
| 7784 | struct hlua_txn *htxn; |
| 7785 | |
| 7786 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 7787 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7788 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7789 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 7790 | return 0; |
| 7791 | } |
| 7792 | |
| 7793 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 7794 | { |
| 7795 | const char *msg; |
| 7796 | struct hlua_txn *htxn; |
| 7797 | |
| 7798 | MAY_LJMP(check_args(L, 2, "Info")); |
| 7799 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7800 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7801 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 7802 | return 0; |
| 7803 | } |
| 7804 | |
| 7805 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 7806 | { |
| 7807 | const char *msg; |
| 7808 | struct hlua_txn *htxn; |
| 7809 | |
| 7810 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 7811 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7812 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7813 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 7814 | return 0; |
| 7815 | } |
| 7816 | |
| 7817 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 7818 | { |
| 7819 | const char *msg; |
| 7820 | struct hlua_txn *htxn; |
| 7821 | |
| 7822 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 7823 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7824 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7825 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 7826 | return 0; |
| 7827 | } |
| 7828 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7829 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 7830 | { |
| 7831 | struct hlua_txn *htxn; |
| 7832 | int ll; |
| 7833 | |
| 7834 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 7835 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7836 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7837 | |
| 7838 | if (ll < 0 || ll > 7) |
| 7839 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 7840 | |
| 7841 | htxn->s->logs.level = ll; |
| 7842 | return 0; |
| 7843 | } |
| 7844 | |
| 7845 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 7846 | { |
| 7847 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7848 | int tos; |
| 7849 | |
| 7850 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 7851 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7852 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7853 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 7854 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7855 | return 0; |
| 7856 | } |
| 7857 | |
| 7858 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 7859 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7860 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7861 | int mark; |
| 7862 | |
| 7863 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 7864 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7865 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7866 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 7867 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7868 | return 0; |
| 7869 | } |
| 7870 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 7871 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 7872 | { |
| 7873 | struct hlua_txn *htxn; |
| 7874 | |
| 7875 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 7876 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7877 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 7878 | return 0; |
| 7879 | } |
| 7880 | |
| 7881 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 7882 | { |
| 7883 | struct hlua_txn *htxn; |
| 7884 | |
| 7885 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 7886 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7887 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 7888 | return 0; |
| 7889 | } |
| 7890 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7891 | /* 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] | 7892 | * 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] | 7893 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 7894 | * error. The Reply must be on top of the stack. |
| 7895 | */ |
| 7896 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 7897 | { |
| 7898 | struct htx *htx; |
| 7899 | struct htx_sl *sl; |
| 7900 | struct h1m h1m; |
| 7901 | const char *status, *reason, *body; |
| 7902 | size_t status_len, reason_len, body_len; |
| 7903 | int ret, code, flags; |
| 7904 | |
| 7905 | code = 200; |
| 7906 | status = "200"; |
| 7907 | status_len = 3; |
| 7908 | ret = lua_getfield(L, -1, "status"); |
| 7909 | if (ret == LUA_TNUMBER) { |
| 7910 | code = lua_tointeger(L, -1); |
| 7911 | status = lua_tolstring(L, -1, &status_len); |
| 7912 | } |
| 7913 | lua_pop(L, 1); |
| 7914 | |
| 7915 | reason = http_get_reason(code); |
| 7916 | reason_len = strlen(reason); |
| 7917 | ret = lua_getfield(L, -1, "reason"); |
| 7918 | if (ret == LUA_TSTRING) |
| 7919 | reason = lua_tolstring(L, -1, &reason_len); |
| 7920 | lua_pop(L, 1); |
| 7921 | |
| 7922 | body = NULL; |
| 7923 | body_len = 0; |
| 7924 | ret = lua_getfield(L, -1, "body"); |
| 7925 | if (ret == LUA_TSTRING) |
| 7926 | body = lua_tolstring(L, -1, &body_len); |
| 7927 | lua_pop(L, 1); |
| 7928 | |
| 7929 | /* Prepare the response before inserting the headers */ |
| 7930 | h1m_init_res(&h1m); |
| 7931 | htx = htx_from_buf(&s->res.buf); |
| 7932 | channel_htx_truncate(&s->res, htx); |
| 7933 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 7934 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 7935 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 7936 | ist2(status, status_len), ist2(reason, reason_len)); |
| 7937 | } |
| 7938 | else { |
| 7939 | flags = HTX_SL_F_IS_RESP; |
| 7940 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 7941 | ist2(status, status_len), ist2(reason, reason_len)); |
| 7942 | } |
| 7943 | if (!sl) |
| 7944 | goto fail; |
| 7945 | sl->info.res.status = code; |
| 7946 | |
| 7947 | /* Push in the stack the "headers" entry. */ |
| 7948 | ret = lua_getfield(L, -1, "headers"); |
| 7949 | if (ret != LUA_TTABLE) |
| 7950 | goto skip_headers; |
| 7951 | |
| 7952 | lua_pushnil(L); |
| 7953 | while (lua_next(L, -2) != 0) { |
| 7954 | struct ist name, value; |
| 7955 | const char *n, *v; |
| 7956 | size_t nlen, vlen; |
| 7957 | |
| 7958 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 7959 | /* Skip element if the key is not a string or if the value is not a table */ |
| 7960 | goto next_hdr; |
| 7961 | } |
| 7962 | |
| 7963 | n = lua_tolstring(L, -2, &nlen); |
| 7964 | name = ist2(n, nlen); |
| 7965 | if (isteqi(name, ist("content-length"))) { |
| 7966 | /* Always skip content-length header. It will be added |
| 7967 | * later with the correct len |
| 7968 | */ |
| 7969 | goto next_hdr; |
| 7970 | } |
| 7971 | |
| 7972 | /* Loop on header's values */ |
| 7973 | lua_pushnil(L); |
| 7974 | while (lua_next(L, -2)) { |
| 7975 | if (!lua_isstring(L, -1)) { |
| 7976 | /* Skip the value if it is not a string */ |
| 7977 | goto next_value; |
| 7978 | } |
| 7979 | |
| 7980 | v = lua_tolstring(L, -1, &vlen); |
| 7981 | value = ist2(v, vlen); |
| 7982 | |
| 7983 | if (isteqi(name, ist("transfer-encoding"))) |
| 7984 | h1_parse_xfer_enc_header(&h1m, value); |
| 7985 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 7986 | goto fail; |
| 7987 | |
| 7988 | next_value: |
| 7989 | lua_pop(L, 1); |
| 7990 | } |
| 7991 | |
| 7992 | next_hdr: |
| 7993 | lua_pop(L, 1); |
| 7994 | } |
| 7995 | skip_headers: |
| 7996 | lua_pop(L, 1); |
| 7997 | |
| 7998 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 7999 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 8000 | const char *clen = ultoa(body_len); |
| 8001 | |
| 8002 | h1m.flags |= H1_MF_CLEN; |
| 8003 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 8004 | goto fail; |
| 8005 | } |
| 8006 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 8007 | h1m.flags |= H1_MF_XFER_LEN; |
| 8008 | |
| 8009 | /* Update HTX start-line flags */ |
| 8010 | if (h1m.flags & H1_MF_XFER_ENC) |
| 8011 | flags |= HTX_SL_F_XFER_ENC; |
| 8012 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 8013 | flags |= HTX_SL_F_XFER_LEN; |
| 8014 | if (h1m.flags & H1_MF_CHNK) |
| 8015 | flags |= HTX_SL_F_CHNK; |
| 8016 | else if (h1m.flags & H1_MF_CLEN) |
| 8017 | flags |= HTX_SL_F_CLEN; |
| 8018 | if (h1m.body_len == 0) |
| 8019 | flags |= HTX_SL_F_BODYLESS; |
| 8020 | } |
| 8021 | sl->flags |= flags; |
| 8022 | |
| 8023 | |
| 8024 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 8025 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8026 | goto fail; |
| 8027 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 8028 | htx->flags |= HTX_FL_EOM; |
| 8029 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8030 | /* Now, forward the response and terminate the transaction */ |
| 8031 | s->txn->status = code; |
| 8032 | htx_to_buf(htx, &s->res.buf); |
| 8033 | if (!http_forward_proxy_resp(s, 1)) |
| 8034 | goto fail; |
| 8035 | |
| 8036 | return 1; |
| 8037 | |
| 8038 | fail: |
| 8039 | channel_htx_truncate(&s->res, htx); |
| 8040 | return 0; |
| 8041 | } |
| 8042 | |
| 8043 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 8044 | * processing is just aborted. Nothing is returned to the client and all |
| 8045 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 8046 | * is forwarded to the client before terminating the transaction. On success, |
| 8047 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 8048 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 8049 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8050 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8051 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8052 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 8053 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8054 | struct stream *s; |
| 8055 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8056 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 8057 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8058 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8059 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 8060 | * just end the execution of the current lua code. */ |
| 8061 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 8062 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 8063 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8064 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8065 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8066 | struct channel *req = &s->req; |
| 8067 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 8068 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8069 | channel_auto_read(req); |
| 8070 | channel_abort(req); |
| 8071 | channel_auto_close(req); |
| 8072 | channel_erase(req); |
| 8073 | |
| 8074 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 8075 | channel_auto_read(res); |
| 8076 | channel_auto_close(res); |
| 8077 | channel_shutr_now(res); |
| 8078 | |
| 8079 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 8080 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8081 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 8082 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8083 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 8084 | /* No reply or invalid reply */ |
| 8085 | s->txn->status = 0; |
| 8086 | http_reply_and_close(s, 0, NULL); |
| 8087 | } |
| 8088 | else { |
| 8089 | /* Remove extra args to have the reply on top of the stack */ |
| 8090 | if (lua_gettop(L) > 2) |
| 8091 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8092 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8093 | if (!hlua_txn_forward_reply(L, s)) { |
| 8094 | if (!(s->flags & SF_ERR_MASK)) |
| 8095 | s->flags |= SF_ERR_PRXCOND; |
| 8096 | lua_pushinteger(L, ACT_RET_ERR); |
| 8097 | WILL_LJMP(hlua_done(L)); |
| 8098 | return 0; /* Never reached */ |
| 8099 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 8100 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8101 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8102 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 8103 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 8104 | /* let's log the request time */ |
| 8105 | s->logs.tv_request = now; |
| 8106 | 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] | 8107 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8108 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8109 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8110 | done: |
| 8111 | if (!(s->flags & SF_ERR_MASK)) |
| 8112 | s->flags |= SF_ERR_LOCAL; |
| 8113 | if (!(s->flags & SF_FINST_MASK)) |
| 8114 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8115 | |
Christopher Faulet | e48d1dc | 2021-08-13 14:11:17 +0200 | [diff] [blame] | 8116 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) |
| 8117 | lua_pushinteger(L, -1); |
| 8118 | else |
| 8119 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8120 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8121 | return 0; |
| 8122 | } |
| 8123 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8124 | /* |
| 8125 | * |
| 8126 | * |
| 8127 | * Class REPLY |
| 8128 | * |
| 8129 | * |
| 8130 | */ |
| 8131 | |
| 8132 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 8133 | * free slots, the function fails and returns 0; |
| 8134 | */ |
| 8135 | static int hlua_txn_reply_new(lua_State *L) |
| 8136 | { |
| 8137 | struct hlua_txn *htxn; |
| 8138 | const char *reason, *body = NULL; |
| 8139 | int ret, status; |
| 8140 | |
| 8141 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8142 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8143 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 8144 | WILL_LJMP(lua_error(L)); |
| 8145 | } |
| 8146 | |
| 8147 | /* Default value */ |
| 8148 | status = 200; |
| 8149 | reason = http_get_reason(status); |
| 8150 | |
| 8151 | if (lua_istable(L, 2)) { |
| 8152 | /* load status and reason from the table argument at index 2 */ |
| 8153 | ret = lua_getfield(L, 2, "status"); |
| 8154 | if (ret == LUA_TNIL) |
| 8155 | goto reason; |
| 8156 | else if (ret != LUA_TNUMBER) { |
| 8157 | /* invalid status: ignore the reason */ |
| 8158 | goto body; |
| 8159 | } |
| 8160 | status = lua_tointeger(L, -1); |
| 8161 | |
| 8162 | reason: |
| 8163 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 8164 | ret = lua_getfield(L, 2, "reason"); |
| 8165 | if (ret == LUA_TSTRING) |
| 8166 | reason = lua_tostring(L, -1); |
| 8167 | |
| 8168 | body: |
| 8169 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 8170 | ret = lua_getfield(L, 2, "body"); |
| 8171 | if (ret == LUA_TSTRING) |
| 8172 | body = lua_tostring(L, -1); |
| 8173 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 8174 | } |
| 8175 | |
| 8176 | /* Create the Reply table */ |
| 8177 | lua_newtable(L); |
| 8178 | |
| 8179 | /* Add status element */ |
| 8180 | lua_pushstring(L, "status"); |
| 8181 | lua_pushinteger(L, status); |
| 8182 | lua_settable(L, -3); |
| 8183 | |
| 8184 | /* Add reason element */ |
| 8185 | reason = http_get_reason(status); |
| 8186 | lua_pushstring(L, "reason"); |
| 8187 | lua_pushstring(L, reason); |
| 8188 | lua_settable(L, -3); |
| 8189 | |
| 8190 | /* Add body element, nil if undefined */ |
| 8191 | lua_pushstring(L, "body"); |
| 8192 | if (body) |
| 8193 | lua_pushstring(L, body); |
| 8194 | else |
| 8195 | lua_pushnil(L); |
| 8196 | lua_settable(L, -3); |
| 8197 | |
| 8198 | /* Add headers element */ |
| 8199 | lua_pushstring(L, "headers"); |
| 8200 | lua_newtable(L); |
| 8201 | |
| 8202 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8203 | if (lua_istable(L, 2)) { |
| 8204 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 8205 | ret = lua_getfield(L, 2, "headers"); |
| 8206 | if (ret == LUA_TTABLE) { |
| 8207 | /* stack: [ ... <headers:table>, <table> ] */ |
| 8208 | lua_pushnil(L); |
| 8209 | while (lua_next(L, -2) != 0) { |
| 8210 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 8211 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 8212 | /* invalid value type, skip it */ |
| 8213 | lua_pop(L, 1); |
| 8214 | continue; |
| 8215 | } |
| 8216 | |
| 8217 | |
| 8218 | /* Duplicate the key and swap it with the value. */ |
| 8219 | lua_pushvalue(L, -2); |
| 8220 | lua_insert(L, -2); |
| 8221 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 8222 | |
| 8223 | lua_newtable(L); |
| 8224 | lua_insert(L, -2); |
| 8225 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 8226 | |
| 8227 | if (lua_isstring(L, -1)) { |
| 8228 | /* push the value in the inner table */ |
| 8229 | lua_rawseti(L, -2, 1); |
| 8230 | } |
| 8231 | else { /* table */ |
| 8232 | lua_pushnil(L); |
| 8233 | while (lua_next(L, -2) != 0) { |
| 8234 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 8235 | if (!lua_isstring(L, -1)) { |
| 8236 | /* invalid value type, skip it*/ |
| 8237 | lua_pop(L, 1); |
| 8238 | continue; |
| 8239 | } |
| 8240 | /* push the value in the inner table */ |
| 8241 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 8242 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 8243 | } |
| 8244 | lua_pop(L, 1); |
| 8245 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 8246 | } |
| 8247 | |
| 8248 | /* push (k,v) on the stack in the headers table: |
| 8249 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 8250 | */ |
| 8251 | lua_settable(L, -5); |
| 8252 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 8253 | } |
| 8254 | } |
| 8255 | lua_pop(L, 1); |
| 8256 | } |
| 8257 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8258 | lua_settable(L, -3); |
| 8259 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 8260 | |
| 8261 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 8262 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 8263 | lua_setmetatable(L, -2); |
| 8264 | return 1; |
| 8265 | } |
| 8266 | |
| 8267 | /* Set the reply status code, and optionally the reason. If no reason is |
| 8268 | * provided, the default one corresponding to the status code is used. |
| 8269 | */ |
| 8270 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 8271 | { |
| 8272 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 8273 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 8274 | |
| 8275 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8276 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8277 | |
| 8278 | if (status < 100 || status > 599) { |
| 8279 | lua_pushboolean(L, 0); |
| 8280 | return 1; |
| 8281 | } |
| 8282 | if (!reason) |
| 8283 | reason = http_get_reason(status); |
| 8284 | |
| 8285 | lua_pushinteger(L, status); |
| 8286 | lua_setfield(L, 1, "status"); |
| 8287 | |
| 8288 | lua_pushstring(L, reason); |
| 8289 | lua_setfield(L, 1, "reason"); |
| 8290 | |
| 8291 | lua_pushboolean(L, 1); |
| 8292 | return 1; |
| 8293 | } |
| 8294 | |
| 8295 | /* Add a header into the reply object. Each header name is associated to an |
| 8296 | * array of values in the "headers" table. If the header name is not found, a |
| 8297 | * new entry is created. |
| 8298 | */ |
| 8299 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 8300 | { |
| 8301 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8302 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 8303 | int ret; |
| 8304 | |
| 8305 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8306 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8307 | |
| 8308 | /* Push in the stack the "headers" entry. */ |
| 8309 | ret = lua_getfield(L, 1, "headers"); |
| 8310 | if (ret != LUA_TTABLE) { |
| 8311 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 8312 | WILL_LJMP(lua_error(L)); |
| 8313 | } |
| 8314 | |
| 8315 | /* check if the header is already registered. If not, register it. */ |
| 8316 | ret = lua_getfield(L, -1, name); |
| 8317 | if (ret == LUA_TNIL) { |
| 8318 | /* Entry not found. */ |
| 8319 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 8320 | |
| 8321 | /* Insert the new header name in the array in the top of the stack. |
| 8322 | * It left the new array in the top of the stack. |
| 8323 | */ |
| 8324 | lua_newtable(L); |
| 8325 | lua_pushstring(L, name); |
| 8326 | lua_pushvalue(L, -2); |
| 8327 | lua_settable(L, -4); |
| 8328 | } |
| 8329 | else if (ret != LUA_TTABLE) { |
| 8330 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 8331 | WILL_LJMP(lua_error(L)); |
| 8332 | } |
| 8333 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8334 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8335 | * the header value as new entry. |
| 8336 | */ |
| 8337 | lua_pushstring(L, value); |
| 8338 | ret = lua_rawlen(L, -2); |
| 8339 | lua_rawseti(L, -2, ret + 1); |
| 8340 | |
| 8341 | lua_pushboolean(L, 1); |
| 8342 | return 1; |
| 8343 | } |
| 8344 | |
| 8345 | /* Remove all occurrences of a given header name. */ |
| 8346 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 8347 | { |
| 8348 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8349 | int ret; |
| 8350 | |
| 8351 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8352 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8353 | |
| 8354 | /* Push in the stack the "headers" entry. */ |
| 8355 | ret = lua_getfield(L, 1, "headers"); |
| 8356 | if (ret != LUA_TTABLE) { |
| 8357 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 8358 | WILL_LJMP(lua_error(L)); |
| 8359 | } |
| 8360 | |
| 8361 | lua_pushstring(L, name); |
| 8362 | lua_pushnil(L); |
| 8363 | lua_settable(L, -3); |
| 8364 | |
| 8365 | lua_pushboolean(L, 1); |
| 8366 | return 1; |
| 8367 | } |
| 8368 | |
| 8369 | /* Set the reply's body. Overwrite any existing entry. */ |
| 8370 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 8371 | { |
| 8372 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8373 | |
| 8374 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8375 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8376 | |
| 8377 | lua_pushstring(L, payload); |
| 8378 | lua_setfield(L, 1, "body"); |
| 8379 | |
| 8380 | lua_pushboolean(L, 1); |
| 8381 | return 1; |
| 8382 | } |
| 8383 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8384 | __LJMP static int hlua_log(lua_State *L) |
| 8385 | { |
| 8386 | int level; |
| 8387 | const char *msg; |
| 8388 | |
| 8389 | MAY_LJMP(check_args(L, 2, "log")); |
| 8390 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 8391 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8392 | |
| 8393 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 8394 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 8395 | |
| 8396 | hlua_sendlog(NULL, level, msg); |
| 8397 | return 0; |
| 8398 | } |
| 8399 | |
| 8400 | __LJMP static int hlua_log_debug(lua_State *L) |
| 8401 | { |
| 8402 | const char *msg; |
| 8403 | |
| 8404 | MAY_LJMP(check_args(L, 1, "debug")); |
| 8405 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8406 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 8407 | return 0; |
| 8408 | } |
| 8409 | |
| 8410 | __LJMP static int hlua_log_info(lua_State *L) |
| 8411 | { |
| 8412 | const char *msg; |
| 8413 | |
| 8414 | MAY_LJMP(check_args(L, 1, "info")); |
| 8415 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8416 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 8417 | return 0; |
| 8418 | } |
| 8419 | |
| 8420 | __LJMP static int hlua_log_warning(lua_State *L) |
| 8421 | { |
| 8422 | const char *msg; |
| 8423 | |
| 8424 | MAY_LJMP(check_args(L, 1, "warning")); |
| 8425 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8426 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 8427 | return 0; |
| 8428 | } |
| 8429 | |
| 8430 | __LJMP static int hlua_log_alert(lua_State *L) |
| 8431 | { |
| 8432 | const char *msg; |
| 8433 | |
| 8434 | MAY_LJMP(check_args(L, 1, "alert")); |
| 8435 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8436 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8437 | return 0; |
| 8438 | } |
| 8439 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8440 | __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] | 8441 | { |
| 8442 | int wakeup_ms = lua_tointeger(L, -1); |
Willy Tarreau | 12c0270 | 2021-09-30 16:12:31 +0200 | [diff] [blame] | 8443 | if (!tick_is_expired(wakeup_ms, now_ms)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8444 | 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] | 8445 | return 0; |
| 8446 | } |
| 8447 | |
| 8448 | __LJMP static int hlua_sleep(lua_State *L) |
| 8449 | { |
| 8450 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8451 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8452 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8453 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8454 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8455 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8456 | wakeup_ms = tick_add(now_ms, delay); |
| 8457 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8458 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8459 | 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] | 8460 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8461 | } |
| 8462 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8463 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8464 | { |
| 8465 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8466 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8467 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8468 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8469 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8470 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8471 | wakeup_ms = tick_add(now_ms, delay); |
| 8472 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8473 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8474 | 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] | 8475 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8476 | } |
| 8477 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8478 | /* This functionis an LUA binding. it permits to give back |
| 8479 | * the hand at the HAProxy scheduler. It is used when the |
| 8480 | * LUA processing consumes a lot of time. |
| 8481 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8482 | __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] | 8483 | { |
| 8484 | return 0; |
| 8485 | } |
| 8486 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8487 | __LJMP static int hlua_yield(lua_State *L) |
| 8488 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8489 | 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] | 8490 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8491 | } |
| 8492 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8493 | /* This function change the nice of the currently executed |
| 8494 | * task. It is used set low or high priority at the current |
| 8495 | * task. |
| 8496 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8497 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8498 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8499 | struct hlua *hlua; |
| 8500 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8501 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8502 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8503 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8504 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8505 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 8506 | hlua = hlua_gethlua(L); |
| 8507 | |
| 8508 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8509 | if (!hlua || !hlua->task) |
| 8510 | return 0; |
| 8511 | |
| 8512 | if (nice < -1024) |
| 8513 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8514 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8515 | nice = 1024; |
| 8516 | |
| 8517 | hlua->task->nice = nice; |
| 8518 | return 0; |
| 8519 | } |
| 8520 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8521 | /* 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] | 8522 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 8523 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 8524 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8525 | * |
| 8526 | * Task wrapper are longjmp safe because the only one Lua code |
| 8527 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8528 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 8529 | 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] | 8530 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 8531 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8532 | enum hlua_exec status; |
| 8533 | |
Willy Tarreau | 6ef52f4 | 2022-06-15 14:19:48 +0200 | [diff] [blame] | 8534 | if (task->tid < 0) |
| 8535 | task->tid = tid; |
| 8536 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8537 | /* If it is the first call to the task, we must initialize the |
| 8538 | * execution timeouts. |
| 8539 | */ |
| 8540 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 8541 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8542 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8543 | /* Execute the Lua code. */ |
| 8544 | status = hlua_ctx_resume(hlua, 1); |
| 8545 | |
| 8546 | switch (status) { |
| 8547 | /* finished or yield */ |
| 8548 | case HLUA_E_OK: |
| 8549 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8550 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 8551 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8552 | break; |
| 8553 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 8554 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 8555 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 8556 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8557 | break; |
| 8558 | |
| 8559 | /* finished with error. */ |
| 8560 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8561 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8562 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8563 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8564 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8565 | break; |
| 8566 | |
| 8567 | case HLUA_E_ERR: |
| 8568 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8569 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8570 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8571 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8572 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8573 | break; |
| 8574 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8575 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8576 | } |
| 8577 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8578 | /* This function is an LUA binding that register LUA function to be |
| 8579 | * executed after the HAProxy configuration parsing and before the |
| 8580 | * HAProxy scheduler starts. This function expect only one LUA |
| 8581 | * argument that is a function. This function returns nothing, but |
| 8582 | * throws if an error is encountered. |
| 8583 | */ |
| 8584 | __LJMP static int hlua_register_init(lua_State *L) |
| 8585 | { |
| 8586 | struct hlua_init_function *init; |
| 8587 | int ref; |
| 8588 | |
| 8589 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 8590 | |
| 8591 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8592 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8593 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8594 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8595 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8596 | |
| 8597 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 8598 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8599 | return 0; |
| 8600 | } |
| 8601 | |
Ilya Shipitsin | 6f86eaa | 2022-11-30 16:22:42 +0500 | [diff] [blame] | 8602 | /* This function is an LUA binding. It permits to register a task |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8603 | * executed in parallel of the main HAroxy activity. The task is |
| 8604 | * created and it is set in the HAProxy scheduler. It can be called |
| 8605 | * from the "init" section, "post init" or during the runtime. |
| 8606 | * |
| 8607 | * Lua prototype: |
| 8608 | * |
| 8609 | * <none> core.register_task(<function>) |
| 8610 | */ |
| 8611 | static int hlua_register_task(lua_State *L) |
| 8612 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8613 | struct hlua *hlua = NULL; |
| 8614 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8615 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8616 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8617 | |
| 8618 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 8619 | |
| 8620 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8621 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8622 | /* Get the reference state. If the reference is NULL, L is the master |
| 8623 | * state, otherwise hlua->T is. |
| 8624 | */ |
| 8625 | hlua = hlua_gethlua(L); |
| 8626 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8627 | /* we are in runtime processing */ |
| 8628 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8629 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8630 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8631 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8632 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8633 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8634 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8635 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8636 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8637 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8638 | /* We are in the common lua state, execute the task anywhere, |
| 8639 | * otherwise, inherit the current thread identifier |
| 8640 | */ |
| 8641 | if (state_id == 0) |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8642 | task = task_new_anywhere(); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8643 | else |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8644 | task = task_new_here(); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8645 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8646 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8647 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8648 | task->context = hlua; |
| 8649 | task->process = hlua_process_task; |
| 8650 | |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8651 | if (!hlua_ctx_init(hlua, state_id, task, 1)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8652 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8653 | |
| 8654 | /* Restore the function in the stack. */ |
| 8655 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 8656 | hlua->nargs = 0; |
| 8657 | |
| 8658 | /* Schedule task. */ |
Willy Tarreau | e3957f8 | 2021-09-30 16:17:37 +0200 | [diff] [blame] | 8659 | task_wakeup(task, TASK_WOKEN_INIT); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8660 | |
| 8661 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8662 | |
| 8663 | alloc_error: |
| 8664 | task_destroy(task); |
| 8665 | hlua_ctx_destroy(hlua); |
| 8666 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8667 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8668 | } |
| 8669 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8670 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 8671 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 8672 | * resume converters. |
| 8673 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8674 | 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] | 8675 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8676 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8677 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8678 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8679 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8680 | if (!stream) |
| 8681 | return 0; |
| 8682 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8683 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8684 | * Lua context can be not initialized. This behavior |
| 8685 | * permits to save performances because a systematic |
| 8686 | * Lua initialization cause 5% performances loss. |
| 8687 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8688 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8689 | struct hlua *hlua; |
| 8690 | |
| 8691 | hlua = pool_alloc(pool_head_hlua); |
| 8692 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8693 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8694 | return 0; |
| 8695 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8696 | HLUA_INIT(hlua); |
| 8697 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8698 | 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] | 8699 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8700 | return 0; |
| 8701 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8702 | } |
| 8703 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8704 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8705 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8706 | |
| 8707 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8708 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8709 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8710 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8711 | else |
| 8712 | error = "critical error"; |
| 8713 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8714 | return 0; |
| 8715 | } |
| 8716 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8717 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8718 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8719 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8720 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8721 | return 0; |
| 8722 | } |
| 8723 | |
| 8724 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8725 | 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] | 8726 | |
| 8727 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8728 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8729 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8730 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8731 | return 0; |
| 8732 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8733 | hlua_smp2lua(stream->hlua->T, smp); |
| 8734 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8735 | |
| 8736 | /* push keywords in the stack. */ |
| 8737 | if (arg_p) { |
| 8738 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8739 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8740 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8741 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8742 | return 0; |
| 8743 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8744 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8745 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8746 | } |
| 8747 | } |
| 8748 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8749 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8750 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8751 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8752 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8753 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8754 | } |
| 8755 | |
| 8756 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8757 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8758 | /* finished. */ |
| 8759 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8760 | /* If the stack is empty, the function fails. */ |
| 8761 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8762 | return 0; |
| 8763 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8764 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8765 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8766 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8767 | return 1; |
| 8768 | |
| 8769 | /* yield. */ |
| 8770 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8771 | 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] | 8772 | return 0; |
| 8773 | |
| 8774 | /* finished with error. */ |
| 8775 | case HLUA_E_ERRMSG: |
| 8776 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8777 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8778 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8779 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8780 | return 0; |
| 8781 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8782 | case HLUA_E_ETMOUT: |
| 8783 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 8784 | return 0; |
| 8785 | |
| 8786 | case HLUA_E_NOMEM: |
| 8787 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 8788 | return 0; |
| 8789 | |
| 8790 | case HLUA_E_YIELD: |
| 8791 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 8792 | return 0; |
| 8793 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8794 | case HLUA_E_ERR: |
| 8795 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8796 | SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name); |
Willy Tarreau | 14de395 | 2022-11-14 07:08:28 +0100 | [diff] [blame] | 8797 | __fallthrough; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8798 | |
| 8799 | default: |
| 8800 | return 0; |
| 8801 | } |
| 8802 | } |
| 8803 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8804 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 8805 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8806 | * resume sample-fetches. This function will be called by the sample |
| 8807 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8808 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8809 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 8810 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8811 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8812 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8813 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8814 | const char *error; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 8815 | unsigned int hflags = HLUA_TXN_NOTERM | HLUA_TXN_SMP_CTX; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8816 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8817 | if (!stream) |
| 8818 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8819 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8820 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8821 | * Lua context can be not initialized. This behavior |
| 8822 | * permits to save performances because a systematic |
| 8823 | * Lua initialization cause 5% performances loss. |
| 8824 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8825 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8826 | struct hlua *hlua; |
| 8827 | |
| 8828 | hlua = pool_alloc(pool_head_hlua); |
| 8829 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8830 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8831 | return 0; |
| 8832 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8833 | hlua->T = NULL; |
| 8834 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8835 | 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] | 8836 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8837 | return 0; |
| 8838 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8839 | } |
| 8840 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8841 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8842 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8843 | |
| 8844 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8845 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8846 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8847 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8848 | else |
| 8849 | error = "critical error"; |
| 8850 | 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] | 8851 | return 0; |
| 8852 | } |
| 8853 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8854 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8855 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8856 | 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] | 8857 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8858 | return 0; |
| 8859 | } |
| 8860 | |
| 8861 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8862 | 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] | 8863 | |
| 8864 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 8865 | 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] | 8866 | 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] | 8867 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8868 | return 0; |
| 8869 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8870 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8871 | |
| 8872 | /* push keywords in the stack. */ |
| 8873 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 8874 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8875 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8876 | 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] | 8877 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8878 | return 0; |
| 8879 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8880 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8881 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8882 | } |
| 8883 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8884 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8885 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8886 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8887 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8888 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8889 | } |
| 8890 | |
| 8891 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8892 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8893 | /* finished. */ |
| 8894 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8895 | /* If the stack is empty, the function fails. */ |
| 8896 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8897 | return 0; |
| 8898 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8899 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8900 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8901 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8902 | |
| 8903 | /* Set the end of execution flag. */ |
| 8904 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 8905 | return 1; |
| 8906 | |
| 8907 | /* yield. */ |
| 8908 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8909 | 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] | 8910 | return 0; |
| 8911 | |
| 8912 | /* finished with error. */ |
| 8913 | case HLUA_E_ERRMSG: |
| 8914 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8915 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8916 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8917 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8918 | return 0; |
| 8919 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8920 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8921 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 8922 | return 0; |
| 8923 | |
| 8924 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8925 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 8926 | return 0; |
| 8927 | |
| 8928 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8929 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 8930 | return 0; |
| 8931 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8932 | case HLUA_E_ERR: |
| 8933 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8934 | SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name); |
Willy Tarreau | 14de395 | 2022-11-14 07:08:28 +0100 | [diff] [blame] | 8935 | __fallthrough; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8936 | |
| 8937 | default: |
| 8938 | return 0; |
| 8939 | } |
| 8940 | } |
| 8941 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8942 | /* This function is an LUA binding used for registering |
| 8943 | * "sample-conv" functions. It expects a converter name used |
| 8944 | * in the haproxy configuration file, and an LUA function. |
| 8945 | */ |
| 8946 | __LJMP static int hlua_register_converters(lua_State *L) |
| 8947 | { |
| 8948 | struct sample_conv_kw_list *sck; |
| 8949 | const char *name; |
| 8950 | int ref; |
| 8951 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8952 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8953 | struct sample_conv *sc; |
| 8954 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8955 | |
| 8956 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 8957 | |
| 8958 | /* First argument : converter name. */ |
| 8959 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8960 | |
| 8961 | /* Second argument : lua function. */ |
| 8962 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 8963 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8964 | /* Check if the converter is already registered */ |
| 8965 | trash = get_trash_chunk(); |
| 8966 | chunk_printf(trash, "lua.%s", name); |
| 8967 | sc = find_sample_conv(trash->area, trash->data); |
| 8968 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8969 | fcn = sc->private; |
| 8970 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8971 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 8972 | "This will become a hard error in version 2.5.\n", name); |
| 8973 | } |
| 8974 | fcn->function_ref[hlua_state_id] = ref; |
| 8975 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8976 | } |
| 8977 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8978 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8979 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8980 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8981 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8982 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8983 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8984 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8985 | |
| 8986 | /* Fill fcn. */ |
| 8987 | fcn->name = strdup(name); |
| 8988 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8989 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8990 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8991 | |
| 8992 | /* List head */ |
| 8993 | sck->list.n = sck->list.p = NULL; |
| 8994 | |
| 8995 | /* converter keyword. */ |
| 8996 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8997 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8998 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8999 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9000 | |
| 9001 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 9002 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 9003 | 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] | 9004 | sck->kw[0].val_args = NULL; |
| 9005 | sck->kw[0].in_type = SMP_T_STR; |
| 9006 | sck->kw[0].out_type = SMP_T_STR; |
| 9007 | sck->kw[0].private = fcn; |
| 9008 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9009 | /* Register this new converter */ |
| 9010 | sample_register_convs(sck); |
| 9011 | |
| 9012 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9013 | |
| 9014 | alloc_error: |
| 9015 | release_hlua_function(fcn); |
| 9016 | ha_free(&sck); |
| 9017 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9018 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9019 | } |
| 9020 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9021 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9022 | * "sample-fetch" functions. It expects a converter name used |
| 9023 | * in the haproxy configuration file, and an LUA function. |
| 9024 | */ |
| 9025 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 9026 | { |
| 9027 | const char *name; |
| 9028 | int ref; |
| 9029 | int len; |
| 9030 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9031 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9032 | struct sample_fetch *sf; |
| 9033 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9034 | |
| 9035 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 9036 | |
| 9037 | /* First argument : sample-fetch name. */ |
| 9038 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9039 | |
| 9040 | /* Second argument : lua function. */ |
| 9041 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 9042 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9043 | /* Check if the sample-fetch is already registered */ |
| 9044 | trash = get_trash_chunk(); |
| 9045 | chunk_printf(trash, "lua.%s", name); |
| 9046 | sf = find_sample_fetch(trash->area, trash->data); |
| 9047 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9048 | fcn = sf->private; |
| 9049 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9050 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 9051 | "This will become a hard error in version 2.5.\n", name); |
| 9052 | } |
| 9053 | fcn->function_ref[hlua_state_id] = ref; |
| 9054 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9055 | } |
| 9056 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9057 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9058 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9059 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9060 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9061 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9062 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9063 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9064 | |
| 9065 | /* Fill fcn. */ |
| 9066 | fcn->name = strdup(name); |
| 9067 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9068 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9069 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9070 | |
| 9071 | /* List head */ |
| 9072 | sfk->list.n = sfk->list.p = NULL; |
| 9073 | |
| 9074 | /* sample-fetch keyword. */ |
| 9075 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9076 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9077 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9078 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9079 | |
| 9080 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 9081 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 9082 | 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] | 9083 | sfk->kw[0].val_args = NULL; |
| 9084 | sfk->kw[0].out_type = SMP_T_STR; |
| 9085 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 9086 | sfk->kw[0].val = 0; |
| 9087 | sfk->kw[0].private = fcn; |
| 9088 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9089 | /* Register this new fetch. */ |
| 9090 | sample_register_fetches(sfk); |
| 9091 | |
| 9092 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9093 | |
| 9094 | alloc_error: |
| 9095 | release_hlua_function(fcn); |
| 9096 | ha_free(&sfk); |
| 9097 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9098 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9099 | } |
| 9100 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 9101 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9102 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 9103 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9104 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9105 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9106 | unsigned int delay; |
| 9107 | unsigned int wakeup_ms; |
| 9108 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9109 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 9110 | hlua = hlua_gethlua(L); |
| 9111 | if (!hlua) { |
| 9112 | return 0; |
| 9113 | } |
| 9114 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9115 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 9116 | |
| 9117 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 9118 | wakeup_ms = tick_add(now_ms, delay); |
| 9119 | hlua->wake_time = wakeup_ms; |
| 9120 | return 0; |
| 9121 | } |
| 9122 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9123 | /* This function is a wrapper to execute each LUA function declared as an action |
| 9124 | * wrapper during the initialisation period. This function may return any |
| 9125 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 9126 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 9127 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9128 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9129 | 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] | 9130 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9131 | { |
| 9132 | char **arg; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 9133 | unsigned int hflags = HLUA_TXN_ACT_CTX; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9134 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9135 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9136 | |
| 9137 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 9138 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 9139 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 9140 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 9141 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9142 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9143 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9144 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9145 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9146 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9147 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9148 | * Lua context can be not initialized. This behavior |
| 9149 | * permits to save performances because a systematic |
| 9150 | * Lua initialization cause 5% performances loss. |
| 9151 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9152 | if (!s->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9153 | struct hlua *hlua; |
| 9154 | |
| 9155 | hlua = pool_alloc(pool_head_hlua); |
| 9156 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9157 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9158 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9159 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9160 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9161 | HLUA_INIT(hlua); |
| 9162 | s->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9163 | 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] | 9164 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9165 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9166 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9167 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9168 | } |
| 9169 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9170 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9171 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9172 | |
| 9173 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9174 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9175 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 9176 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9177 | else |
| 9178 | error = "critical error"; |
| 9179 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9180 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9181 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9182 | } |
| 9183 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9184 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9185 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9186 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9187 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9188 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9189 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9190 | } |
| 9191 | |
| 9192 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9193 | 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] | 9194 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9195 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 9196 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9197 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9198 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9199 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9200 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9201 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9202 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9203 | |
| 9204 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9205 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9206 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9207 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9208 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9209 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9210 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9211 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9212 | lua_pushstring(s->hlua->T, *arg); |
| 9213 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9214 | } |
| 9215 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9216 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9217 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9218 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9219 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9220 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9221 | } |
| 9222 | |
| 9223 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 9224 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9225 | /* finished. */ |
| 9226 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9227 | /* Catch the return value */ |
| 9228 | if (lua_gettop(s->hlua->T) > 0) |
| 9229 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9230 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9231 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 9232 | if (act_ret == ACT_RET_YIELD) { |
| 9233 | if (flags & ACT_OPT_FINAL) |
| 9234 | goto err_yield; |
| 9235 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9236 | if (dir == SMP_OPT_DIR_REQ) |
| 9237 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 9238 | s->hlua->wake_time); |
| 9239 | else |
| 9240 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 9241 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9242 | } |
| 9243 | goto end; |
| 9244 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9245 | /* yield. */ |
| 9246 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 9247 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9248 | if (dir == SMP_OPT_DIR_REQ) |
| 9249 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 9250 | s->hlua->wake_time); |
| 9251 | else |
| 9252 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 9253 | s->hlua->wake_time); |
| 9254 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9255 | /* Some actions can be wake up when a "write" event |
| 9256 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9257 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9258 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 9259 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9260 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9261 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9262 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9263 | act_ret = ACT_RET_YIELD; |
| 9264 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9265 | |
| 9266 | /* finished with error. */ |
| 9267 | case HLUA_E_ERRMSG: |
| 9268 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9269 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9270 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9271 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9272 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9273 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9274 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9275 | 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] | 9276 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9277 | |
| 9278 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9279 | 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] | 9280 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9281 | |
| 9282 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 9283 | err_yield: |
| 9284 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9285 | 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] | 9286 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9287 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9288 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9289 | case HLUA_E_ERR: |
| 9290 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9291 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9292 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9293 | |
| 9294 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9295 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9296 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9297 | |
| 9298 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 9299 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9300 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9301 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9302 | } |
| 9303 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 9304 | 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] | 9305 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 9306 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9307 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9308 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 9309 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 9310 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9311 | } |
| 9312 | |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9313 | static int hlua_applet_tcp_init(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9314 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9315 | 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] | 9316 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9317 | struct stream *strm = __sc_strm(sc); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9318 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9319 | struct task *task; |
| 9320 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9321 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9322 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9323 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9324 | if (!hlua) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9325 | SEND_ERR(strm->be, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9326 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9327 | return -1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9328 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9329 | HLUA_INIT(hlua); |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9330 | tcp_ctx->hlua = hlua; |
| 9331 | tcp_ctx->flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9332 | |
| 9333 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 9334 | task = task_new_here(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9335 | if (!task) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9336 | SEND_ERR(strm->be, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9337 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9338 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9339 | } |
| 9340 | task->nice = 0; |
| 9341 | task->context = ctx; |
| 9342 | task->process = hlua_applet_wakeup; |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9343 | tcp_ctx->task = task; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9344 | |
| 9345 | /* In the execution wrappers linked with a stream, the |
| 9346 | * Lua context can be not initialized. This behavior |
| 9347 | * permits to save performances because a systematic |
| 9348 | * Lua initialization cause 5% performances loss. |
| 9349 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9350 | 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] | 9351 | 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] | 9352 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9353 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9354 | } |
| 9355 | |
| 9356 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 9357 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9358 | |
| 9359 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9360 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9361 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9362 | error = lua_tostring(hlua->T, -1); |
| 9363 | else |
| 9364 | error = "critical error"; |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9365 | SEND_ERR(strm->be, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9366 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9367 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9368 | } |
| 9369 | |
| 9370 | /* Check stack available size. */ |
| 9371 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9372 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9373 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9374 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9375 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9376 | } |
| 9377 | |
| 9378 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9379 | 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] | 9380 | |
| 9381 | /* Create and and push object stream in the stack. */ |
| 9382 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9383 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9384 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9385 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9386 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9387 | } |
| 9388 | hlua->nargs = 1; |
| 9389 | |
| 9390 | /* push keywords in the stack. */ |
| 9391 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 9392 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9393 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9394 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9395 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9396 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9397 | } |
| 9398 | lua_pushstring(hlua->T, *arg); |
| 9399 | hlua->nargs++; |
| 9400 | } |
| 9401 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9402 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9403 | |
| 9404 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 9405 | applet_need_more_data(ctx); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 9406 | applet_have_more_data(ctx); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9407 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9408 | return 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9409 | } |
| 9410 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 9411 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9412 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9413 | struct hlua_tcp_ctx *tcp_ctx = ctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 9414 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9415 | struct stream *strm = __sc_strm(sc); |
| 9416 | struct channel *res = sc_ic(sc); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9417 | struct act_rule *rule = ctx->rule; |
| 9418 | struct proxy *px = strm->be; |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9419 | struct hlua *hlua = tcp_ctx->hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9420 | |
| 9421 | /* The applet execution is already done. */ |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9422 | if (tcp_ctx->flags & APPLET_DONE) { |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 9423 | /* eat the whole request */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9424 | co_skip(sc_oc(sc), co_data(sc_oc(sc))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9425 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 9426 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9427 | |
| 9428 | /* If the stream is disconnect or closed, ldo nothing. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9429 | if (unlikely(sc->state == SC_ST_DIS || sc->state == SC_ST_CLO)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9430 | return; |
| 9431 | |
| 9432 | /* Execute the function. */ |
| 9433 | switch (hlua_ctx_resume(hlua, 1)) { |
| 9434 | /* finished. */ |
| 9435 | case HLUA_E_OK: |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9436 | tcp_ctx->flags |= APPLET_DONE; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9437 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9438 | /* eat the whole request */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9439 | co_skip(sc_oc(sc), co_data(sc_oc(sc))); |
Christopher Faulet | 6e1bbc4 | 2022-12-12 08:08:15 +0100 | [diff] [blame] | 9440 | res->flags |= CF_READ_EVENT; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9441 | sc_shutr(sc); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9442 | return; |
| 9443 | |
| 9444 | /* yield. */ |
| 9445 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 9446 | if (hlua->wake_time != TICK_ETERNITY) |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9447 | task_schedule(tcp_ctx->task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9448 | return; |
| 9449 | |
| 9450 | /* finished with error. */ |
| 9451 | case HLUA_E_ERRMSG: |
| 9452 | /* Display log. */ |
| 9453 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9454 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9455 | lua_pop(hlua->T, 1); |
| 9456 | goto error; |
| 9457 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9458 | case HLUA_E_ETMOUT: |
| 9459 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9460 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9461 | goto error; |
| 9462 | |
| 9463 | case HLUA_E_NOMEM: |
| 9464 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9465 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9466 | goto error; |
| 9467 | |
| 9468 | case HLUA_E_YIELD: /* unexpected */ |
| 9469 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9470 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9471 | goto error; |
| 9472 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9473 | case HLUA_E_ERR: |
| 9474 | /* Display log. */ |
| 9475 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9476 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9477 | goto error; |
| 9478 | |
| 9479 | default: |
| 9480 | goto error; |
| 9481 | } |
| 9482 | |
| 9483 | error: |
| 9484 | |
| 9485 | /* For all other cases, just close the stream. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9486 | sc_shutw(sc); |
| 9487 | sc_shutr(sc); |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9488 | tcp_ctx->flags |= APPLET_DONE; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9489 | } |
| 9490 | |
| 9491 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 9492 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9493 | struct hlua_tcp_ctx *tcp_ctx = ctx->svcctx; |
| 9494 | |
| 9495 | task_destroy(tcp_ctx->task); |
| 9496 | tcp_ctx->task = NULL; |
| 9497 | hlua_ctx_destroy(tcp_ctx->hlua); |
| 9498 | tcp_ctx->hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9499 | } |
| 9500 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9501 | /* The function returns 0 if the initialisation is complete or -1 if |
| 9502 | * 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] | 9503 | */ |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9504 | static int hlua_applet_http_init(struct appctx *ctx) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9505 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9506 | 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] | 9507 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9508 | struct stream *strm = __sc_strm(sc); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9509 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9510 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9511 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9512 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9513 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9514 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9515 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9516 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9517 | if (!hlua) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9518 | SEND_ERR(strm->be, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9519 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9520 | return -1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9521 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9522 | HLUA_INIT(hlua); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9523 | http_ctx->hlua = hlua; |
| 9524 | http_ctx->left_bytes = -1; |
| 9525 | http_ctx->flags = 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9526 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 9527 | if (txn->req.flags & HTTP_MSGF_VER_11) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9528 | http_ctx->flags |= APPLET_HTTP11; |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 9529 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9530 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 9531 | task = task_new_here(); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9532 | if (!task) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9533 | SEND_ERR(strm->be, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9534 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9535 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9536 | } |
| 9537 | task->nice = 0; |
| 9538 | task->context = ctx; |
| 9539 | task->process = hlua_applet_wakeup; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9540 | http_ctx->task = task; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9541 | |
| 9542 | /* In the execution wrappers linked with a stream, the |
| 9543 | * Lua context can be not initialized. This behavior |
| 9544 | * permits to save performances because a systematic |
| 9545 | * Lua initialization cause 5% performances loss. |
| 9546 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9547 | 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] | 9548 | 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] | 9549 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9550 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9551 | } |
| 9552 | |
| 9553 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 9554 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9555 | |
| 9556 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9557 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9558 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9559 | error = lua_tostring(hlua->T, -1); |
| 9560 | else |
| 9561 | error = "critical error"; |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9562 | SEND_ERR(strm->be, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9563 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9564 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9565 | } |
| 9566 | |
| 9567 | /* Check stack available size. */ |
| 9568 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9569 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9570 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9571 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9572 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9573 | } |
| 9574 | |
| 9575 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9576 | 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] | 9577 | |
| 9578 | /* Create and and push object stream in the stack. */ |
| 9579 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9580 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9581 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9582 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9583 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9584 | } |
| 9585 | hlua->nargs = 1; |
| 9586 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9587 | /* push keywords in the stack. */ |
| 9588 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 9589 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9590 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9591 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9592 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9593 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9594 | } |
| 9595 | lua_pushstring(hlua->T, *arg); |
| 9596 | hlua->nargs++; |
| 9597 | } |
| 9598 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9599 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9600 | |
| 9601 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 9602 | applet_need_more_data(ctx); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9603 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9604 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9605 | } |
| 9606 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 9607 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9608 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9609 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 9610 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9611 | struct stream *strm = __sc_strm(sc); |
| 9612 | struct channel *req = sc_oc(sc); |
| 9613 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9614 | struct act_rule *rule = ctx->rule; |
| 9615 | struct proxy *px = strm->be; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9616 | struct hlua *hlua = http_ctx->hlua; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9617 | struct htx *req_htx, *res_htx; |
| 9618 | |
| 9619 | res_htx = htx_from_buf(&res->buf); |
| 9620 | |
| 9621 | /* If the stream is disconnect or closed, ldo nothing. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9622 | if (unlikely(sc->state == SC_ST_DIS || sc->state == SC_ST_CLO)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9623 | goto out; |
| 9624 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9625 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9626 | if (!b_size(&res->buf)) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9627 | sc_need_room(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9628 | goto out; |
| 9629 | } |
| 9630 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9631 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9632 | http_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9633 | |
| 9634 | /* Set the currently running flag. */ |
| 9635 | if (!HLUA_IS_RUNNING(hlua) && |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9636 | !(http_ctx->flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 9637 | if (!co_data(req)) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 9638 | applet_need_more_data(ctx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9639 | goto out; |
| 9640 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9641 | } |
| 9642 | |
| 9643 | /* Executes The applet if it is not done. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9644 | if (!(http_ctx->flags & APPLET_DONE)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9645 | |
| 9646 | /* Execute the function. */ |
| 9647 | switch (hlua_ctx_resume(hlua, 1)) { |
| 9648 | /* finished. */ |
| 9649 | case HLUA_E_OK: |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9650 | http_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9651 | break; |
| 9652 | |
| 9653 | /* yield. */ |
| 9654 | case HLUA_E_AGAIN: |
| 9655 | if (hlua->wake_time != TICK_ETERNITY) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9656 | task_schedule(http_ctx->task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9657 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9658 | |
| 9659 | /* finished with error. */ |
| 9660 | case HLUA_E_ERRMSG: |
| 9661 | /* Display log. */ |
| 9662 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9663 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9664 | lua_pop(hlua->T, 1); |
| 9665 | goto error; |
| 9666 | |
| 9667 | case HLUA_E_ETMOUT: |
| 9668 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9669 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9670 | goto error; |
| 9671 | |
| 9672 | case HLUA_E_NOMEM: |
| 9673 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9674 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9675 | goto error; |
| 9676 | |
| 9677 | case HLUA_E_YIELD: /* unexpected */ |
| 9678 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9679 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9680 | goto error; |
| 9681 | |
| 9682 | case HLUA_E_ERR: |
| 9683 | /* Display log. */ |
| 9684 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9685 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9686 | goto error; |
| 9687 | |
| 9688 | default: |
| 9689 | goto error; |
| 9690 | } |
| 9691 | } |
| 9692 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9693 | if (http_ctx->flags & APPLET_DONE) { |
| 9694 | if (http_ctx->flags & APPLET_RSP_SENT) |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9695 | goto done; |
| 9696 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9697 | if (!(http_ctx->flags & APPLET_HDR_SENT)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9698 | goto error; |
| 9699 | |
Christopher Faulet | f89af9c | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 9700 | /* no more data are expected. If the response buffer is empty |
| 9701 | * for a chunked message, be sure to add something (EOT block in |
| 9702 | * this case) to have something to send. It is important to be |
| 9703 | * sure the EOM flags will be handled by the endpoint. |
| 9704 | */ |
| 9705 | if (htx_is_empty(res_htx) && (strm->txn->rsp.flags & (HTTP_MSGF_XFER_LEN|HTTP_MSGF_CNT_LEN)) == HTTP_MSGF_XFER_LEN) { |
| 9706 | if (!htx_add_endof(res_htx, HTX_BLK_EOT)) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9707 | sc_need_room(sc); |
Christopher Faulet | f89af9c | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 9708 | goto out; |
| 9709 | } |
| 9710 | channel_add_input(res, 1); |
| 9711 | } |
| 9712 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 9713 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | d8d2708 | 2022-03-07 15:50:54 +0100 | [diff] [blame] | 9714 | res->flags |= CF_EOI; |
Willy Tarreau | d869e13 | 2022-05-17 18:05:31 +0200 | [diff] [blame] | 9715 | se_fl_set(ctx->sedesc, SE_FL_EOI); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9716 | strm->txn->status = http_ctx->status; |
| 9717 | http_ctx->flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9718 | } |
| 9719 | |
| 9720 | done: |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9721 | if (http_ctx->flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9722 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 6e1bbc4 | 2022-12-12 08:08:15 +0100 | [diff] [blame] | 9723 | res->flags |= CF_READ_EVENT; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9724 | sc_shutr(sc); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9725 | } |
| 9726 | |
| 9727 | /* eat the whole request */ |
| 9728 | if (co_data(req)) { |
| 9729 | req_htx = htx_from_buf(&req->buf); |
| 9730 | co_htx_skip(req, req_htx, co_data(req)); |
| 9731 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9732 | } |
| 9733 | } |
| 9734 | |
| 9735 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9736 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9737 | return; |
| 9738 | |
| 9739 | error: |
| 9740 | |
| 9741 | /* If we are in HTTP mode, and we are not send any |
| 9742 | * data, return a 500 server error in best effort: |
| 9743 | * if there is no room available in the buffer, |
| 9744 | * just close the connection. |
| 9745 | */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9746 | if (!(http_ctx->flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 9747 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9748 | |
| 9749 | channel_erase(res); |
| 9750 | res->buf.data = b_data(err); |
| 9751 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 9752 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 9753 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9754 | } |
| 9755 | if (!(strm->flags & SF_ERR_MASK)) |
| 9756 | strm->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9757 | http_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9758 | goto done; |
| 9759 | } |
| 9760 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9761 | static void hlua_applet_http_release(struct appctx *ctx) |
| 9762 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9763 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
| 9764 | |
| 9765 | task_destroy(http_ctx->task); |
| 9766 | http_ctx->task = NULL; |
| 9767 | hlua_ctx_destroy(http_ctx->hlua); |
| 9768 | http_ctx->hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9769 | } |
| 9770 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9771 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9772 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9773 | * |
| 9774 | * This function can fail with an abort() due to an Lua critical error. |
| 9775 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 9776 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9777 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9778 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 9779 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9780 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9781 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9782 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9783 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9784 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9785 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9786 | if (!rule->arg.hlua_rule) { |
| 9787 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9788 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9789 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9790 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9791 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 9792 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 9793 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9794 | if (!rule->arg.hlua_rule->args) { |
| 9795 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9796 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9797 | } |
| 9798 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9799 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9800 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9801 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9802 | /* Expect some arguments */ |
| 9803 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9804 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9805 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9806 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9807 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9808 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9809 | if (!rule->arg.hlua_rule->args[i]) { |
| 9810 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9811 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9812 | } |
| 9813 | (*cur_arg)++; |
| 9814 | } |
| 9815 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9816 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 9817 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9818 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9819 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9820 | |
| 9821 | error: |
| 9822 | if (rule->arg.hlua_rule) { |
| 9823 | if (rule->arg.hlua_rule->args) { |
| 9824 | for (i = 0; i < fcn->nargs; i++) |
| 9825 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 9826 | ha_free(&rule->arg.hlua_rule->args); |
| 9827 | } |
| 9828 | ha_free(&rule->arg.hlua_rule); |
| 9829 | } |
| 9830 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9831 | } |
| 9832 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9833 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 9834 | struct act_rule *rule, char **err) |
| 9835 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9836 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9837 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9838 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9839 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9840 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9841 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9842 | * the call of this analyzer. |
| 9843 | */ |
| 9844 | if (rule->from != ACT_F_HTTP_REQ) { |
| 9845 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 9846 | return ACT_RET_PRS_ERR; |
| 9847 | } |
| 9848 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9849 | /* Memory for the rule. */ |
| 9850 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 9851 | if (!rule->arg.hlua_rule) { |
| 9852 | memprintf(err, "out of memory error"); |
| 9853 | return ACT_RET_PRS_ERR; |
| 9854 | } |
| 9855 | |
| 9856 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9857 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9858 | |
| 9859 | /* TODO: later accept arguments. */ |
| 9860 | rule->arg.hlua_rule->args = NULL; |
| 9861 | |
| 9862 | /* Add applet pointer in the rule. */ |
| 9863 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 9864 | rule->applet.name = fcn->name; |
| 9865 | rule->applet.init = hlua_applet_http_init; |
| 9866 | rule->applet.fct = hlua_applet_http_fct; |
| 9867 | rule->applet.release = hlua_applet_http_release; |
| 9868 | rule->applet.timeout = hlua_timeout_applet; |
| 9869 | |
| 9870 | return ACT_RET_PRS_OK; |
| 9871 | } |
| 9872 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9873 | /* This function is an LUA binding used for registering |
| 9874 | * "sample-conv" functions. It expects a converter name used |
| 9875 | * in the haproxy configuration file, and an LUA function. |
| 9876 | */ |
| 9877 | __LJMP static int hlua_register_action(lua_State *L) |
| 9878 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9879 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9880 | const char *name; |
| 9881 | int ref; |
| 9882 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9883 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9884 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9885 | struct buffer *trash; |
| 9886 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9887 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9888 | /* Initialise the number of expected arguments at 0. */ |
| 9889 | nargs = 0; |
| 9890 | |
| 9891 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 9892 | 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] | 9893 | |
| 9894 | /* First argument : converter name. */ |
| 9895 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9896 | |
| 9897 | /* Second argument : environment. */ |
| 9898 | if (lua_type(L, 2) != LUA_TTABLE) |
| 9899 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9900 | |
| 9901 | /* Third argument : lua function. */ |
| 9902 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 9903 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9904 | /* 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] | 9905 | if (lua_gettop(L) >= 4) |
| 9906 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 9907 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9908 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9909 | lua_pushnil(L); |
| 9910 | while (lua_next(L, 2) != 0) { |
| 9911 | if (lua_type(L, -1) != LUA_TSTRING) |
| 9912 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9913 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9914 | /* Check if action exists */ |
| 9915 | trash = get_trash_chunk(); |
| 9916 | chunk_printf(trash, "lua.%s", name); |
| 9917 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 9918 | akw = tcp_req_cont_action(trash->area); |
| 9919 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 9920 | akw = tcp_res_cont_action(trash->area); |
| 9921 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 9922 | akw = action_http_req_custom(trash->area); |
| 9923 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 9924 | akw = action_http_res_custom(trash->area); |
| 9925 | } else { |
| 9926 | akw = NULL; |
| 9927 | } |
| 9928 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9929 | fcn = akw->private; |
| 9930 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9931 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 9932 | "This will become a hard error in version 2.5.\n", name); |
| 9933 | } |
| 9934 | fcn->function_ref[hlua_state_id] = ref; |
| 9935 | |
| 9936 | /* pop the environment string. */ |
| 9937 | lua_pop(L, 1); |
| 9938 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9939 | } |
| 9940 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9941 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 9942 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9943 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9944 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9945 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9946 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9947 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9948 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9949 | |
| 9950 | /* Fill fcn. */ |
| 9951 | fcn->name = strdup(name); |
| 9952 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9953 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9954 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9955 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 9956 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9957 | fcn->nargs = nargs; |
| 9958 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9959 | /* List head */ |
| 9960 | akl->list.n = akl->list.p = NULL; |
| 9961 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9962 | /* action keyword. */ |
| 9963 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9964 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9965 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9966 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9967 | |
| 9968 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 9969 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 9970 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9971 | akl->kw[0].private = fcn; |
| 9972 | akl->kw[0].parse = action_register_lua; |
| 9973 | |
| 9974 | /* select the action registering point. */ |
| 9975 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 9976 | tcp_req_cont_keywords_register(akl); |
| 9977 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 9978 | tcp_res_cont_keywords_register(akl); |
| 9979 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 9980 | http_req_keywords_register(akl); |
| 9981 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 9982 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9983 | else { |
| 9984 | release_hlua_function(fcn); |
| 9985 | if (akl) |
| 9986 | ha_free((char **)&(akl->kw[0].kw)); |
| 9987 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 9988 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9989 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 9990 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9991 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9992 | |
| 9993 | /* pop the environment string. */ |
| 9994 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9995 | |
| 9996 | /* reset for next loop */ |
| 9997 | akl = NULL; |
| 9998 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9999 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10000 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10001 | |
| 10002 | alloc_error: |
| 10003 | release_hlua_function(fcn); |
| 10004 | ha_free(&akl); |
| 10005 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 10006 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10007 | } |
| 10008 | |
| 10009 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 10010 | struct act_rule *rule, char **err) |
| 10011 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 10012 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10013 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 10014 | if (px->mode == PR_MODE_HTTP) { |
| 10015 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 10016 | return ACT_RET_PRS_ERR; |
| 10017 | } |
| 10018 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10019 | /* Memory for the rule. */ |
| 10020 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 10021 | if (!rule->arg.hlua_rule) { |
| 10022 | memprintf(err, "out of memory error"); |
| 10023 | return ACT_RET_PRS_ERR; |
| 10024 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10025 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10026 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10027 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10028 | |
| 10029 | /* TODO: later accept arguments. */ |
| 10030 | rule->arg.hlua_rule->args = NULL; |
| 10031 | |
| 10032 | /* Add applet pointer in the rule. */ |
| 10033 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 10034 | rule->applet.name = fcn->name; |
| 10035 | rule->applet.init = hlua_applet_tcp_init; |
| 10036 | rule->applet.fct = hlua_applet_tcp_fct; |
| 10037 | rule->applet.release = hlua_applet_tcp_release; |
| 10038 | rule->applet.timeout = hlua_timeout_applet; |
| 10039 | |
| 10040 | return 0; |
| 10041 | } |
| 10042 | |
| 10043 | /* This function is an LUA binding used for registering |
| 10044 | * "sample-conv" functions. It expects a converter name used |
| 10045 | * in the haproxy configuration file, and an LUA function. |
| 10046 | */ |
| 10047 | __LJMP static int hlua_register_service(lua_State *L) |
| 10048 | { |
| 10049 | struct action_kw_list *akl; |
| 10050 | const char *name; |
| 10051 | const char *env; |
| 10052 | int ref; |
| 10053 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10054 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10055 | struct buffer *trash; |
| 10056 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10057 | |
| 10058 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 10059 | |
| 10060 | /* First argument : converter name. */ |
| 10061 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 10062 | |
| 10063 | /* Second argument : environment. */ |
| 10064 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 10065 | |
| 10066 | /* Third argument : lua function. */ |
| 10067 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 10068 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10069 | /* Check for service already registered */ |
| 10070 | trash = get_trash_chunk(); |
| 10071 | chunk_printf(trash, "lua.%s", name); |
| 10072 | akw = service_find(trash->area); |
| 10073 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10074 | fcn = akw->private; |
| 10075 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 10076 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 10077 | "This will become a hard error in version 2.5.\n", name); |
| 10078 | } |
| 10079 | fcn->function_ref[hlua_state_id] = ref; |
| 10080 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10081 | } |
| 10082 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10083 | /* Allocate and fill the sample fetch keyword struct. */ |
| 10084 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 10085 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10086 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10087 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10088 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10089 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10090 | |
| 10091 | /* Fill fcn. */ |
| 10092 | len = strlen("<lua.>") + strlen(name) + 1; |
| 10093 | fcn->name = calloc(1, len); |
| 10094 | if (!fcn->name) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10095 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10096 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10097 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10098 | |
| 10099 | /* List head */ |
| 10100 | akl->list.n = akl->list.p = NULL; |
| 10101 | |
| 10102 | /* converter keyword. */ |
| 10103 | len = strlen("lua.") + strlen(name) + 1; |
| 10104 | akl->kw[0].kw = calloc(1, len); |
| 10105 | if (!akl->kw[0].kw) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10106 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10107 | |
| 10108 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 10109 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 10110 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10111 | if (strcmp(env, "tcp") == 0) |
| 10112 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10113 | else if (strcmp(env, "http") == 0) |
| 10114 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10115 | else { |
| 10116 | release_hlua_function(fcn); |
| 10117 | if (akl) |
| 10118 | ha_free((char **)&(akl->kw[0].kw)); |
| 10119 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 10120 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 10121 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10122 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10123 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 10124 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10125 | akl->kw[0].private = fcn; |
| 10126 | |
| 10127 | /* End of array. */ |
| 10128 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 10129 | |
| 10130 | /* Register this new converter */ |
| 10131 | service_keywords_register(akl); |
| 10132 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10133 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10134 | |
| 10135 | alloc_error: |
| 10136 | release_hlua_function(fcn); |
| 10137 | ha_free(&akl); |
| 10138 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 10139 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10140 | } |
| 10141 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10142 | /* This function initialises Lua cli handler. It copies the |
| 10143 | * arguments in the Lua stack and create channel IO objects. |
| 10144 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 10145 | 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] | 10146 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10147 | 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] | 10148 | struct hlua *hlua; |
| 10149 | struct hlua_function *fcn; |
| 10150 | int i; |
| 10151 | const char *error; |
| 10152 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10153 | fcn = private; |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10154 | ctx->fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10155 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10156 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10157 | if (!hlua) { |
| 10158 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10159 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10160 | } |
| 10161 | HLUA_INIT(hlua); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10162 | ctx->hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10163 | |
| 10164 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 10165 | * 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] | 10166 | * applet_http. It is absolutely compatible. |
| 10167 | */ |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10168 | ctx->task = task_new_here(); |
| 10169 | if (!ctx->task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 10170 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10171 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10172 | } |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10173 | ctx->task->nice = 0; |
| 10174 | ctx->task->context = appctx; |
| 10175 | ctx->task->process = hlua_applet_wakeup; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10176 | |
| 10177 | /* Initialises the Lua context */ |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10178 | 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] | 10179 | 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] | 10180 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10181 | } |
| 10182 | |
| 10183 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10184 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10185 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 10186 | error = lua_tostring(hlua->T, -1); |
| 10187 | else |
| 10188 | error = "critical error"; |
| 10189 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 10190 | goto error; |
| 10191 | } |
| 10192 | |
| 10193 | /* Check stack available size. */ |
| 10194 | if (!lua_checkstack(hlua->T, 2)) { |
| 10195 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10196 | goto error; |
| 10197 | } |
| 10198 | |
| 10199 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10200 | 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] | 10201 | |
| 10202 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 10203 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10204 | */ |
| 10205 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 10206 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10207 | goto error; |
| 10208 | } |
| 10209 | hlua->nargs = 1; |
| 10210 | |
| 10211 | /* push keywords in the stack. */ |
| 10212 | for (i = 0; *args[i]; i++) { |
| 10213 | /* Check stack available size. */ |
| 10214 | if (!lua_checkstack(hlua->T, 1)) { |
| 10215 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10216 | goto error; |
| 10217 | } |
| 10218 | lua_pushstring(hlua->T, args[i]); |
| 10219 | hlua->nargs++; |
| 10220 | } |
| 10221 | |
| 10222 | /* We must initialize the execution timeouts. */ |
| 10223 | hlua->max_time = hlua_timeout_session; |
| 10224 | |
| 10225 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10226 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10227 | |
| 10228 | /* It's ok */ |
| 10229 | return 0; |
| 10230 | |
| 10231 | /* It's not ok. */ |
| 10232 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10233 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10234 | hlua_ctx_destroy(hlua); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10235 | ctx->hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10236 | return 1; |
| 10237 | } |
| 10238 | |
| 10239 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 10240 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10241 | struct hlua_cli_ctx *ctx = appctx->svcctx; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10242 | struct hlua *hlua; |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 10243 | struct stconn *sc; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10244 | struct hlua_function *fcn; |
| 10245 | |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10246 | hlua = ctx->hlua; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 10247 | sc = appctx_sc(appctx); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10248 | fcn = ctx->fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10249 | |
| 10250 | /* If the stream is disconnect or closed, ldo nothing. */ |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 10251 | 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] | 10252 | return 1; |
| 10253 | |
| 10254 | /* Execute the function. */ |
| 10255 | switch (hlua_ctx_resume(hlua, 1)) { |
| 10256 | |
| 10257 | /* finished. */ |
| 10258 | case HLUA_E_OK: |
| 10259 | return 1; |
| 10260 | |
| 10261 | /* yield. */ |
| 10262 | case HLUA_E_AGAIN: |
| 10263 | /* We want write. */ |
| 10264 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 10265 | sc_need_room(sc); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10266 | /* Set the timeout. */ |
| 10267 | if (hlua->wake_time != TICK_ETERNITY) |
| 10268 | task_schedule(hlua->task, hlua->wake_time); |
| 10269 | return 0; |
| 10270 | |
| 10271 | /* finished with error. */ |
| 10272 | case HLUA_E_ERRMSG: |
| 10273 | /* Display log. */ |
| 10274 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 10275 | fcn->name, lua_tostring(hlua->T, -1)); |
| 10276 | lua_pop(hlua->T, 1); |
| 10277 | return 1; |
| 10278 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10279 | case HLUA_E_ETMOUT: |
| 10280 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 10281 | fcn->name); |
| 10282 | return 1; |
| 10283 | |
| 10284 | case HLUA_E_NOMEM: |
| 10285 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 10286 | fcn->name); |
| 10287 | return 1; |
| 10288 | |
| 10289 | case HLUA_E_YIELD: /* unexpected */ |
| 10290 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 10291 | fcn->name); |
| 10292 | return 1; |
| 10293 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10294 | case HLUA_E_ERR: |
| 10295 | /* Display log. */ |
| 10296 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 10297 | fcn->name); |
| 10298 | return 1; |
| 10299 | |
| 10300 | default: |
| 10301 | return 1; |
| 10302 | } |
| 10303 | |
| 10304 | return 1; |
| 10305 | } |
| 10306 | |
| 10307 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 10308 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10309 | struct hlua_cli_ctx *ctx = appctx->svcctx; |
| 10310 | |
| 10311 | hlua_ctx_destroy(ctx->hlua); |
| 10312 | ctx->hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10313 | } |
| 10314 | |
| 10315 | /* This function is an LUA binding used for registering |
| 10316 | * new keywords in the cli. It expects a list of keywords |
| 10317 | * which are the "path". It is limited to 5 keywords. A |
| 10318 | * description of the command, a function to be executed |
| 10319 | * for the parsing and a function for io handlers. |
| 10320 | */ |
| 10321 | __LJMP static int hlua_register_cli(lua_State *L) |
| 10322 | { |
| 10323 | struct cli_kw_list *cli_kws; |
| 10324 | const char *message; |
| 10325 | int ref_io; |
| 10326 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10327 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10328 | int index; |
| 10329 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10330 | struct buffer *trash; |
| 10331 | const char *kw[5]; |
| 10332 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10333 | const char *errmsg; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10334 | |
| 10335 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 10336 | |
| 10337 | /* First argument : an array of maximum 5 keywords. */ |
| 10338 | if (!lua_istable(L, 1)) |
| 10339 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 10340 | |
| 10341 | /* Second argument : string with contextual message. */ |
| 10342 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 10343 | |
| 10344 | /* Third and fourth argument : lua function. */ |
| 10345 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 10346 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10347 | /* Check for CLI service already registered */ |
| 10348 | trash = get_trash_chunk(); |
| 10349 | index = 0; |
| 10350 | lua_pushnil(L); |
| 10351 | memset(kw, 0, sizeof(kw)); |
| 10352 | while (lua_next(L, 1) != 0) { |
| 10353 | if (index >= CLI_PREFIX_KW_NB) |
| 10354 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 10355 | if (lua_type(L, -1) != LUA_TSTRING) |
| 10356 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 10357 | kw[index] = lua_tostring(L, -1); |
| 10358 | if (index == 0) |
| 10359 | chunk_printf(trash, "%s", kw[index]); |
| 10360 | else |
| 10361 | chunk_appendf(trash, " %s", kw[index]); |
| 10362 | index++; |
| 10363 | lua_pop(L, 1); |
| 10364 | } |
| 10365 | cli_kw = cli_find_kw_exact((char **)kw); |
| 10366 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10367 | fcn = cli_kw->private; |
| 10368 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 10369 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 10370 | "This will become a hard error in version 2.5.\n", trash->area); |
| 10371 | } |
| 10372 | fcn->function_ref[hlua_state_id] = ref_io; |
| 10373 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10374 | } |
| 10375 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10376 | /* Allocate and fill the sample fetch keyword struct. */ |
| 10377 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10378 | if (!cli_kws) { |
| 10379 | errmsg = "Lua out of memory error."; |
| 10380 | goto error; |
| 10381 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10382 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10383 | if (!fcn) { |
| 10384 | errmsg = "Lua out of memory error."; |
| 10385 | goto error; |
| 10386 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10387 | |
| 10388 | /* Fill path. */ |
| 10389 | index = 0; |
| 10390 | lua_pushnil(L); |
| 10391 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10392 | if (index >= 5) { |
| 10393 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 10394 | goto error; |
| 10395 | } |
| 10396 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 10397 | errmsg = "1st argument must be a table filled with strings"; |
| 10398 | goto error; |
| 10399 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10400 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10401 | if (!cli_kws->kw[0].str_kw[index]) { |
| 10402 | errmsg = "Lua out of memory error."; |
| 10403 | goto error; |
| 10404 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10405 | index++; |
| 10406 | lua_pop(L, 1); |
| 10407 | } |
| 10408 | |
| 10409 | /* Copy help message. */ |
| 10410 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10411 | if (!cli_kws->kw[0].usage) { |
| 10412 | errmsg = "Lua out of memory error."; |
| 10413 | goto error; |
| 10414 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10415 | |
| 10416 | /* Fill fcn io handler. */ |
| 10417 | len = strlen("<lua.cli>") + 1; |
| 10418 | for (i = 0; i < index; i++) |
| 10419 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 10420 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10421 | if (!fcn->name) { |
| 10422 | errmsg = "Lua out of memory error."; |
| 10423 | goto error; |
| 10424 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10425 | strncat((char *)fcn->name, "<lua.cli", len); |
| 10426 | for (i = 0; i < index; i++) { |
| 10427 | strncat((char *)fcn->name, ".", len); |
| 10428 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 10429 | } |
| 10430 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10431 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10432 | |
| 10433 | /* Fill last entries. */ |
| 10434 | cli_kws->kw[0].private = fcn; |
| 10435 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 10436 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 10437 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 10438 | |
| 10439 | /* Register this new converter */ |
| 10440 | cli_register_kw(cli_kws); |
| 10441 | |
| 10442 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10443 | |
| 10444 | error: |
| 10445 | release_hlua_function(fcn); |
| 10446 | if (cli_kws) { |
| 10447 | for (i = 0; i < index; i++) |
| 10448 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 10449 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 10450 | } |
| 10451 | ha_free(&cli_kws); |
| 10452 | WILL_LJMP(luaL_error(L, errmsg)); |
| 10453 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10454 | } |
| 10455 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10456 | static int hlua_filter_init_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 10457 | { |
| 10458 | struct hlua_flt_config *conf = fconf->conf; |
| 10459 | lua_State *L; |
| 10460 | int error, pos, state_id, flt_ref; |
| 10461 | |
| 10462 | state_id = reg_flt_to_stack_id(conf->reg); |
| 10463 | L = hlua_states[state_id]; |
| 10464 | pos = lua_gettop(L); |
| 10465 | |
| 10466 | /* The filter parsing function */ |
| 10467 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->fun_ref[state_id]); |
| 10468 | |
| 10469 | /* Push the filter class on the stack and resolve all callbacks */ |
| 10470 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->flt_ref[state_id]); |
| 10471 | |
| 10472 | /* Duplicate the filter class so each filter will have its own copy */ |
| 10473 | lua_newtable(L); |
| 10474 | lua_pushnil(L); |
| 10475 | |
| 10476 | while (lua_next(L, pos+2)) { |
| 10477 | lua_pushvalue(L, -2); |
| 10478 | lua_insert(L, -2); |
| 10479 | lua_settable(L, -4); |
| 10480 | } |
| 10481 | flt_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
| 10482 | |
| 10483 | /* Remove the original lua filter class from the stack */ |
| 10484 | lua_pop(L, 1); |
| 10485 | |
| 10486 | /* Push the copy on the stack */ |
| 10487 | lua_rawgeti(L, LUA_REGISTRYINDEX, flt_ref); |
| 10488 | |
| 10489 | /* extra args are pushed in a table */ |
| 10490 | lua_newtable(L); |
| 10491 | for (pos = 0; conf->args[pos]; pos++) { |
| 10492 | /* Check stack available size. */ |
| 10493 | if (!lua_checkstack(L, 1)) { |
| 10494 | ha_alert("Lua filter '%s' : Lua error : full stack.", conf->reg->name); |
| 10495 | goto error; |
| 10496 | } |
| 10497 | lua_pushstring(L, conf->args[pos]); |
| 10498 | lua_rawseti(L, -2, lua_rawlen(L, -2) + 1); |
| 10499 | } |
| 10500 | |
| 10501 | error = lua_pcall(L, 2, LUA_MULTRET, 0); |
| 10502 | switch (error) { |
| 10503 | case LUA_OK: |
| 10504 | /* replace the filter ref */ |
| 10505 | conf->ref[state_id] = flt_ref; |
| 10506 | break; |
| 10507 | case LUA_ERRRUN: |
| 10508 | ha_alert("Lua filter '%s' : runtime error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10509 | goto error; |
| 10510 | case LUA_ERRMEM: |
| 10511 | ha_alert("Lua filter '%s' : out of memory error", conf->reg->name); |
| 10512 | goto error; |
| 10513 | case LUA_ERRERR: |
| 10514 | ha_alert("Lua filter '%s' : message handler error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10515 | goto error; |
| 10516 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
| 10517 | case LUA_ERRGCMM: |
| 10518 | ha_alert("Lua filter '%s' : garbage collector error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10519 | goto error; |
| 10520 | #endif |
| 10521 | default: |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 10522 | 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] | 10523 | goto error; |
| 10524 | } |
| 10525 | |
| 10526 | lua_settop(L, 0); |
| 10527 | return 0; |
| 10528 | |
| 10529 | error: |
| 10530 | lua_settop(L, 0); |
| 10531 | return -1; |
| 10532 | } |
| 10533 | |
| 10534 | static void hlua_filter_deinit_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 10535 | { |
| 10536 | struct hlua_flt_config *conf = fconf->conf; |
| 10537 | lua_State *L; |
| 10538 | int state_id; |
| 10539 | |
| 10540 | if (!conf) |
| 10541 | return; |
| 10542 | |
| 10543 | state_id = reg_flt_to_stack_id(conf->reg); |
| 10544 | L = hlua_states[state_id]; |
| 10545 | luaL_unref(L, LUA_REGISTRYINDEX, conf->ref[state_id]); |
| 10546 | } |
| 10547 | |
| 10548 | static int hlua_filter_init(struct proxy *px, struct flt_conf *fconf) |
| 10549 | { |
| 10550 | struct hlua_flt_config *conf = fconf->conf; |
| 10551 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 10552 | |
| 10553 | /* Rely on per-thread init for global scripts */ |
| 10554 | if (!state_id) |
| 10555 | return hlua_filter_init_per_thread(px, fconf); |
| 10556 | return 0; |
| 10557 | } |
| 10558 | |
| 10559 | static void hlua_filter_deinit(struct proxy *px, struct flt_conf *fconf) |
| 10560 | { |
| 10561 | |
| 10562 | if (fconf->conf) { |
| 10563 | struct hlua_flt_config *conf = fconf->conf; |
| 10564 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 10565 | int pos; |
| 10566 | |
| 10567 | /* Rely on per-thread deinit for global scripts */ |
| 10568 | if (!state_id) |
| 10569 | hlua_filter_deinit_per_thread(px, fconf); |
| 10570 | |
| 10571 | for (pos = 0; conf->args[pos]; pos++) |
| 10572 | free(conf->args[pos]); |
| 10573 | free(conf->args); |
| 10574 | } |
| 10575 | ha_free(&fconf->conf); |
| 10576 | ha_free((char **)&fconf->id); |
| 10577 | ha_free(&fconf->ops); |
| 10578 | } |
| 10579 | |
| 10580 | static int hlua_filter_new(struct stream *s, struct filter *filter) |
| 10581 | { |
| 10582 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 10583 | struct hlua_flt_ctx *flt_ctx = NULL; |
| 10584 | int ret = 1; |
| 10585 | |
| 10586 | /* In the execution wrappers linked with a stream, the |
| 10587 | * Lua context can be not initialized. This behavior |
| 10588 | * permits to save performances because a systematic |
| 10589 | * Lua initialization cause 5% performances loss. |
| 10590 | */ |
| 10591 | if (!s->hlua) { |
| 10592 | struct hlua *hlua; |
| 10593 | |
| 10594 | hlua = pool_alloc(pool_head_hlua); |
| 10595 | if (!hlua) { |
| 10596 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 10597 | conf->reg->name); |
| 10598 | ret = 0; |
| 10599 | goto end; |
| 10600 | } |
| 10601 | HLUA_INIT(hlua); |
| 10602 | s->hlua = hlua; |
| 10603 | if (!hlua_ctx_init(s->hlua, reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 10604 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 10605 | conf->reg->name); |
| 10606 | ret = 0; |
| 10607 | goto end; |
| 10608 | } |
| 10609 | } |
| 10610 | |
| 10611 | flt_ctx = pool_zalloc(pool_head_hlua_flt_ctx); |
| 10612 | if (!flt_ctx) { |
| 10613 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10614 | conf->reg->name); |
| 10615 | ret = 0; |
| 10616 | goto end; |
| 10617 | } |
| 10618 | flt_ctx->hlua[0] = pool_alloc(pool_head_hlua); |
| 10619 | flt_ctx->hlua[1] = pool_alloc(pool_head_hlua); |
| 10620 | if (!flt_ctx->hlua[0] || !flt_ctx->hlua[1]) { |
| 10621 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10622 | conf->reg->name); |
| 10623 | ret = 0; |
| 10624 | goto end; |
| 10625 | } |
| 10626 | HLUA_INIT(flt_ctx->hlua[0]); |
| 10627 | HLUA_INIT(flt_ctx->hlua[1]); |
| 10628 | if (!hlua_ctx_init(flt_ctx->hlua[0], reg_flt_to_stack_id(conf->reg), s->task, 0) || |
| 10629 | !hlua_ctx_init(flt_ctx->hlua[1], reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 10630 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10631 | conf->reg->name); |
| 10632 | ret = 0; |
| 10633 | goto end; |
| 10634 | } |
| 10635 | |
| 10636 | if (!HLUA_IS_RUNNING(s->hlua)) { |
| 10637 | /* The following Lua calls can fail. */ |
| 10638 | if (!SET_SAFE_LJMP(s->hlua)) { |
| 10639 | const char *error; |
| 10640 | |
| 10641 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 10642 | error = lua_tostring(s->hlua->T, -1); |
| 10643 | else |
| 10644 | error = "critical error"; |
| 10645 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 10646 | ret = 0; |
| 10647 | goto end; |
| 10648 | } |
| 10649 | |
| 10650 | /* Check stack size. */ |
| 10651 | if (!lua_checkstack(s->hlua->T, 1)) { |
| 10652 | 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] | 10653 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10654 | ret = 0; |
| 10655 | goto end; |
| 10656 | } |
| 10657 | |
| 10658 | lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, conf->ref[s->hlua->state_id]); |
| 10659 | if (lua_getfield(s->hlua->T, -1, "new") != LUA_TFUNCTION) { |
| 10660 | SEND_ERR(s->be, "Lua filter '%s': 'new' field is not a function.\n", |
| 10661 | conf->reg->name); |
| 10662 | RESET_SAFE_LJMP(s->hlua); |
| 10663 | ret = 0; |
| 10664 | goto end; |
| 10665 | } |
| 10666 | lua_insert(s->hlua->T, -2); |
| 10667 | |
| 10668 | /* Push the copy on the stack */ |
| 10669 | s->hlua->nargs = 1; |
| 10670 | |
| 10671 | /* We must initialize the execution timeouts. */ |
| 10672 | s->hlua->max_time = hlua_timeout_session; |
| 10673 | |
| 10674 | /* At this point the execution is safe. */ |
| 10675 | RESET_SAFE_LJMP(s->hlua); |
| 10676 | } |
| 10677 | |
| 10678 | switch (hlua_ctx_resume(s->hlua, 0)) { |
| 10679 | case HLUA_E_OK: |
| 10680 | /* Nothing returned or not a table, ignore the filter for current stream */ |
| 10681 | if (!lua_gettop(s->hlua->T) || !lua_istable(s->hlua->T, 1)) { |
| 10682 | ret = 0; |
| 10683 | goto end; |
| 10684 | } |
| 10685 | |
| 10686 | /* Attached the filter pointer to the ctx */ |
| 10687 | lua_pushstring(s->hlua->T, "__filter"); |
| 10688 | lua_pushlightuserdata(s->hlua->T, filter); |
| 10689 | lua_settable(s->hlua->T, -3); |
| 10690 | |
| 10691 | /* Save a ref on the filter ctx */ |
| 10692 | lua_pushvalue(s->hlua->T, 1); |
| 10693 | flt_ctx->ref = luaL_ref(s->hlua->T, LUA_REGISTRYINDEX); |
| 10694 | filter->ctx = flt_ctx; |
| 10695 | break; |
| 10696 | case HLUA_E_ERRMSG: |
| 10697 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(s->hlua->T, -1)); |
| 10698 | ret = -1; |
| 10699 | goto end; |
| 10700 | case HLUA_E_ETMOUT: |
| 10701 | SEND_ERR(s->be, "Lua filter '%s' : 'new' execution timeout.\n", conf->reg->name); |
| 10702 | ret = 0; |
| 10703 | goto end; |
| 10704 | case HLUA_E_NOMEM: |
| 10705 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10706 | ret = 0; |
| 10707 | goto end; |
| 10708 | case HLUA_E_AGAIN: |
| 10709 | case HLUA_E_YIELD: |
| 10710 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10711 | " are not allowed from 'new' function.\n", conf->reg->name); |
| 10712 | ret = 0; |
| 10713 | goto end; |
| 10714 | case HLUA_E_ERR: |
| 10715 | SEND_ERR(s->be, "Lua filter '%s': 'new' returns an unknown error.\n", conf->reg->name); |
| 10716 | ret = 0; |
| 10717 | goto end; |
| 10718 | default: |
| 10719 | ret = 0; |
| 10720 | goto end; |
| 10721 | } |
| 10722 | |
| 10723 | end: |
| 10724 | if (s->hlua) |
| 10725 | lua_settop(s->hlua->T, 0); |
| 10726 | if (ret <= 0) { |
| 10727 | if (flt_ctx) { |
| 10728 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10729 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10730 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10731 | } |
| 10732 | } |
| 10733 | return ret; |
| 10734 | } |
| 10735 | |
| 10736 | static void hlua_filter_delete(struct stream *s, struct filter *filter) |
| 10737 | { |
| 10738 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10739 | |
| 10740 | luaL_unref(s->hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 10741 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10742 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10743 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10744 | filter->ctx = NULL; |
| 10745 | } |
| 10746 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 10747 | static int hlua_filter_from_payload(struct filter *filter) |
| 10748 | { |
| 10749 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10750 | |
| 10751 | return (flt_ctx && !!(flt_ctx->flags & HLUA_FLT_CTX_FL_PAYLOAD)); |
| 10752 | } |
| 10753 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10754 | static int hlua_filter_callback(struct stream *s, struct filter *filter, const char *fun, |
| 10755 | int dir, unsigned int flags) |
| 10756 | { |
| 10757 | struct hlua *flt_hlua; |
| 10758 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 10759 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10760 | unsigned int hflags = HLUA_TXN_FLT_CTX; |
| 10761 | int ret = 1; |
| 10762 | |
| 10763 | flt_hlua = flt_ctx->hlua[(dir == SMP_OPT_DIR_REQ ? 0 : 1)]; |
| 10764 | if (!flt_hlua) |
| 10765 | goto end; |
| 10766 | |
| 10767 | if (!HLUA_IS_RUNNING(flt_hlua)) { |
| 10768 | int extra_idx = lua_gettop(flt_hlua->T); |
| 10769 | |
| 10770 | /* The following Lua calls can fail. */ |
| 10771 | if (!SET_SAFE_LJMP(flt_hlua)) { |
| 10772 | const char *error; |
| 10773 | |
| 10774 | if (lua_type(flt_hlua->T, -1) == LUA_TSTRING) |
| 10775 | error = lua_tostring(flt_hlua->T, -1); |
| 10776 | else |
| 10777 | error = "critical error"; |
| 10778 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 10779 | goto end; |
| 10780 | } |
| 10781 | |
| 10782 | /* Check stack size. */ |
| 10783 | if (!lua_checkstack(flt_hlua->T, 3)) { |
| 10784 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10785 | RESET_SAFE_LJMP(flt_hlua); |
| 10786 | goto end; |
| 10787 | } |
| 10788 | |
| 10789 | lua_rawgeti(flt_hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 10790 | if (lua_getfield(flt_hlua->T, -1, fun) != LUA_TFUNCTION) { |
| 10791 | RESET_SAFE_LJMP(flt_hlua); |
| 10792 | goto end; |
| 10793 | } |
| 10794 | lua_insert(flt_hlua->T, -2); |
| 10795 | |
| 10796 | if (!hlua_txn_new(flt_hlua->T, s, s->be, dir, hflags)) { |
| 10797 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10798 | RESET_SAFE_LJMP(flt_hlua); |
| 10799 | goto end; |
| 10800 | } |
| 10801 | flt_hlua->nargs = 2; |
| 10802 | |
| 10803 | if (flags & HLUA_FLT_CB_ARG_CHN) { |
| 10804 | if (dir == SMP_OPT_DIR_REQ) |
| 10805 | lua_getfield(flt_hlua->T, -1, "req"); |
| 10806 | else |
| 10807 | lua_getfield(flt_hlua->T, -1, "res"); |
| 10808 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10809 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10810 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10811 | lua_settable(flt_hlua->T, -3); |
| 10812 | } |
| 10813 | flt_hlua->nargs++; |
| 10814 | } |
| 10815 | else if (flags & HLUA_FLT_CB_ARG_HTTP_MSG) { |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10816 | if (dir == SMP_OPT_DIR_REQ) |
| 10817 | lua_getfield(flt_hlua->T, -1, "http_req"); |
| 10818 | else |
| 10819 | lua_getfield(flt_hlua->T, -1, "http_res"); |
| 10820 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10821 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10822 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10823 | lua_settable(flt_hlua->T, -3); |
| 10824 | } |
| 10825 | flt_hlua->nargs++; |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10826 | } |
| 10827 | |
| 10828 | /* Check stack size. */ |
| 10829 | if (!lua_checkstack(flt_hlua->T, 1)) { |
| 10830 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10831 | RESET_SAFE_LJMP(flt_hlua); |
| 10832 | goto end; |
| 10833 | } |
| 10834 | |
| 10835 | while (extra_idx--) { |
| 10836 | lua_pushvalue(flt_hlua->T, 1); |
| 10837 | lua_remove(flt_hlua->T, 1); |
| 10838 | flt_hlua->nargs++; |
| 10839 | } |
| 10840 | |
| 10841 | /* We must initialize the execution timeouts. */ |
| 10842 | flt_hlua->max_time = hlua_timeout_session; |
| 10843 | |
| 10844 | /* At this point the execution is safe. */ |
| 10845 | RESET_SAFE_LJMP(flt_hlua); |
| 10846 | } |
| 10847 | |
| 10848 | switch (hlua_ctx_resume(flt_hlua, !(flags & HLUA_FLT_CB_FINAL))) { |
| 10849 | case HLUA_E_OK: |
| 10850 | /* Catch the return value if it required */ |
| 10851 | if ((flags & HLUA_FLT_CB_RETVAL) && lua_gettop(flt_hlua->T) > 0) { |
| 10852 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10853 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10854 | } |
| 10855 | |
| 10856 | /* Set timeout in the required channel. */ |
| 10857 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10858 | if (dir == SMP_OPT_DIR_REQ) |
| 10859 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10860 | else |
| 10861 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10862 | } |
| 10863 | break; |
| 10864 | case HLUA_E_AGAIN: |
| 10865 | /* Set timeout in the required channel. */ |
| 10866 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10867 | if (dir == SMP_OPT_DIR_REQ) |
| 10868 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10869 | else |
| 10870 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10871 | } |
| 10872 | /* Some actions can be wake up when a "write" event |
| 10873 | * is detected on a response channel. This is useful |
| 10874 | * only for actions targeted on the requests. |
| 10875 | */ |
| 10876 | if (HLUA_IS_WAKERESWR(flt_hlua)) |
| 10877 | s->res.flags |= CF_WAKE_WRITE; |
| 10878 | if (HLUA_IS_WAKEREQWR(flt_hlua)) |
| 10879 | s->req.flags |= CF_WAKE_WRITE; |
| 10880 | ret = 0; |
| 10881 | goto end; |
| 10882 | case HLUA_E_ERRMSG: |
| 10883 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(flt_hlua->T, -1)); |
| 10884 | ret = -1; |
| 10885 | goto end; |
| 10886 | case HLUA_E_ETMOUT: |
| 10887 | SEND_ERR(s->be, "Lua filter '%s' : '%s' callback execution timeout.\n", conf->reg->name, fun); |
| 10888 | goto end; |
| 10889 | case HLUA_E_NOMEM: |
| 10890 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10891 | goto end; |
| 10892 | case HLUA_E_YIELD: |
| 10893 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10894 | " are not allowed from '%s' callback.\n", conf->reg->name, fun); |
| 10895 | goto end; |
| 10896 | case HLUA_E_ERR: |
| 10897 | SEND_ERR(s->be, "Lua filter '%s': '%s' returns an unknown error.\n", conf->reg->name, fun); |
| 10898 | goto end; |
| 10899 | default: |
| 10900 | goto end; |
| 10901 | } |
| 10902 | |
| 10903 | |
| 10904 | end: |
| 10905 | return ret; |
| 10906 | } |
| 10907 | |
| 10908 | static int hlua_filter_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 10909 | { |
| 10910 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10911 | |
| 10912 | flt_ctx->flags = 0; |
| 10913 | return hlua_filter_callback(s, filter, "start_analyze", |
| 10914 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10915 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 10916 | } |
| 10917 | |
| 10918 | static int hlua_filter_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 10919 | { |
| 10920 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10921 | |
| 10922 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10923 | return hlua_filter_callback(s, filter, "end_analyze", |
| 10924 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10925 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 10926 | } |
| 10927 | |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10928 | static int hlua_filter_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 10929 | { |
| 10930 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10931 | |
| 10932 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10933 | return hlua_filter_callback(s, filter, "http_headers", |
| 10934 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10935 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10936 | } |
| 10937 | |
| 10938 | static int hlua_filter_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg, |
| 10939 | unsigned int offset, unsigned int len) |
| 10940 | { |
| 10941 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10942 | struct hlua *flt_hlua; |
| 10943 | int dir = (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 10944 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 10945 | int ret; |
| 10946 | |
| 10947 | flt_hlua = flt_ctx->hlua[idx]; |
| 10948 | flt_ctx->cur_off[idx] = offset; |
| 10949 | flt_ctx->cur_len[idx] = len; |
| 10950 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 10951 | ret = hlua_filter_callback(s, filter, "http_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10952 | if (ret != -1) { |
| 10953 | ret = flt_ctx->cur_len[idx]; |
| 10954 | if (lua_gettop(flt_hlua->T) > 0) { |
| 10955 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10956 | if (ret > flt_ctx->cur_len[idx]) |
| 10957 | ret = flt_ctx->cur_len[idx]; |
| 10958 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10959 | } |
| 10960 | } |
| 10961 | return ret; |
| 10962 | } |
| 10963 | |
| 10964 | static int hlua_filter_http_end(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 10965 | { |
| 10966 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10967 | |
| 10968 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10969 | return hlua_filter_callback(s, filter, "http_end", |
| 10970 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10971 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10972 | } |
| 10973 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10974 | static int hlua_filter_tcp_payload(struct stream *s, struct filter *filter, struct channel *chn, |
| 10975 | unsigned int offset, unsigned int len) |
| 10976 | { |
| 10977 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10978 | struct hlua *flt_hlua; |
| 10979 | int dir = (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 10980 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 10981 | int ret; |
| 10982 | |
| 10983 | flt_hlua = flt_ctx->hlua[idx]; |
| 10984 | flt_ctx->cur_off[idx] = offset; |
| 10985 | flt_ctx->cur_len[idx] = len; |
| 10986 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 10987 | ret = hlua_filter_callback(s, filter, "tcp_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_CHN)); |
| 10988 | if (ret != -1) { |
| 10989 | ret = flt_ctx->cur_len[idx]; |
| 10990 | if (lua_gettop(flt_hlua->T) > 0) { |
| 10991 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10992 | if (ret > flt_ctx->cur_len[idx]) |
| 10993 | ret = flt_ctx->cur_len[idx]; |
| 10994 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10995 | } |
| 10996 | } |
| 10997 | return ret; |
| 10998 | } |
| 10999 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11000 | static int hlua_filter_parse_fct(char **args, int *cur_arg, struct proxy *px, |
| 11001 | struct flt_conf *fconf, char **err, void *private) |
| 11002 | { |
| 11003 | struct hlua_reg_filter *reg_flt = private; |
| 11004 | lua_State *L; |
| 11005 | struct hlua_flt_config *conf = NULL; |
| 11006 | const char *flt_id = NULL; |
| 11007 | int state_id, pos, flt_flags = 0; |
| 11008 | struct flt_ops *hlua_flt_ops = NULL; |
| 11009 | |
| 11010 | state_id = reg_flt_to_stack_id(reg_flt); |
| 11011 | L = hlua_states[state_id]; |
| 11012 | |
| 11013 | /* Initialize the filter ops with default callbacks */ |
| 11014 | hlua_flt_ops = calloc(1, sizeof(*hlua_flt_ops)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11015 | if (!hlua_flt_ops) |
| 11016 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11017 | hlua_flt_ops->init = hlua_filter_init; |
| 11018 | hlua_flt_ops->deinit = hlua_filter_deinit; |
| 11019 | if (state_id) { |
| 11020 | /* Set per-thread callback if script is loaded per-thread */ |
| 11021 | hlua_flt_ops->init_per_thread = hlua_filter_init_per_thread; |
| 11022 | hlua_flt_ops->deinit_per_thread = hlua_filter_deinit_per_thread; |
| 11023 | } |
| 11024 | hlua_flt_ops->attach = hlua_filter_new; |
| 11025 | hlua_flt_ops->detach = hlua_filter_delete; |
| 11026 | |
| 11027 | /* Push the filter class on the stack and resolve all callbacks */ |
| 11028 | lua_rawgeti(L, LUA_REGISTRYINDEX, reg_flt->flt_ref[state_id]); |
| 11029 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11030 | if (lua_getfield(L, -1, "start_analyze") == LUA_TFUNCTION) |
| 11031 | hlua_flt_ops->channel_start_analyze = hlua_filter_start_analyze; |
| 11032 | lua_pop(L, 1); |
| 11033 | if (lua_getfield(L, -1, "end_analyze") == LUA_TFUNCTION) |
| 11034 | hlua_flt_ops->channel_end_analyze = hlua_filter_end_analyze; |
| 11035 | lua_pop(L, 1); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 11036 | if (lua_getfield(L, -1, "http_headers") == LUA_TFUNCTION) |
| 11037 | hlua_flt_ops->http_headers = hlua_filter_http_headers; |
| 11038 | lua_pop(L, 1); |
| 11039 | if (lua_getfield(L, -1, "http_payload") == LUA_TFUNCTION) |
| 11040 | hlua_flt_ops->http_payload = hlua_filter_http_payload; |
| 11041 | lua_pop(L, 1); |
| 11042 | if (lua_getfield(L, -1, "http_end") == LUA_TFUNCTION) |
| 11043 | hlua_flt_ops->http_end = hlua_filter_http_end; |
| 11044 | lua_pop(L, 1); |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11045 | if (lua_getfield(L, -1, "tcp_payload") == LUA_TFUNCTION) |
| 11046 | hlua_flt_ops->tcp_payload = hlua_filter_tcp_payload; |
| 11047 | lua_pop(L, 1); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11048 | |
| 11049 | /* Get id and flags of the filter class */ |
| 11050 | if (lua_getfield(L, -1, "id") == LUA_TSTRING) |
| 11051 | flt_id = lua_tostring(L, -1); |
| 11052 | lua_pop(L, 1); |
| 11053 | if (lua_getfield(L, -1, "flags") == LUA_TNUMBER) |
| 11054 | flt_flags = lua_tointeger(L, -1); |
| 11055 | lua_pop(L, 1); |
| 11056 | |
| 11057 | /* Create the filter config */ |
| 11058 | conf = calloc(1, sizeof(*conf)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11059 | if (!conf) |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11060 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11061 | conf->reg = reg_flt; |
| 11062 | |
| 11063 | /* duplicate args */ |
| 11064 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++); |
| 11065 | conf->args = calloc(pos + 1, sizeof(*conf->args)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11066 | if (!conf->args) |
| 11067 | goto error; |
| 11068 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++) { |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11069 | conf->args[pos] = strdup(args[*cur_arg + 1 + pos]); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11070 | if (!conf->args[pos]) |
| 11071 | goto error; |
| 11072 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11073 | conf->args[pos] = NULL; |
| 11074 | *cur_arg += pos + 1; |
| 11075 | |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11076 | if (flt_id) { |
| 11077 | fconf->id = strdup(flt_id); |
| 11078 | if (!fconf->id) |
| 11079 | goto error; |
| 11080 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11081 | fconf->flags = flt_flags; |
| 11082 | fconf->conf = conf; |
| 11083 | fconf->ops = hlua_flt_ops; |
| 11084 | |
| 11085 | lua_settop(L, 0); |
| 11086 | return 0; |
| 11087 | |
| 11088 | error: |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11089 | 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] | 11090 | free(hlua_flt_ops); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11091 | if (conf && conf->args) { |
| 11092 | for (pos = 0; conf->args[pos]; pos++) |
| 11093 | free(conf->args[pos]); |
| 11094 | free(conf->args); |
| 11095 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11096 | free(conf); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11097 | free((char *)fconf->id); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11098 | lua_settop(L, 0); |
| 11099 | return -1; |
| 11100 | } |
| 11101 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11102 | __LJMP static int hlua_register_data_filter(lua_State *L) |
| 11103 | { |
| 11104 | struct filter *filter; |
| 11105 | struct channel *chn; |
| 11106 | |
| 11107 | MAY_LJMP(check_args(L, 2, "register_data_filter")); |
| 11108 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 11109 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 11110 | |
| 11111 | lua_getfield(L, 1, "__filter"); |
| 11112 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 11113 | filter = lua_touserdata (L, -1); |
| 11114 | lua_pop(L, 1); |
| 11115 | |
| 11116 | register_data_filter(chn_strm(chn), chn, filter); |
| 11117 | return 1; |
| 11118 | } |
| 11119 | |
| 11120 | __LJMP static int hlua_unregister_data_filter(lua_State *L) |
| 11121 | { |
| 11122 | struct filter *filter; |
| 11123 | struct channel *chn; |
| 11124 | |
| 11125 | MAY_LJMP(check_args(L, 2, "unregister_data_filter")); |
| 11126 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 11127 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 11128 | |
| 11129 | lua_getfield(L, 1, "__filter"); |
| 11130 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 11131 | filter = lua_touserdata (L, -1); |
| 11132 | lua_pop(L, 1); |
| 11133 | |
| 11134 | unregister_data_filter(chn_strm(chn), chn, filter); |
| 11135 | return 1; |
| 11136 | } |
| 11137 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11138 | /* 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] | 11139 | * 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] | 11140 | * parse configuration arguments. |
| 11141 | */ |
| 11142 | __LJMP static int hlua_register_filter(lua_State *L) |
| 11143 | { |
| 11144 | struct buffer *trash; |
| 11145 | struct flt_kw_list *fkl; |
| 11146 | struct flt_kw *fkw; |
| 11147 | const char *name; |
| 11148 | struct hlua_reg_filter *reg_flt= NULL; |
| 11149 | int flt_ref, fun_ref; |
| 11150 | int len; |
| 11151 | |
| 11152 | MAY_LJMP(check_args(L, 3, "register_filter")); |
| 11153 | |
| 11154 | /* First argument : filter name. */ |
| 11155 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 11156 | |
| 11157 | /* Second argument : The filter class */ |
| 11158 | flt_ref = MAY_LJMP(hlua_checktable(L, 2)); |
| 11159 | |
| 11160 | /* Third argument : lua function. */ |
| 11161 | fun_ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 11162 | |
| 11163 | trash = get_trash_chunk(); |
| 11164 | chunk_printf(trash, "lua.%s", name); |
| 11165 | fkw = flt_find_kw(trash->area); |
| 11166 | if (fkw != NULL) { |
| 11167 | reg_flt = fkw->private; |
| 11168 | if (reg_flt->flt_ref[hlua_state_id] != -1 || reg_flt->fun_ref[hlua_state_id] != -1) { |
| 11169 | ha_warning("Trying to register filter 'lua.%s' more than once. " |
| 11170 | "This will become a hard error in version 2.5.\n", name); |
| 11171 | } |
| 11172 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 11173 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 11174 | return 0; |
| 11175 | } |
| 11176 | |
| 11177 | fkl = calloc(1, sizeof(*fkl) + sizeof(struct flt_kw) * 2); |
| 11178 | if (!fkl) |
| 11179 | goto alloc_error; |
| 11180 | fkl->scope = "HLUA"; |
| 11181 | |
| 11182 | reg_flt = new_hlua_reg_filter(name); |
| 11183 | if (!reg_flt) |
| 11184 | goto alloc_error; |
| 11185 | |
| 11186 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 11187 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 11188 | |
| 11189 | /* The filter keyword */ |
| 11190 | len = strlen("lua.") + strlen(name) + 1; |
| 11191 | fkl->kw[0].kw = calloc(1, len); |
| 11192 | if (!fkl->kw[0].kw) |
| 11193 | goto alloc_error; |
| 11194 | |
| 11195 | snprintf((char *)fkl->kw[0].kw, len, "lua.%s", name); |
| 11196 | |
| 11197 | fkl->kw[0].parse = hlua_filter_parse_fct; |
| 11198 | fkl->kw[0].private = reg_flt; |
| 11199 | memset(&fkl->kw[1], 0, sizeof(*fkl->kw)); |
| 11200 | |
| 11201 | /* Register this new filter */ |
| 11202 | flt_register_keywords(fkl); |
| 11203 | |
| 11204 | return 0; |
| 11205 | |
| 11206 | alloc_error: |
| 11207 | release_hlua_reg_filter(reg_flt); |
| 11208 | ha_free(&fkl); |
| 11209 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 11210 | return 0; /* Never reached */ |
| 11211 | } |
| 11212 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11213 | 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] | 11214 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11215 | char **err, unsigned int *timeout) |
| 11216 | { |
| 11217 | const char *error; |
| 11218 | |
| 11219 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 11220 | if (error == PARSE_TIME_OVER) { |
| 11221 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 11222 | args[1], args[0]); |
| 11223 | return -1; |
| 11224 | } |
| 11225 | else if (error == PARSE_TIME_UNDER) { |
| 11226 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 11227 | args[1], args[0]); |
| 11228 | return -1; |
| 11229 | } |
| 11230 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11231 | memprintf(err, "%s: invalid timeout", args[0]); |
| 11232 | return -1; |
| 11233 | } |
| 11234 | return 0; |
| 11235 | } |
| 11236 | |
| 11237 | 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] | 11238 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11239 | char **err) |
| 11240 | { |
| 11241 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11242 | file, line, err, &hlua_timeout_session); |
| 11243 | } |
| 11244 | |
| 11245 | 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] | 11246 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11247 | char **err) |
| 11248 | { |
| 11249 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11250 | file, line, err, &hlua_timeout_task); |
| 11251 | } |
| 11252 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11253 | 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] | 11254 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11255 | char **err) |
| 11256 | { |
| 11257 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11258 | file, line, err, &hlua_timeout_applet); |
| 11259 | } |
| 11260 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11261 | 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] | 11262 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11263 | char **err) |
| 11264 | { |
| 11265 | char *error; |
| 11266 | |
| 11267 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 11268 | if (*error != '\0') { |
| 11269 | memprintf(err, "%s: invalid number", args[0]); |
| 11270 | return -1; |
| 11271 | } |
| 11272 | return 0; |
| 11273 | } |
| 11274 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11275 | 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] | 11276 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11277 | char **err) |
| 11278 | { |
| 11279 | char *error; |
| 11280 | |
| 11281 | if (*(args[1]) == 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11282 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).", args[0]); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11283 | return -1; |
| 11284 | } |
| 11285 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 11286 | if (*error != '\0') { |
| 11287 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 11288 | return -1; |
| 11289 | } |
| 11290 | return 0; |
| 11291 | } |
| 11292 | |
| 11293 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11294 | /* This function is called by the main configuration key "lua-load". It loads and |
| 11295 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 11296 | * the main lua entry point. |
| 11297 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 11298 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 11299 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11300 | * |
| 11301 | * In some error case, LUA set an error message in top of the stack. This function |
| 11302 | * 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] | 11303 | * |
| 11304 | * This function can fail with an abort() due to an Lua critical error. |
| 11305 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 11306 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11307 | */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11308 | static int hlua_load_state(char **args, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11309 | { |
| 11310 | int error; |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11311 | int nargs; |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11312 | |
| 11313 | /* Just load and compile the file. */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11314 | error = luaL_loadfile(L, args[0]); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11315 | if (error) { |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11316 | memprintf(err, "error in Lua file '%s': %s", args[0], lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11317 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11318 | return -1; |
| 11319 | } |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11320 | |
| 11321 | /* Push args in the Lua stack, except the first one which is the filename */ |
| 11322 | for (nargs = 1; *(args[nargs]) != 0; nargs++) { |
Aurelien DARRAGON | 4d7aefe | 2022-09-23 10:22:14 +0200 | [diff] [blame] | 11323 | /* Check stack size. */ |
| 11324 | if (!lua_checkstack(L, 1)) { |
| 11325 | memprintf(err, "Lua runtime error while loading arguments: stack is full."); |
| 11326 | return -1; |
| 11327 | } |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11328 | lua_pushstring(L, args[nargs]); |
| 11329 | } |
| 11330 | nargs--; |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11331 | |
| 11332 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11333 | error = lua_pcall(L, nargs, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11334 | switch (error) { |
| 11335 | case LUA_OK: |
| 11336 | break; |
| 11337 | case LUA_ERRRUN: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11338 | memprintf(err, "Lua runtime error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11339 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11340 | return -1; |
| 11341 | case LUA_ERRMEM: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11342 | memprintf(err, "Lua out of memory error"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11343 | return -1; |
| 11344 | case LUA_ERRERR: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11345 | memprintf(err, "Lua message handler error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11346 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11347 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 11348 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11349 | case LUA_ERRGCMM: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11350 | memprintf(err, "Lua garbage collector error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11351 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11352 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 11353 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11354 | default: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11355 | memprintf(err, "Lua unknown error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11356 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11357 | return -1; |
| 11358 | } |
| 11359 | |
| 11360 | return 0; |
| 11361 | } |
| 11362 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11363 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 11364 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11365 | char **err) |
| 11366 | { |
| 11367 | if (*(args[1]) == 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11368 | memprintf(err, "'%s' expects a file name as parameter.", args[0]); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11369 | return -1; |
| 11370 | } |
| 11371 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11372 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11373 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11374 | ha_set_thread(NULL); |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11375 | return hlua_load_state(&args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11376 | } |
| 11377 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11378 | 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] | 11379 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11380 | char **err) |
| 11381 | { |
| 11382 | int len; |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11383 | int i; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11384 | |
| 11385 | if (*(args[1]) == 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11386 | memprintf(err, "'%s' expects a file as parameter.", args[0]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11387 | return -1; |
| 11388 | } |
| 11389 | |
| 11390 | if (per_thread_load == NULL) { |
| 11391 | /* allocate the first entry large enough to store the final NULL */ |
| 11392 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 11393 | if (per_thread_load == NULL) { |
| 11394 | memprintf(err, "out of memory error"); |
| 11395 | return -1; |
| 11396 | } |
| 11397 | } |
| 11398 | |
| 11399 | /* count used entries */ |
| 11400 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 11401 | ; |
| 11402 | |
| 11403 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 11404 | if (per_thread_load == NULL) { |
| 11405 | memprintf(err, "out of memory error"); |
| 11406 | return -1; |
| 11407 | } |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11408 | per_thread_load[len + 1] = NULL; |
| 11409 | |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11410 | /* count args excepting the first, allocate array and copy args */ |
| 11411 | for (i = 0; *(args[i + 1]) != 0; i++); |
Aurelien DARRAGON | b12d169 | 2022-09-23 08:48:34 +0200 | [diff] [blame] | 11412 | per_thread_load[len] = calloc(i + 1, sizeof(*per_thread_load[len])); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11413 | if (per_thread_load[len] == NULL) { |
| 11414 | memprintf(err, "out of memory error"); |
| 11415 | return -1; |
| 11416 | } |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11417 | for (i = 1; *(args[i]) != 0; i++) { |
| 11418 | per_thread_load[len][i - 1] = strdup(args[i]); |
| 11419 | if (per_thread_load[len][i - 1] == NULL) { |
| 11420 | memprintf(err, "out of memory error"); |
| 11421 | return -1; |
| 11422 | } |
| 11423 | } |
| 11424 | per_thread_load[len][i - 1] = strdup(""); |
| 11425 | if (per_thread_load[len][i - 1] == NULL) { |
| 11426 | memprintf(err, "out of memory error"); |
| 11427 | return -1; |
| 11428 | } |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11429 | |
| 11430 | /* loading for thread 1 only */ |
| 11431 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11432 | ha_set_thread(NULL); |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11433 | return hlua_load_state(per_thread_load[len], hlua_states[1], err); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11434 | } |
| 11435 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11436 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 11437 | * in the given <ctx>. |
| 11438 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 11439 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11440 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 11441 | lua_getglobal(L, "package"); /* push package variable */ |
| 11442 | lua_pushstring(L, path); /* push given path */ |
| 11443 | lua_pushstring(L, ";"); /* push semicolon */ |
| 11444 | lua_getfield(L, -3, type); /* push old path */ |
| 11445 | lua_concat(L, 3); /* concatenate to new path */ |
| 11446 | lua_setfield(L, -2, type); /* store new path */ |
| 11447 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11448 | |
| 11449 | return 0; |
| 11450 | } |
| 11451 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11452 | 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] | 11453 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11454 | char **err) |
| 11455 | { |
| 11456 | char *path; |
| 11457 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11458 | struct prepend_path *p = NULL; |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 11459 | size_t i; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11460 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11461 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11462 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11463 | } |
| 11464 | |
| 11465 | if (!(*args[1])) { |
| 11466 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11467 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11468 | } |
| 11469 | path = args[1]; |
| 11470 | |
| 11471 | if (*args[2]) { |
| 11472 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 11473 | 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] | 11474 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11475 | } |
| 11476 | type = args[2]; |
| 11477 | } |
| 11478 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11479 | p = calloc(1, sizeof(*p)); |
| 11480 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11481 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11482 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11483 | } |
| 11484 | p->path = strdup(path); |
| 11485 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11486 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11487 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11488 | } |
| 11489 | p->type = strdup(type); |
| 11490 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11491 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11492 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11493 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 11494 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11495 | |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 11496 | /* Handle the global state and the per-thread state for the first |
| 11497 | * thread. The remaining threads will be initialized based on |
| 11498 | * prepend_path_list. |
| 11499 | */ |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 11500 | for (i = 0; i < 2; i++) { |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 11501 | lua_State *L = hlua_states[i]; |
| 11502 | const char *error; |
| 11503 | |
| 11504 | if (setjmp(safe_ljmp_env) != 0) { |
| 11505 | lua_atpanic(L, hlua_panic_safe); |
| 11506 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11507 | error = lua_tostring(L, -1); |
| 11508 | else |
| 11509 | error = "critical error"; |
| 11510 | fprintf(stderr, "lua-prepend-path: %s.\n", error); |
| 11511 | exit(1); |
| 11512 | } else { |
| 11513 | lua_atpanic(L, hlua_panic_ljmp); |
| 11514 | } |
| 11515 | |
| 11516 | hlua_prepend_path(L, type, path); |
| 11517 | |
| 11518 | lua_atpanic(L, hlua_panic_safe); |
| 11519 | } |
| 11520 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11521 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11522 | |
| 11523 | err2: |
| 11524 | free(p->type); |
| 11525 | free(p->path); |
| 11526 | err: |
| 11527 | free(p); |
| 11528 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11529 | } |
| 11530 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11531 | /* configuration keywords declaration */ |
| 11532 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11533 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11534 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11535 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11536 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 11537 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 11538 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11539 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11540 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11541 | { 0, NULL, NULL }, |
| 11542 | }}; |
| 11543 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 11544 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 11545 | |
William Lallemand | 5213918 | 2022-03-30 15:05:42 +0200 | [diff] [blame] | 11546 | #ifdef USE_OPENSSL |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 11547 | |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11548 | /* |
| 11549 | * This function replace a ckch_store by another one, and rebuild the ckch_inst and all its dependencies. |
| 11550 | * It does the sam as "cli_io_handler_commit_cert" but for lua, the major |
| 11551 | * difference is that the yield in lua and for the CLI is not handled the same |
| 11552 | * way. |
| 11553 | */ |
| 11554 | __LJMP static int hlua_ckch_commit_yield(lua_State *L, int status, lua_KContext ctx) |
| 11555 | { |
| 11556 | struct ckch_inst **lua_ckchi = lua_touserdata(L, -1); |
| 11557 | struct ckch_store **lua_ckchs = lua_touserdata(L, -2); |
| 11558 | struct ckch_inst *ckchi = *lua_ckchi; |
| 11559 | struct ckch_store *old_ckchs = lua_ckchs[0]; |
| 11560 | struct ckch_store *new_ckchs = lua_ckchs[1]; |
| 11561 | struct hlua *hlua; |
| 11562 | char *err = NULL; |
| 11563 | int y = 1; |
| 11564 | |
| 11565 | hlua = hlua_gethlua(L); |
| 11566 | |
| 11567 | /* get the first ckchi to copy */ |
| 11568 | if (ckchi == NULL) |
| 11569 | ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs); |
| 11570 | |
| 11571 | /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */ |
| 11572 | list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) { |
| 11573 | struct ckch_inst *new_inst; |
| 11574 | |
| 11575 | /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */ |
| 11576 | if (y % 10 == 0) { |
| 11577 | |
| 11578 | *lua_ckchi = ckchi; |
| 11579 | |
| 11580 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 11581 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_ckch_commit_yield, TICK_ETERNITY, 0)); |
| 11582 | } |
| 11583 | |
| 11584 | if (ckch_inst_rebuild(new_ckchs, ckchi, &new_inst, &err)) |
| 11585 | goto error; |
| 11586 | |
| 11587 | /* link the new ckch_inst to the duplicate */ |
| 11588 | LIST_APPEND(&new_ckchs->ckch_inst, &new_inst->by_ckchs); |
| 11589 | y++; |
| 11590 | } |
| 11591 | |
| 11592 | /* The generation is finished, we can insert everything */ |
| 11593 | ckch_store_replace(old_ckchs, new_ckchs); |
| 11594 | |
| 11595 | lua_pop(L, 2); /* pop the lua_ckchs and ckchi */ |
| 11596 | |
| 11597 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11598 | |
| 11599 | return 0; |
| 11600 | |
| 11601 | error: |
| 11602 | ckch_store_free(new_ckchs); |
| 11603 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11604 | WILL_LJMP(luaL_error(L, "%s", err)); |
| 11605 | free(err); |
| 11606 | |
| 11607 | return 0; |
| 11608 | } |
| 11609 | |
| 11610 | /* |
| 11611 | * Replace a ckch_store <filename> in the ckchs_tree with a ckch_store created |
| 11612 | * from the table in parameter. |
| 11613 | * |
| 11614 | * This is equivalent to "set ssl cert" + "commit ssl cert" over the CLI, which |
| 11615 | * means it does not need to have a transaction since everything is done in the |
| 11616 | * same function. |
| 11617 | * |
| 11618 | * CertCache.set{filename="", crt="", key="", sctl="", ocsp="", issuer=""} |
| 11619 | * |
| 11620 | */ |
| 11621 | __LJMP static int hlua_ckch_set(lua_State *L) |
| 11622 | { |
| 11623 | struct hlua *hlua; |
| 11624 | struct ckch_inst **lua_ckchi; |
| 11625 | struct ckch_store **lua_ckchs; |
| 11626 | struct ckch_store *old_ckchs = NULL; |
| 11627 | struct ckch_store *new_ckchs = NULL; |
| 11628 | int errcode = 0; |
| 11629 | char *err = NULL; |
| 11630 | struct cert_exts *cert_ext = NULL; |
| 11631 | char *filename; |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 11632 | struct ckch_data *data; |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11633 | int ret; |
| 11634 | |
| 11635 | if (lua_type(L, -1) != LUA_TTABLE) |
| 11636 | WILL_LJMP(luaL_error(L, "'CertCache.set' needs a table as argument")); |
| 11637 | |
| 11638 | hlua = hlua_gethlua(L); |
| 11639 | |
| 11640 | /* FIXME: this should not return an error but should come back later */ |
| 11641 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 11642 | WILL_LJMP(luaL_error(L, "CertCache already under lock")); |
| 11643 | |
| 11644 | ret = lua_getfield(L, -1, "filename"); |
| 11645 | if (ret != LUA_TSTRING) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11646 | memprintf(&err, "%sNo filename specified!", err ? err : ""); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11647 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11648 | goto end; |
| 11649 | } |
| 11650 | filename = (char *)lua_tostring(L, -1); |
| 11651 | |
| 11652 | |
| 11653 | /* look for the filename in the tree */ |
| 11654 | old_ckchs = ckchs_lookup(filename); |
| 11655 | if (!old_ckchs) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11656 | memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!", err ? err : ""); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11657 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11658 | goto end; |
| 11659 | } |
| 11660 | /* TODO: handle extra_files_noext */ |
| 11661 | |
| 11662 | new_ckchs = ckchs_dup(old_ckchs); |
| 11663 | if (!new_ckchs) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11664 | memprintf(&err, "%sCannot allocate memory!", err ? err : ""); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11665 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11666 | goto end; |
| 11667 | } |
| 11668 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 11669 | data = new_ckchs->data; |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11670 | |
| 11671 | /* loop on the field in the table, which have the same name as the |
| 11672 | * possible extensions of files */ |
| 11673 | lua_pushnil(L); |
| 11674 | while (lua_next(L, 1)) { |
| 11675 | int i; |
| 11676 | const char *field = lua_tostring(L, -2); |
| 11677 | char *payload = (char *)lua_tostring(L, -1); |
| 11678 | |
| 11679 | if (!field || strcmp(field, "filename") == 0) { |
| 11680 | lua_pop(L, 1); |
| 11681 | continue; |
| 11682 | } |
| 11683 | |
| 11684 | for (i = 0; field && cert_exts[i].ext != NULL; i++) { |
| 11685 | if (strcmp(field, cert_exts[i].ext) == 0) { |
| 11686 | cert_ext = &cert_exts[i]; |
| 11687 | break; |
| 11688 | } |
| 11689 | } |
| 11690 | |
| 11691 | /* this is the default type, the field is not supported */ |
| 11692 | if (cert_ext == NULL) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11693 | memprintf(&err, "%sUnsupported field '%s'", err ? err : "", field); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11694 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11695 | goto end; |
| 11696 | } |
| 11697 | |
| 11698 | /* appply the change on the duplicate */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 11699 | if (cert_ext->load(filename, payload, data, &err) != 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11700 | memprintf(&err, "%sCan't load the payload for '%s'", err ? err : "", cert_ext->ext); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11701 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11702 | goto end; |
| 11703 | } |
| 11704 | lua_pop(L, 1); |
| 11705 | } |
| 11706 | |
| 11707 | /* store the pointers on the lua stack */ |
| 11708 | lua_ckchs = lua_newuserdata(L, sizeof(struct ckch_store *) * 2); |
| 11709 | lua_ckchs[0] = old_ckchs; |
| 11710 | lua_ckchs[1] = new_ckchs; |
| 11711 | lua_ckchi = lua_newuserdata(L, sizeof(struct ckch_inst *)); |
| 11712 | *lua_ckchi = NULL; |
| 11713 | |
| 11714 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 11715 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_ckch_commit_yield, TICK_ETERNITY, 0)); |
| 11716 | |
| 11717 | end: |
| 11718 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11719 | |
| 11720 | if (errcode & ERR_CODE) { |
| 11721 | ckch_store_free(new_ckchs); |
| 11722 | WILL_LJMP(luaL_error(L, "%s", err)); |
| 11723 | } |
| 11724 | free(err); |
| 11725 | |
| 11726 | return 0; |
| 11727 | } |
| 11728 | |
William Lallemand | 5213918 | 2022-03-30 15:05:42 +0200 | [diff] [blame] | 11729 | #else |
| 11730 | |
| 11731 | __LJMP static int hlua_ckch_set(lua_State *L) |
| 11732 | { |
| 11733 | WILL_LJMP(luaL_error(L, "'CertCache.set' needs an HAProxy built with OpenSSL")); |
| 11734 | |
| 11735 | return 0; |
| 11736 | } |
| 11737 | #endif /* ! USE_OPENSSL */ |
| 11738 | |
| 11739 | |
| 11740 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 11741 | /* This function can fail with an abort() due to an Lua critical error. |
| 11742 | * We are in the initialisation process of HAProxy, this abort() is |
| 11743 | * tolerated. |
| 11744 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11745 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11746 | { |
| 11747 | struct hlua_init_function *init; |
| 11748 | const char *msg; |
| 11749 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 11750 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11751 | const char *kind; |
| 11752 | const char *trace; |
| 11753 | int return_status = 1; |
| 11754 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 11755 | int nres; |
| 11756 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11757 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11758 | /* disable memory limit checks if limit is not set */ |
| 11759 | if (!hlua_global_allocator.limit) |
| 11760 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 11761 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 11762 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11763 | if (setjmp(safe_ljmp_env) != 0) { |
| 11764 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11765 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11766 | error = lua_tostring(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11767 | else |
| 11768 | error = "critical error"; |
| 11769 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 11770 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11771 | } else { |
| 11772 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11773 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 11774 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11775 | hlua_fcn_post_init(L); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11776 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11777 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11778 | lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11779 | |
| 11780 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11781 | ret = lua_resume(L, L, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11782 | #else |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11783 | ret = lua_resume(L, L, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11784 | #endif |
| 11785 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11786 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11787 | |
| 11788 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11789 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 11790 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11791 | |
| 11792 | case LUA_ERRERR: |
| 11793 | kind = "message handler error"; |
Willy Tarreau | 14de395 | 2022-11-14 07:08:28 +0100 | [diff] [blame] | 11794 | __fallthrough; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11795 | case LUA_ERRRUN: |
| 11796 | if (!kind) |
| 11797 | kind = "runtime error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11798 | msg = lua_tostring(L, -1); |
| 11799 | lua_settop(L, 0); /* Empty the stack. */ |
| 11800 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 11801 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11802 | if (msg) |
| 11803 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 11804 | else |
| 11805 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
| 11806 | return_status = 0; |
| 11807 | break; |
| 11808 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11809 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11810 | /* Unknown error */ |
| 11811 | kind = "Unknown error"; |
Willy Tarreau | 14de395 | 2022-11-14 07:08:28 +0100 | [diff] [blame] | 11812 | __fallthrough; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11813 | case LUA_YIELD: |
| 11814 | /* yield is not configured at this step, this state doesn't happen */ |
| 11815 | if (!kind) |
| 11816 | kind = "yield not allowed"; |
Willy Tarreau | 14de395 | 2022-11-14 07:08:28 +0100 | [diff] [blame] | 11817 | __fallthrough; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11818 | case LUA_ERRMEM: |
| 11819 | if (!kind) |
| 11820 | kind = "out of memory error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11821 | lua_settop(L, 0); |
| 11822 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 11823 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11824 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 11825 | return_status = 0; |
| 11826 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11827 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11828 | if (!return_status) |
| 11829 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11830 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11831 | |
| 11832 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11833 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11834 | } |
| 11835 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11836 | int hlua_post_init() |
| 11837 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11838 | int ret; |
| 11839 | int i; |
| 11840 | int errors; |
| 11841 | char *err = NULL; |
| 11842 | struct hlua_function *fcn; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11843 | struct hlua_reg_filter *reg_flt; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11844 | |
Willy Tarreau | b131049 | 2021-08-30 09:35:18 +0200 | [diff] [blame] | 11845 | #if defined(USE_OPENSSL) |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11846 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11847 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11848 | int saved_used_backed = global.ssl_used_backend; |
| 11849 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11850 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11851 | global.ssl_used_backend = saved_used_backed; |
| 11852 | } |
| 11853 | #endif |
| 11854 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11855 | /* Perform post init of common thread */ |
| 11856 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11857 | ha_set_thread(&ha_thread_info[0]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11858 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 11859 | if (ret == 0) |
| 11860 | return 0; |
| 11861 | |
| 11862 | /* init remaining lua states and load files */ |
| 11863 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 11864 | |
| 11865 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11866 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11867 | |
| 11868 | /* Init lua state */ |
| 11869 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 11870 | |
| 11871 | /* Load lua files */ |
| 11872 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 11873 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 11874 | if (ret != 0) { |
| 11875 | ha_alert("Lua init: %s\n", err); |
| 11876 | return 0; |
| 11877 | } |
| 11878 | } |
| 11879 | } |
| 11880 | |
| 11881 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11882 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11883 | |
| 11884 | /* Execute post init for all states */ |
| 11885 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 11886 | |
| 11887 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11888 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11889 | |
| 11890 | /* run post init */ |
| 11891 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 11892 | if (ret == 0) |
| 11893 | return 0; |
| 11894 | } |
| 11895 | |
| 11896 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11897 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11898 | |
| 11899 | /* control functions registering. Each function must have: |
| 11900 | * - only the function_ref[0] set positive and all other to -1 |
| 11901 | * - only the function_ref[0] set to -1 and all other positive |
| 11902 | * This ensure a same reference is not used both in shared |
| 11903 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11904 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11905 | * complicated to found for the end user. |
| 11906 | */ |
| 11907 | errors = 0; |
| 11908 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 11909 | ret = 0; |
| 11910 | for (i = 1; i < global.nbthread + 1; i++) { |
| 11911 | if (fcn->function_ref[i] == -1) |
| 11912 | ret--; |
| 11913 | else |
| 11914 | ret++; |
| 11915 | } |
| 11916 | if (abs(ret) != global.nbthread) { |
| 11917 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 11918 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 11919 | errors++; |
| 11920 | continue; |
| 11921 | } |
| 11922 | |
| 11923 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11924 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 11925 | "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] | 11926 | "exclusive.\n", fcn->name); |
| 11927 | errors++; |
| 11928 | } |
| 11929 | } |
| 11930 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11931 | /* Do the same with registered filters */ |
| 11932 | list_for_each_entry(reg_flt, &referenced_filters, l) { |
| 11933 | ret = 0; |
| 11934 | for (i = 1; i < global.nbthread + 1; i++) { |
| 11935 | if (reg_flt->flt_ref[i] == -1) |
| 11936 | ret--; |
| 11937 | else |
| 11938 | ret++; |
| 11939 | } |
| 11940 | if (abs(ret) != global.nbthread) { |
| 11941 | ha_alert("Lua filter '%s' is not referenced in all thread. " |
| 11942 | "Expect function in all thread or in none thread.\n", reg_flt->name); |
| 11943 | errors++; |
| 11944 | continue; |
| 11945 | } |
| 11946 | |
| 11947 | if ((reg_flt->flt_ref[0] == -1) == (ret < 0)) { |
| 11948 | ha_alert("Lua filter '%s' is referenced both ins shared Lua context (through lua-load) " |
| 11949 | "and per-thread Lua context (through lua-load-per-thread). these two context " |
| 11950 | "exclusive.\n", fcn->name); |
| 11951 | errors++; |
| 11952 | } |
| 11953 | } |
| 11954 | |
| 11955 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11956 | if (errors > 0) |
| 11957 | return 0; |
| 11958 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11959 | /* 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] | 11960 | * -1 in order to have probably a segfault if someone use it |
| 11961 | */ |
| 11962 | hlua_state_id = -1; |
| 11963 | |
| 11964 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11965 | } |
| 11966 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11967 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 11968 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 11969 | * 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] | 11970 | * allocation. <nsize> is the requested new size. A new allocation is |
| 11971 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11972 | * zero. This one verifies that the limits are respected but is optimized |
| 11973 | * 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] | 11974 | * |
| 11975 | * Warning: while this API ressembles glibc's realloc() a lot, glibc surpasses |
| 11976 | * POSIX by making realloc(ptr,0) an effective free(), but others do not do |
| 11977 | * that and will simply allocate zero as if it were the result of malloc(0), |
| 11978 | * so mapping this onto realloc() will lead to memory leaks on non-glibc |
| 11979 | * systems. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11980 | */ |
| 11981 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 11982 | { |
| 11983 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11984 | size_t limit, old, new; |
| 11985 | |
| 11986 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 11987 | * for accounting anymore. |
| 11988 | */ |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 11989 | if (likely(~zone->limit == 0)) { |
| 11990 | if (!nsize) |
| 11991 | ha_free(&ptr); |
| 11992 | else |
| 11993 | ptr = realloc(ptr, nsize); |
| 11994 | return ptr; |
| 11995 | } |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11996 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 11997 | if (!ptr) |
| 11998 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11999 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 12000 | /* enforce strict limits across all threads */ |
| 12001 | limit = zone->limit; |
| 12002 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 12003 | do { |
| 12004 | new = old + nsize - osize; |
| 12005 | if (unlikely(nsize && limit && new > limit)) |
| 12006 | return NULL; |
| 12007 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12008 | |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 12009 | if (!nsize) |
| 12010 | ha_free(&ptr); |
| 12011 | else |
| 12012 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 12013 | |
| 12014 | if (unlikely(!ptr && nsize)) // failed |
| 12015 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 12016 | |
| 12017 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12018 | return ptr; |
| 12019 | } |
| 12020 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 12021 | /* 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] | 12022 | * We are in the initialisation process of HAProxy, this abort() is |
| 12023 | * tolerated. |
| 12024 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 12025 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 12026 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12027 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12028 | int idx; |
| 12029 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12030 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12031 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 12032 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 12033 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12034 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12035 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12036 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 12037 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12038 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 12039 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 12040 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12041 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 12042 | *context = NULL; |
| 12043 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 12044 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 12045 | * the Lua function can fail with an abort. We are in the initialisation |
| 12046 | * process of HAProxy, this abort() is tolerated. |
| 12047 | */ |
| 12048 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 12049 | /* Call post initialisation function in safe environment. */ |
| 12050 | if (setjmp(safe_ljmp_env) != 0) { |
| 12051 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12052 | if (lua_type(L, -1) == LUA_TSTRING) |
| 12053 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 12054 | else |
| 12055 | error_msg = "critical error"; |
| 12056 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 12057 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 12058 | } else { |
| 12059 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 12060 | } |
| 12061 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 12062 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12063 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 12064 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 12065 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 12066 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12067 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 12068 | #endif |
| 12069 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12070 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 12071 | #endif |
| 12072 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 12073 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12074 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12075 | /* Apply configured prepend path */ |
| 12076 | list_for_each_entry(pp, &prepend_path_list, l) |
| 12077 | hlua_prepend_path(L, pp->type, pp->path); |
| 12078 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12079 | /* |
| 12080 | * |
| 12081 | * Create "core" object. |
| 12082 | * |
| 12083 | */ |
| 12084 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 12085 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12086 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12087 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 12088 | /* set the thread id */ |
| 12089 | hlua_class_const_int(L, "thread", thread_num); |
| 12090 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12091 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 12092 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12093 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12094 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12095 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12096 | hlua_class_function(L, "register_init", hlua_register_init); |
| 12097 | hlua_class_function(L, "register_task", hlua_register_task); |
| 12098 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 12099 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 12100 | hlua_class_function(L, "register_action", hlua_register_action); |
| 12101 | hlua_class_function(L, "register_service", hlua_register_service); |
| 12102 | hlua_class_function(L, "register_cli", hlua_register_cli); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12103 | hlua_class_function(L, "register_filter", hlua_register_filter); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12104 | hlua_class_function(L, "yield", hlua_yield); |
| 12105 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 12106 | hlua_class_function(L, "sleep", hlua_sleep); |
| 12107 | hlua_class_function(L, "msleep", hlua_msleep); |
| 12108 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 12109 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 12110 | hlua_class_function(L, "set_map", hlua_set_map); |
| 12111 | hlua_class_function(L, "del_map", hlua_del_map); |
| 12112 | hlua_class_function(L, "tcp", hlua_socket_new); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12113 | hlua_class_function(L, "httpclient", hlua_httpclient_new); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12114 | hlua_class_function(L, "log", hlua_log); |
| 12115 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 12116 | hlua_class_function(L, "Info", hlua_log_info); |
| 12117 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 12118 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 12119 | hlua_class_function(L, "done", hlua_done); |
| 12120 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12121 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12122 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12123 | |
| 12124 | /* |
| 12125 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12126 | * Create "act" object. |
| 12127 | * |
| 12128 | */ |
| 12129 | |
| 12130 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12131 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12132 | |
| 12133 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12134 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 12135 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 12136 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 12137 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 12138 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 12139 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 12140 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 12141 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12142 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12143 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 12144 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12145 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12146 | |
| 12147 | /* |
| 12148 | * |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12149 | * Create "Filter" object. |
| 12150 | * |
| 12151 | */ |
| 12152 | |
| 12153 | /* This table entry is the object "filter" base. */ |
| 12154 | lua_newtable(L); |
| 12155 | |
| 12156 | /* push flags and constants */ |
| 12157 | hlua_class_const_int(L, "CONTINUE", 1); |
| 12158 | hlua_class_const_int(L, "WAIT", 0); |
| 12159 | hlua_class_const_int(L, "ERROR", -1); |
| 12160 | |
| 12161 | hlua_class_const_int(L, "FLT_CFG_FL_HTX", FLT_CFG_FL_HTX); |
| 12162 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 12163 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
| 12164 | hlua_class_function(L, "register_data_filter", hlua_register_data_filter); |
| 12165 | hlua_class_function(L, "unregister_data_filter", hlua_unregister_data_filter); |
| 12166 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12167 | lua_setglobal(L, "filter"); |
| 12168 | |
| 12169 | /* |
| 12170 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12171 | * Register class Map |
| 12172 | * |
| 12173 | */ |
| 12174 | |
| 12175 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12176 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12177 | |
| 12178 | /* register pattern types. */ |
| 12179 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12180 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 12181 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12182 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12183 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 12184 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12185 | |
| 12186 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12187 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12188 | |
| 12189 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12190 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12191 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12192 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12193 | lua_pushstring(L, "__index"); |
| 12194 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12195 | |
| 12196 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12197 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 12198 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12199 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12200 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12201 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 12202 | /* Register previous table in the registry with reference and named entry. |
| 12203 | * The function hlua_register_metatable() pops the stack, so we |
| 12204 | * previously create a copy of the table. |
| 12205 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12206 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 12207 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12208 | |
| 12209 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12210 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12211 | |
| 12212 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12213 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12214 | |
| 12215 | /* |
| 12216 | * |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12217 | * Register "CertCache" class |
| 12218 | * |
| 12219 | */ |
| 12220 | |
| 12221 | /* Create and fill the metatable. */ |
| 12222 | lua_newtable(L); |
| 12223 | /* Register */ |
| 12224 | hlua_class_function(L, "set", hlua_ckch_set); |
| 12225 | lua_setglobal(L, CLASS_CERTCACHE); /* Create global object called Regex */ |
| 12226 | |
| 12227 | /* |
| 12228 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12229 | * Register class Channel |
| 12230 | * |
| 12231 | */ |
| 12232 | |
| 12233 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12234 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12235 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12236 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12237 | lua_pushstring(L, "__index"); |
| 12238 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12239 | |
| 12240 | /* Register . */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12241 | hlua_class_function(L, "data", hlua_channel_get_data); |
| 12242 | hlua_class_function(L, "line", hlua_channel_get_line); |
| 12243 | hlua_class_function(L, "set", hlua_channel_set_data); |
| 12244 | hlua_class_function(L, "remove", hlua_channel_del_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12245 | hlua_class_function(L, "append", hlua_channel_append); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12246 | hlua_class_function(L, "prepend", hlua_channel_prepend); |
| 12247 | hlua_class_function(L, "insert", hlua_channel_insert_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12248 | hlua_class_function(L, "send", hlua_channel_send); |
| 12249 | hlua_class_function(L, "forward", hlua_channel_forward); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12250 | hlua_class_function(L, "input", hlua_channel_get_in_len); |
| 12251 | hlua_class_function(L, "output", hlua_channel_get_out_len); |
| 12252 | hlua_class_function(L, "may_recv", hlua_channel_may_recv); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12253 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 12254 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12255 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12256 | /* Deprecated API */ |
| 12257 | hlua_class_function(L, "get", hlua_channel_get); |
| 12258 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 12259 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 12260 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 12261 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 12262 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12263 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12264 | |
| 12265 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12266 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12267 | |
| 12268 | /* |
| 12269 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12270 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12271 | * |
| 12272 | */ |
| 12273 | |
| 12274 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12275 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12276 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12277 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12278 | lua_pushstring(L, "__index"); |
| 12279 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12280 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12281 | /* Browse existing fetches and create the associated |
| 12282 | * object method. |
| 12283 | */ |
| 12284 | sf = NULL; |
| 12285 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12286 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 12287 | * by an underscore. |
| 12288 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 12289 | strlcpy2(trash.area, sf->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12290 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12291 | if (*p == '.' || *p == '-' || *p == '+') |
| 12292 | *p = '_'; |
| 12293 | |
| 12294 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12295 | lua_pushstring(L, trash.area); |
| 12296 | lua_pushlightuserdata(L, sf); |
| 12297 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 12298 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12299 | } |
| 12300 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12301 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12302 | |
| 12303 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12304 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12305 | |
| 12306 | /* |
| 12307 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12308 | * Register class Converters |
| 12309 | * |
| 12310 | */ |
| 12311 | |
| 12312 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12313 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12314 | |
| 12315 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12316 | lua_pushstring(L, "__index"); |
| 12317 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12318 | |
| 12319 | /* Browse existing converters and create the associated |
| 12320 | * object method. |
| 12321 | */ |
| 12322 | sc = NULL; |
| 12323 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12324 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 12325 | * by an underscore. |
| 12326 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 12327 | strlcpy2(trash.area, sc->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12328 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12329 | if (*p == '.' || *p == '-' || *p == '+') |
| 12330 | *p = '_'; |
| 12331 | |
| 12332 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12333 | lua_pushstring(L, trash.area); |
| 12334 | lua_pushlightuserdata(L, sc); |
| 12335 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 12336 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12337 | } |
| 12338 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12339 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12340 | |
| 12341 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12342 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12343 | |
| 12344 | /* |
| 12345 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12346 | * Register class HTTP |
| 12347 | * |
| 12348 | */ |
| 12349 | |
| 12350 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12351 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12352 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12353 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12354 | lua_pushstring(L, "__index"); |
| 12355 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12356 | |
| 12357 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12358 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 12359 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 12360 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 12361 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 12362 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 12363 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 12364 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 12365 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 12366 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 12367 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12368 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12369 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 12370 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 12371 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 12372 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 12373 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 12374 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 12375 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12376 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12377 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12378 | |
| 12379 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12380 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12381 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 12382 | /* |
| 12383 | * |
| 12384 | * Register class HTTPMessage |
| 12385 | * |
| 12386 | */ |
| 12387 | |
| 12388 | /* Create and fill the metatable. */ |
| 12389 | lua_newtable(L); |
| 12390 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 12391 | /* Create and fill the __index entry. */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 12392 | lua_pushstring(L, "__index"); |
| 12393 | lua_newtable(L); |
| 12394 | |
| 12395 | /* Register Lua functions. */ |
| 12396 | hlua_class_function(L, "is_resp", hlua_http_msg_is_resp); |
| 12397 | hlua_class_function(L, "get_stline", hlua_http_msg_get_stline); |
| 12398 | hlua_class_function(L, "get_headers", hlua_http_msg_get_headers); |
| 12399 | hlua_class_function(L, "del_header", hlua_http_msg_del_hdr); |
| 12400 | hlua_class_function(L, "rep_header", hlua_http_msg_rep_hdr); |
| 12401 | hlua_class_function(L, "rep_value", hlua_http_msg_rep_val); |
| 12402 | hlua_class_function(L, "add_header", hlua_http_msg_add_hdr); |
| 12403 | hlua_class_function(L, "set_header", hlua_http_msg_set_hdr); |
| 12404 | hlua_class_function(L, "set_method", hlua_http_msg_set_meth); |
| 12405 | hlua_class_function(L, "set_path", hlua_http_msg_set_path); |
| 12406 | hlua_class_function(L, "set_query", hlua_http_msg_set_query); |
| 12407 | hlua_class_function(L, "set_uri", hlua_http_msg_set_uri); |
| 12408 | hlua_class_function(L, "set_status", hlua_http_msg_set_status); |
| 12409 | hlua_class_function(L, "is_full", hlua_http_msg_is_full); |
| 12410 | hlua_class_function(L, "may_recv", hlua_http_msg_may_recv); |
| 12411 | hlua_class_function(L, "eom", hlua_http_msg_is_eom); |
| 12412 | hlua_class_function(L, "input", hlua_http_msg_get_in_len); |
| 12413 | hlua_class_function(L, "output", hlua_http_msg_get_out_len); |
| 12414 | |
| 12415 | hlua_class_function(L, "body", hlua_http_msg_get_body); |
| 12416 | hlua_class_function(L, "set", hlua_http_msg_set_data); |
| 12417 | hlua_class_function(L, "remove", hlua_http_msg_del_data); |
| 12418 | hlua_class_function(L, "append", hlua_http_msg_append); |
| 12419 | hlua_class_function(L, "prepend", hlua_http_msg_prepend); |
| 12420 | hlua_class_function(L, "insert", hlua_http_msg_insert_data); |
| 12421 | hlua_class_function(L, "set_eom", hlua_http_msg_set_eom); |
| 12422 | hlua_class_function(L, "unset_eom", hlua_http_msg_unset_eom); |
| 12423 | |
| 12424 | hlua_class_function(L, "send", hlua_http_msg_send); |
| 12425 | hlua_class_function(L, "forward", hlua_http_msg_forward); |
| 12426 | |
| 12427 | lua_rawset(L, -3); |
| 12428 | |
| 12429 | /* Register previous table in the registry with reference and named entry. */ |
| 12430 | class_http_msg_ref = hlua_register_metatable(L, CLASS_HTTP_MSG); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12431 | |
| 12432 | /* |
| 12433 | * |
| 12434 | * Register class HTTPClient |
| 12435 | * |
| 12436 | */ |
| 12437 | |
| 12438 | /* Create and fill the metatable. */ |
| 12439 | lua_newtable(L); |
| 12440 | lua_pushstring(L, "__index"); |
| 12441 | lua_newtable(L); |
| 12442 | hlua_class_function(L, "get", hlua_httpclient_get); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 12443 | hlua_class_function(L, "head", hlua_httpclient_head); |
| 12444 | hlua_class_function(L, "put", hlua_httpclient_put); |
| 12445 | hlua_class_function(L, "post", hlua_httpclient_post); |
| 12446 | hlua_class_function(L, "delete", hlua_httpclient_delete); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12447 | lua_settable(L, -3); /* Sets the __index entry. */ |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 12448 | /* Register the garbage collector entry. */ |
| 12449 | lua_pushstring(L, "__gc"); |
| 12450 | lua_pushcclosure(L, hlua_httpclient_gc, 0); |
| 12451 | lua_settable(L, -3); /* Push the last 2 entries in the table at index -3 */ |
| 12452 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12453 | |
| 12454 | |
| 12455 | class_httpclient_ref = hlua_register_metatable(L, CLASS_HTTPCLIENT); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12456 | /* |
| 12457 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12458 | * Register class AppletTCP |
| 12459 | * |
| 12460 | */ |
| 12461 | |
| 12462 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12463 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12464 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12465 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12466 | lua_pushstring(L, "__index"); |
| 12467 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12468 | |
| 12469 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12470 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 12471 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 12472 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 12473 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 12474 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 12475 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 12476 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 12477 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12478 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12479 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12480 | |
| 12481 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12482 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12483 | |
| 12484 | /* |
| 12485 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12486 | * Register class AppletHTTP |
| 12487 | * |
| 12488 | */ |
| 12489 | |
| 12490 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12491 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12492 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12493 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12494 | lua_pushstring(L, "__index"); |
| 12495 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12496 | |
| 12497 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12498 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 12499 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 12500 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 12501 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 12502 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 12503 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 12504 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 12505 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 12506 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 12507 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 12508 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12509 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12510 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12511 | |
| 12512 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12513 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12514 | |
| 12515 | /* |
| 12516 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12517 | * Register class TXN |
| 12518 | * |
| 12519 | */ |
| 12520 | |
| 12521 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12522 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12523 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12524 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12525 | lua_pushstring(L, "__index"); |
| 12526 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12527 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 12528 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12529 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 12530 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 12531 | hlua_class_function(L, "set_var", hlua_set_var); |
| 12532 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 12533 | hlua_class_function(L, "get_var", hlua_get_var); |
| 12534 | hlua_class_function(L, "done", hlua_txn_done); |
| 12535 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 12536 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 12537 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 12538 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 12539 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 12540 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 12541 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 12542 | hlua_class_function(L, "log", hlua_txn_log); |
| 12543 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 12544 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 12545 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 12546 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 12547 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12548 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12549 | |
| 12550 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12551 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12552 | |
| 12553 | /* |
| 12554 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 12555 | * Register class reply |
| 12556 | * |
| 12557 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12558 | lua_newtable(L); |
| 12559 | lua_pushstring(L, "__index"); |
| 12560 | lua_newtable(L); |
| 12561 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 12562 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 12563 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 12564 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 12565 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 12566 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 12567 | |
| 12568 | |
| 12569 | /* |
| 12570 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12571 | * Register class Socket |
| 12572 | * |
| 12573 | */ |
| 12574 | |
| 12575 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12576 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12577 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12578 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12579 | lua_pushstring(L, "__index"); |
| 12580 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12581 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 12582 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12583 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 12584 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12585 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 12586 | hlua_class_function(L, "send", hlua_socket_send); |
| 12587 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 12588 | hlua_class_function(L, "close", hlua_socket_close); |
| 12589 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 12590 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 12591 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 12592 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12593 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12594 | 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] | 12595 | |
| 12596 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12597 | lua_pushstring(L, "__gc"); |
| 12598 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 12599 | 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] | 12600 | |
| 12601 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12602 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12603 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12604 | lua_atpanic(L, hlua_panic_safe); |
| 12605 | |
| 12606 | return L; |
| 12607 | } |
| 12608 | |
| 12609 | void hlua_init(void) { |
| 12610 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 12611 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12612 | #ifdef USE_OPENSSL |
| 12613 | struct srv_kw *kw; |
| 12614 | int tmp_error; |
| 12615 | char *error; |
| 12616 | char *args[] = { /* SSL client configuration. */ |
| 12617 | "ssl", |
| 12618 | "verify", |
| 12619 | "none", |
| 12620 | NULL |
| 12621 | }; |
| 12622 | #endif |
| 12623 | |
| 12624 | /* Init post init function list head */ |
| 12625 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 12626 | LIST_INIT(&hlua_init_functions[i]); |
| 12627 | |
| 12628 | /* Init state for common/shared lua parts */ |
| 12629 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12630 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12631 | hlua_states[0] = hlua_init_state(0); |
| 12632 | |
| 12633 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 12634 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12635 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12636 | hlua_states[1] = hlua_init_state(1); |
| 12637 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12638 | /* Proxy and server configuration initialisation. */ |
William Lallemand | 6bb77b9 | 2021-07-28 15:48:16 +0200 | [diff] [blame] | 12639 | 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] | 12640 | if (!socket_proxy) { |
| 12641 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 12642 | exit(1); |
| 12643 | } |
| 12644 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12645 | |
| 12646 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12647 | socket_tcp = new_server(socket_proxy); |
| 12648 | if (!socket_tcp) { |
| 12649 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 12650 | exit(1); |
| 12651 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12652 | |
| 12653 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12654 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12655 | socket_ssl = new_server(socket_proxy); |
| 12656 | if (!socket_ssl) { |
| 12657 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 12658 | exit(1); |
| 12659 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12660 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12661 | socket_ssl->use_ssl = 1; |
| 12662 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12663 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 12664 | for (i = 0; args[i] != NULL; i++) { |
| 12665 | 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] | 12666 | /* |
| 12667 | * |
| 12668 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 12669 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12670 | * features like client certificates and ssl_verify. |
| 12671 | * |
| 12672 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12673 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12674 | if (tmp_error != 0) { |
| 12675 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 12676 | abort(); /* This must be never arrives because the command line |
| 12677 | not editable by the user. */ |
| 12678 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 12679 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12680 | } |
| 12681 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12682 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 12683 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 12684 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12685 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 12686 | static void hlua_deinit() |
| 12687 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 12688 | int thr; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12689 | struct hlua_reg_filter *reg_flt, *reg_flt_bck; |
| 12690 | |
| 12691 | list_for_each_entry_safe(reg_flt, reg_flt_bck, &referenced_filters, l) |
| 12692 | release_hlua_reg_filter(reg_flt); |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 12693 | |
| 12694 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 12695 | if (hlua_states[thr]) |
| 12696 | lua_close(hlua_states[thr]); |
| 12697 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 12698 | |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 12699 | srv_drop(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 12700 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 12701 | #ifdef USE_OPENSSL |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 12702 | srv_drop(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 12703 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 12704 | |
| 12705 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 12706 | } |
| 12707 | |
| 12708 | REGISTER_POST_DEINIT(hlua_deinit); |
| 12709 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12710 | static void hlua_register_build_options(void) |
| 12711 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12712 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12713 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12714 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 12715 | hap_register_build_opts(ptr, 1); |
| 12716 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12717 | |
| 12718 | INITCALL0(STG_REGISTER, hlua_register_build_options); |